Example #1
0
  public JSRequest(Request request, Context cx, Scriptable scope) {
    this();

    ESXX esxx = ESXX.getInstance();

    this.request = request;

    requestURI = (JSURI) cx.newObject(scope, "URI", new Object[] {request.getRequestURI()});
    scriptURI = (JSURI) cx.newObject(scope, "URI", new Object[] {request.getScriptURI()});

    env = cx.newObject(scope);
    headers = cx.newObject(scope);
    cookies = cx.newObject(scope);
    accept = cx.newObject(scope);
    query = cx.newObject(scope);
    args = null;
    params = cx.newObject(scope);

    acceptValueOf = new FunctionObject("valueOf", acceptValueOfMethod, accept);

    for (String name : request.getProperties().stringPropertyNames()) {
      String value = request.getProperties().getProperty(name).trim();

      // Add environtment variable to esxx.env
      ScriptableObject.putProperty(env, name, value);

      // If this is an HTTP header, get the original name back
      String hdr = esxx.cgiToHTTP(name);

      if (hdr != null) {
        // Add real HTTP header to this.headers
        addHeader(hdr, value);

        // Decode cookies
        handleCookieHeader(hdr, value);

        // Decode Accept* HTTP headers
        handleAcceptHeader(hdr, value, cx, accept);

        // Decode Content-* HTTP headers
        handleContentHeader(hdr, value);

        // Handle SOAPAction
        if (hdr.equals("SOAPAction")) {
          soapAction = value;
        }
      }

      if (name.equals("QUERY_STRING")) {
        try {
          StringUtil.decodeFormVariables(value, query);
        } catch (UnsupportedEncodingException ex) {
          throw new ESXXException("Unable to parse request entity: " + ex.getMessage(), ex);
        }
      }
    }

    logger = JSESXX.newObject(cx, scope, "Logger", new Object[] {request, request.getScriptName()});
  }
Example #2
0
  private Object parseMessage() {
    // Consume SOAP message, if any
    // TODO: Add a SOAP handler in Parser.java
    if (soapAction != null) {
      try {
        MimeHeaders mime_headers = new MimeHeaders();

        for (Object k : headers.getIds()) {
          if (k instanceof String) {
            String name = (String) k;
            String value = Context.toString(ScriptableObject.getProperty(headers, name));

            mime_headers.addHeader(name, value);
          }
        }

        return MessageFactory.newInstance(SOAPConstants.DYNAMIC_SOAP_PROTOCOL)
            .createMessage(mime_headers, request.getInputStream());
      } catch (IOException ex) {
        throw new ESXXException("Unable to read SOAP message stream: " + ex.getMessage());
      } catch (SOAPException ex) {
        throw new ESXXException(400 /* Bad Request */, "Invalid SOAP message: " + ex.getMessage());
      } finally {
        try {
          request.getInputStream().close();
        } catch (Exception ignored) {
        }
      }
    } else if (contentType != null && contentLength > 0) {
      try {
        ESXX esxx = ESXX.getInstance();
        return esxx.parseStream(
            contentType,
            request.getInputStream(),
            URI.create("urn:x-esxx:incoming-request-entity"),
            null,
            new java.io.PrintWriter(request.getErrorWriter()),
            Context.getCurrentContext(),
            this);
      } catch (Exception ex) {
        throw new ESXXException(
            400 /* Bad Request */, "Unable to parse request entity: " + ex.getMessage(), ex);
      } finally {
        try {
          request.getInputStream().close();
        } catch (Exception ignored) {
        }
      }
    } else {
      // Return a dummy object
      return Context.getCurrentContext().newObject(this);
    }
  }
  public Integer handleError(ESXX esxx, Context cx, Throwable ex) {
    String title = "ESXX Server Error";
    int code = 500;

    if (ex instanceof ESXXException) {
      code = ((ESXXException) ex).getStatus();
    }

    StringWriter sw = new StringWriter();
    PrintWriter out = new PrintWriter(sw);

    out.println(esxx.getHTMLHeader());
    out.println("<h2>" + title + "</h2>");
    out.println("<h3>Unhandled exception: " + ex.getClass().getSimpleName() + "</h3>");
    if (ex instanceof ESXXException
        || ex instanceof javax.xml.stream.XMLStreamException
        || ex instanceof javax.xml.transform.TransformerException) {
      out.println("<p><tt>" + encodeXMLContent(ex.getMessage()) + "</tt></p>");
    } else if (ex instanceof RhinoException) {
      out.println("<pre>");
      out.println(ex.getClass().getSimpleName() + ": " + encodeXMLContent(ex.getMessage()));
      out.println(((RhinoException) ex).getScriptStackTrace(new ESXX.JSFilenameFilter()));
      out.println("</pre>");
    } else {
      out.println("<pre>");
      ex.printStackTrace(out);
      out.println("</pre>");
    }
    out.println(esxx.getHTMLFooter());
    out.close();

    try {
      return handleResponse(
          esxx, cx, new Response(code, "text/html; charset=UTF-8", sw.toString(), null));
    } catch (Exception ex2) {
      // Hmm
      return 20;
    }
  }