001: /******************************************************************************
002: * ResponderConnectionAgent.java
003: * ****************************************************************************/package org.openlaszlo.servlets.responders;
004:
005: import java.io.*;
006: import java.util.*;
007: import java.net.*;
008: import javax.servlet.*;
009: import javax.servlet.http.*;
010: import org.openlaszlo.compiler.*;
011: import org.openlaszlo.connection.*;
012: import org.openlaszlo.server.*;
013: import org.openlaszlo.utils.*;
014: import org.apache.log4j.*;
015:
016: // Connection agents are configured in ResponderConnection
017: public abstract class ResponderConnectionAgent extends Responder {
018: private static Logger mLogger = Logger
019: .getLogger(ResponderConnectionAgent.class);
020:
021: public final static int SC_ERROR = 800;
022: public final static int SC_MISSING_PARAMETER = 801;
023:
024: protected abstract void respondAgent(HttpServletRequest req,
025: HttpServletResponse res, ConnectionGroup group)
026: throws IOException;
027:
028: protected void respondImpl(HttpServletRequest req,
029: HttpServletResponse res) throws IOException {
030: String ip = req.getRemoteAddr();
031: String _url = req.getParameter("url");
032: String _group = req.getParameter("group");
033:
034: if (!LPS.configuration.optionAllows("connection-agent-ip", ip)) {
035: String err =
036: /* (non-Javadoc)
037: * @i18n.test
038: * @org-mes="Forbidden connection agent host: " + p[0]
039: */
040: org.openlaszlo.i18n.LaszloMessages.getMessage(
041: ResponderConnectionAgent.class.getName(),
042: "051018-50", new Object[] { ip });
043: replyWithXMLStatus(res, err,
044: HttpServletResponse.SC_FORBIDDEN);
045: return;
046: }
047:
048: ConnectionAgent agent = ConnectionAgent.getAgent(_url, false);
049: if (agent == null) {
050: replyWithXMLStatus(res,
051: /* (non-Javadoc)
052: * @i18n.test
053: * @org-mes="Bad connection agent"
054: */
055: org.openlaszlo.i18n.LaszloMessages.getMessage(
056: ResponderConnectionAgent.class.getName(),
057: "051018-64"), SC_ERROR);
058: return;
059: }
060:
061: ConnectionGroup group = ConnectionGroup.getGroup(_group);
062: if (group == null) {
063: replyWithXMLStatus(res,
064: /* (non-Javadoc)
065: * @i18n.test
066: * @org-mes="Bad connection group"
067: */
068: org.openlaszlo.i18n.LaszloMessages.getMessage(
069: ResponderConnectionAgent.class.getName(),
070: "051018-77"), SC_ERROR);
071: return;
072: }
073:
074: respondAgent(req, res, group);
075: }
076:
077: protected void replyWithXMLStatus(HttpServletResponse res,
078: String message) throws IOException {
079: replyWithXML(res, message, null, HttpServletResponse.SC_OK);
080: }
081:
082: protected void replyWithXMLStatus(HttpServletResponse res,
083: String message, int sc) throws IOException {
084: replyWithXML(res, message, null, sc);
085: }
086:
087: protected void replyWithXML(HttpServletResponse res,
088: String message, String xml) throws IOException {
089: replyWithXML(res, message, xml, HttpServletResponse.SC_OK);
090: }
091:
092: protected void replyWithXML(HttpServletResponse res,
093: String message, String xml, int sc) throws IOException {
094: ServletOutputStream out = res.getOutputStream();
095: ;
096: try {
097: res.setContentType("text/xml");
098: out.println("<lps>");
099: out.println("<status>" + sc + "</status>");
100: out.println("<message>" + message + "</message>");
101: if (xml != null)
102: out.println("<body>" + xml + "</body>");
103: out.println("</lps>");
104: } finally {
105: FileUtils.close(out);
106: }
107: }
108:
109: public final int getMimeType() {
110: return MIME_TYPE_XML;
111: }
112: }
|