Commit b6e35290 authored by Alex Wenckus's avatar Alex Wenckus Committed by alex

Work on using get

git-svn-id: http://svn.igniterealtime.org/svn/repos/wildfire/trunk@7634 b35dd754-fafc-0310-a699-88a17e54d16e
parent ed652e86
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.lang;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
/**
* <p>Escapes and unescapes <code>String</code>s for
* Java, Java Script, HTML, XML, and SQL.</p>
*
* @author Apache Jakarta Turbine
* @author Purple Technology
* @author <a href="mailto:alex@purpletech.com">Alexander Day Chaffee</a>
* @author Antony Riley
* @author Helge Tesgaard
* @author <a href="sean@boohai.com">Sean Brown</a>
* @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
* @author Phil Steitz
* @author Pete Gieser
* @since 2.0
* @version $Id: StringEscapeUtils.java 471626 2006-11-06 04:02:09Z bayard $
*/
public class StringEscapeUtils {
/**
* <p><code>StringEscapeUtils</code> instances should NOT be constructed in
* standard programming.</p>
*
* <p>Instead, the class should be used as:
* <pre>StringEscapeUtils.escapeJava("foo");</pre></p>
*
* <p>This constructor is public to permit tools that require a JavaBean
* instance to operate.</p>
*/
public StringEscapeUtils() {
super();
}
/**
* <p>Escapes the characters in a <code>String</code> using JavaScript String rules.</p>
* <p>Escapes any values it finds into their JavaScript String form.
* Deals correctly with quotes and control-chars (tab, backslash, cr, ff, etc.) </p>
*
* <p>So a tab becomes the characters <code>'\\'</code> and
* <code>'t'</code>.</p>
*
* <p>The only difference between Java strings and JavaScript strings
* is that in JavaScript, a single quote must be escaped.</p>
*
* <p>Example:
* <pre>
* input string: He didn't say, "Stop!"
* output string: He didn\'t say, \"Stop!\"
* </pre>
* </p>
*
* @param str String to escape values in, may be null
* @return String with escaped values, <code>null</code> if null string input
*/
public static String escapeJavaScript(String str) {
return escapeJavaStyleString(str, true);
}
/**
* <p>Escapes the characters in a <code>String</code> using JavaScript String rules
* to a <code>Writer</code>.</p>
*
* <p>A <code>null</code> string input has no effect.</p>
*
* @see #escapeJavaScript(java.lang.String)
* @param out Writer to write escaped string into
* @param str String to escape values in, may be null
* @throws IllegalArgumentException if the Writer is <code>null</code>
* @throws IOException if error occurs on underlying Writer
**/
public static void escapeJavaScript(Writer out, String str) throws IOException {
escapeJavaStyleString(out, str, true);
}
/**
* <p>Worker method for the {@link #escapeJavaScript(String)} method.</p>
*
* @param str String to escape values in, may be null
* @param escapeSingleQuotes escapes single quotes if <code>true</code>
* @return the escaped string
*/
private static String escapeJavaStyleString(String str, boolean escapeSingleQuotes) {
if (str == null) {
return null;
}
try {
StringWriter writer = new StringWriter(str.length() * 2);
escapeJavaStyleString(writer, str, escapeSingleQuotes);
return writer.toString();
} catch (IOException ioe) {
// this should never ever happen while writing to a StringWriter
ioe.printStackTrace();
return null;
}
}
/**
* <p>Worker method for the {@link #escapeJavaScript(String)} method.</p>
*
* @param out write to receieve the escaped string
* @param str String to escape values in, may be null
* @param escapeSingleQuote escapes single quotes if <code>true</code>
* @throws IOException if an IOException occurs
*/
private static void escapeJavaStyleString(Writer out, String str, boolean escapeSingleQuote) throws IOException {
if (out == null) {
throw new IllegalArgumentException("The Writer must not be null");
}
if (str == null) {
return;
}
int sz;
sz = str.length();
for (int i = 0; i < sz; i++) {
char ch = str.charAt(i);
// handle unicode
if (ch > 0xfff) {
out.write("\\u" + hex(ch));
} else if (ch > 0xff) {
out.write("\\u0" + hex(ch));
} else if (ch > 0x7f) {
out.write("\\u00" + hex(ch));
} else if (ch < 32) {
switch (ch) {
case '\b':
out.write('\\');
out.write('b');
break;
case '\n':
out.write('\\');
out.write('n');
break;
case '\t':
out.write('\\');
out.write('t');
break;
case '\f':
out.write('\\');
out.write('f');
break;
case '\r':
out.write('\\');
out.write('r');
break;
default :
if (ch > 0xf) {
out.write("\\u00" + hex(ch));
} else {
out.write("\\u000" + hex(ch));
}
break;
}
} else {
switch (ch) {
case '\'':
if (escapeSingleQuote) {
out.write('\\');
}
out.write('\'');
break;
case '"':
out.write('\\');
out.write('"');
break;
case '\\':
out.write('\\');
out.write('\\');
break;
default :
out.write(ch);
break;
}
}
}
}
/**
* <p>Returns an upper case hexadecimal <code>String</code> for the given
* character.</p>
*
* @param ch The character to convert.
* @return An upper case hexadecimal <code>String</code>
*/
private static String hex(char ch) {
return Integer.toHexString(ch).toUpperCase();
}
}
/**
* $RCSfile$
* $Revision: $
* $Date: $
*
* Copyright (C) 2006 Jive Software. All rights reserved.
* Copyright (C) 2007 Jive Software. All rights reserved.
*
* This software is published under the terms of the GNU Public License (GPL),
* a copy of which is included in this distribution.
......@@ -22,6 +21,7 @@ import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.DocumentHelper;
import org.mortbay.util.ajax.ContinuationSupport;
import org.apache.commons.lang.StringEscapeUtils;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
......@@ -29,13 +29,16 @@ import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import javax.servlet.ServletConfig;
import java.io.IOException;
import java.io.InputStream;
import java.io.ByteArrayInputStream;
import java.net.InetAddress;
import java.net.URLDecoder;
/**
* Servlet which handles requests to the HTTP binding service. It determines if there is currently
* an {@link HttpSession} related to the connection or if one needs to be created and then passes
* it off to the {@link HttpBindManager} for processing of the client request and formulating of
* the response.
* an {@link HttpSession} related to the connection or if one needs to be created and then passes it
* off to the {@link HttpBindManager} for processing of the client request and formulating of the
* response.
*
* @author Alexander Wenckus
*/
......@@ -59,27 +62,56 @@ public class HttpBindServlet extends HttpServlet {
}
@Override public void init(ServletConfig servletConfig) throws ServletException {
@Override
public void init(ServletConfig servletConfig) throws ServletException {
super.init(servletConfig);
sessionManager = HttpBindManager.getInstance().getSessionManager();
sessionManager.start();
}
@Override public void destroy() {
@Override
public void destroy() {
super.destroy();
sessionManager.stop();
}
@Override protected void doPost(HttpServletRequest request, HttpServletResponse response)
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
if (isContinuation(request, response)) {
return;
}
String queryString = request.getQueryString();
if (queryString == null || "".equals(queryString)) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST,
"Unable to parse request content");
return;
}
queryString = URLDecoder.decode(queryString, "utf-8");
parseDocument(request, response, new ByteArrayInputStream(queryString.getBytes()));
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
if (isContinuation(request, response)) {
return;
}
parseDocument(request, response, request.getInputStream());
}
private void parseDocument(HttpServletRequest request, HttpServletResponse response,
InputStream documentContent)
throws IOException {
Document document;
try {
document = createDocument(request);
document = createDocument(documentContent);
}
catch (Exception e) {
Log.warn("Error parsing user request. [" + request.getRemoteAddr() + "]");
......@@ -115,12 +147,12 @@ public class HttpBindServlet extends HttpServlet {
}
synchronized (session) {
try {
respond(response, session.getResponse((Long) request.getAttribute("request"))
.getBytes("utf-8"));
respond(response, session.getResponse((Long) request.getAttribute("request")),
request.getMethod());
}
catch (HttpBindException e) {
response.sendError(e.getHttpError(), e.getMessage());
if(e.shouldCloseSession()) {
if (e.shouldCloseSession()) {
session.close();
}
}
......@@ -149,11 +181,11 @@ public class HttpBindServlet extends HttpServlet {
HttpConnection connection;
try {
connection = sessionManager.forwardRequest(rid, session,
request.isSecure(), rootNode);
request.isSecure(), rootNode);
}
catch (HttpBindException e) {
response.sendError(e.getHttpError(), e.getMessage());
if(e.shouldCloseSession()) {
if (e.shouldCloseSession()) {
session.close();
}
return;
......@@ -162,11 +194,11 @@ public class HttpBindServlet extends HttpServlet {
Log.error("Error sending packet to client.", nc);
return;
}
String type = rootNode.attributeValue("type");
if ("terminate".equals(type)) {
session.close();
respond(response, createEmptyBody().getBytes("utf-8"));
respond(response, createEmptyBody(), request.getMethod());
}
else {
connection
......@@ -174,8 +206,8 @@ public class HttpBindServlet extends HttpServlet {
request.setAttribute("request-session", connection.getSession());
request.setAttribute("request", connection.getRequestId());
try {
respond(response, session.getResponse(connection.getRequestId())
.getBytes("utf-8"));
respond(response, session.getResponse(connection.getRequestId()),
request.getMethod());
}
catch (HttpBindException e) {
response.sendError(e.getHttpError(), e.getMessage());
......@@ -201,7 +233,7 @@ public class HttpBindServlet extends HttpServlet {
HttpConnection connection = new HttpConnection(rid, request.isSecure());
InetAddress address = InetAddress.getByName(request.getRemoteAddr());
connection.setSession(sessionManager.createSession(address, rootNode, connection));
respond(response, connection);
respond(response, connection, request.getMethod());
}
catch (UnauthorizedException e) {
// Server wasn't initialized yet.
......@@ -214,27 +246,33 @@ public class HttpBindServlet extends HttpServlet {
}
private void respond(HttpServletResponse response, HttpConnection connection)
private void respond(HttpServletResponse response, HttpConnection connection, String method)
throws IOException
{
byte[] content;
String content;
try {
content = connection.getResponse().getBytes("utf-8");
content = connection.getResponse();
}
catch (HttpBindTimeoutException e) {
content = createEmptyBody().getBytes("utf-8");
content = createEmptyBody();
}
respond(response, content);
respond(response, content, method);
}
private void respond(HttpServletResponse response, byte [] content) throws IOException {
private void respond(HttpServletResponse response, String content, String method)
throws IOException {
response.setStatus(HttpServletResponse.SC_OK);
response.setContentType("text/xml");
response.setContentType("GET".equals(method) ? "text/javascript" : "text/xml");
response.setCharacterEncoding("utf-8");
response.setContentLength(content.length);
response.getOutputStream().write(content);
if ("GET".equals(method)) {
content = "_BOSH_(\"" + StringEscapeUtils.escapeJavaScript(content) + "\")";
}
byte[] byteContent = content.getBytes("utf-8");
response.setContentLength(byteContent.length);
response.getOutputStream().write(byteContent);
}
private static String createEmptyBody() {
......@@ -255,8 +293,7 @@ public class HttpBindServlet extends HttpServlet {
}
}
private Document createDocument(HttpServletRequest request) throws
DocumentException, IOException, XmlPullParserException {
private XMPPPacketReader getPacketReader() {
// Reader is associated with a new XMPPPacketReader
XMPPPacketReader reader = localReader.get();
if (reader == null) {
......@@ -264,6 +301,12 @@ public class HttpBindServlet extends HttpServlet {
reader.setXPPFactory(factory);
localReader.set(reader);
}
return reader.read("utf-8", request.getInputStream());
return reader;
}
private Document createDocument(InputStream request) throws
DocumentException, IOException, XmlPullParserException
{
return getPacketReader().read("utf-8", request);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment