KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > servlets > responders > ResponderConnectionAgent


1 /******************************************************************************
2  * ResponderConnectionAgent.java
3  * ****************************************************************************/

4
5 /* J_LZ_COPYRIGHT_BEGIN *******************************************************
6 * Copyright 2001-2004 Laszlo Systems, Inc. All Rights Reserved. *
7 * Use is subject to license terms. *
8 * J_LZ_COPYRIGHT_END *********************************************************/

9
10 package org.openlaszlo.servlets.responders;
11
12 import java.io.*;
13 import java.util.*;
14 import java.net.*;
15 import javax.servlet.*;
16 import javax.servlet.http.*;
17 import org.openlaszlo.compiler.*;
18 import org.openlaszlo.connection.*;
19 import org.openlaszlo.server.*;
20 import org.openlaszlo.utils.*;
21 import org.apache.log4j.*;
22
23
24 // Connection agents are configured in ResponderConnection
25
public abstract class ResponderConnectionAgent extends Responder
26 {
27     private static Logger mLogger = Logger.getLogger(ResponderConnectionAgent.class);
28
29     public final static int SC_ERROR = 800;
30     public final static int SC_MISSING_PARAMETER = 801;
31
32     protected abstract void respondAgent(HttpServletRequest req, HttpServletResponse res,
33                                          ConnectionGroup group)
34         throws IOException;
35
36
37     protected void respondImpl(HttpServletRequest req, HttpServletResponse res)
38         throws IOException
39     {
40         String JavaDoc ip = req.getRemoteAddr();
41         String JavaDoc _url = req.getParameter("url");
42         String JavaDoc _group = req.getParameter("group");
43
44         if ( ! LPS.configuration.optionAllows("connection-agent-ip", ip)) {
45             String JavaDoc err = "Forbidden connection agent host: " + ip;
46             replyWithXMLStatus(res, err, HttpServletResponse.SC_FORBIDDEN);
47             return;
48         }
49
50         ConnectionAgent agent = ConnectionAgent.getAgent(_url, false);
51         if (agent == null) {
52             replyWithXMLStatus(res, "Bad connection agent", SC_ERROR);
53             return;
54         }
55
56         ConnectionGroup group = ConnectionGroup.getGroup(_group);
57         if (group == null) {
58             replyWithXMLStatus(res, "Bad connection group", SC_ERROR);
59             return;
60         }
61
62         respondAgent(req, res, group);
63     }
64
65     protected void replyWithXMLStatus(HttpServletResponse res, String JavaDoc message)
66         throws IOException {
67         replyWithXML(res, message, null, HttpServletResponse.SC_OK);
68     }
69
70     protected void replyWithXMLStatus(HttpServletResponse res, String JavaDoc message, int sc)
71         throws IOException {
72         replyWithXML(res, message, null, sc);
73     }
74
75     protected void replyWithXML(HttpServletResponse res, String JavaDoc message, String JavaDoc xml)
76         throws IOException {
77         replyWithXML(res, message, xml, HttpServletResponse.SC_OK);
78     }
79
80     protected void replyWithXML(HttpServletResponse res, String JavaDoc message, String JavaDoc xml, int sc)
81         throws IOException {
82         ServletOutputStream out = res.getOutputStream();;
83         try {
84             res.setContentType ("text/xml");
85             out.println("<lps>");
86             out.println("<status>" + sc + "</status>");
87             out.println("<message>" + message + "</message>");
88             if (xml != null)
89                 out.println("<body>" + xml + "</body>");
90             out.println("</lps>");
91         } finally {
92             FileUtils.close(out);
93         }
94     }
95
96     public final int getMimeType()
97     {
98         return MIME_TYPE_XML;
99     }
100 }
101
Popular Tags