Example usage for java.io BufferedReader read

List of usage examples for java.io BufferedReader read

Introduction

In this page you can find the example usage for java.io BufferedReader read.

Prototype

public int read(char cbuf[], int off, int len) throws IOException 

Source Link

Document

Reads characters into a portion of an array.

Usage

From source file:org.mitre.mat.engineclient.MATCgiClient.java

protected JsonNode postHTTP(ArrayList<NameValuePair> pArrayList, HashMap<String, String> data)
        throws MATEngineClientException {

    // Serialize the document, send it to HTTP, deserialize.
    if (data != null) {
        int mapsize = data.size();

        Iterator keyValuePairs1 = data.entrySet().iterator();
        for (int i = 0; i < mapsize; i++) {
            Map.Entry entry = (Map.Entry) keyValuePairs1.next();
            Object key = entry.getKey();
            Object value = entry.getValue();
            pArrayList.add(new NameValuePair((String) key, (String) value));
        }//  ww w  . j  a v  a2 s.co m
    }

    NameValuePair[] pArray = (NameValuePair[]) pArrayList.toArray(new NameValuePair[1]);
    HttpClientParams params = new HttpClientParams();
    params.setContentCharset("utf-8");
    HttpClient client = new HttpClient(params);

    PostMethod method = new PostMethod(url);

    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(0, false));

    method.setRequestBody(pArray);

    String resString = null;

    try {
        // Execute the method.
        int statusCode = client.executeMethod(method);

        if (statusCode != HttpStatus.SC_OK) {
            throw new MATEngineClientException("HTTP method failed: " + method.getStatusLine());
        }
        BufferedReader b = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream(), "utf-8"));
        StringBuffer buf = new StringBuffer();
        int READ_LEN = 2048;
        char[] cbuf = new char[READ_LEN];

        while (true) {
            int chars = b.read(cbuf, 0, READ_LEN);
            if (chars < 0) {
                break;
            }
            // You may not read READ_LEN chars, but
            // that doesn't mean you're done.
            buf.append(cbuf, 0, chars);
        }
        resString = new String(buf);
    } catch (HttpException e) {
        throw new MATEngineClientException("Fatal protocol violation: " + e.getMessage());
    } catch (IOException e) {
        throw new MATEngineClientException("Fatal transport error: " + e.getMessage());
    } finally {
        // Release the connection.
        method.releaseConnection();
    }

    JsonNode responseObj = null;
    JsonFactory jsonFact = new JsonFactory();
    JsonNode jsonValues;

    JsonParser parser;
    try {
        parser = jsonFact.createJsonParser(new StringReader(resString));
        return new ObjectMapper().readTree(parser);
    } catch (org.codehaus.jackson.JsonParseException ex) {
        Logger.getLogger(MATCgiClient.class.getName()).log(Level.SEVERE, null, ex);
        throw new MATEngineClientException("Couldn't digest the following string as JSON: " + resString);
    } catch (IOException ex) {
        Logger.getLogger(MATCgiClient.class.getName()).log(Level.SEVERE, null, ex);
        throw new MATEngineClientException("Couldn't interpret response document: " + ex.getMessage());
    }
}

From source file:org.jabsorb.JSONRPCServlet.java

/**
 * Called when a JSON-RPC requests comes in.
 * Looks in the session for a JSONRPCBridge and if not found there,
 * uses the global bridge; then passes off the
 * JSON-PRC call to be handled by the JSONRPCBridge found.
 *
 * @param request servlet request from browser.
 * @param response servlet response to browser.
 *
 * @throws IOException if an IOException occurs during processing.
 *//* w  w  w .j ava2s  .  c  om*/
public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
    // Use protected method in case someone wants to override it
    JSONRPCBridge json_bridge = findBridge(request);

    // Decode using the charset in the request if it exists otherwise
    // use UTF-8 as this is what all browser implementations use.
    // The JSON-RPC-Java JavaScript client is ASCII clean so it
    // although here we can correctly handle data from other clients
    // that do not escape non ASCII data
    String charset = request.getCharacterEncoding();
    if (charset == null) {
        charset = "UTF-8";
    }

    BufferedReader in = new BufferedReader(new InputStreamReader(request.getInputStream(), charset));

    String receiveString = (String) request.getAttribute("_jabsorb_beenHere");

    // if JSON data is found in a special request attribute, it means
    // that a continuation was used and this request is being retried
    // as a consequence of a Jetty continuation
    // see http://blogs.webtide.com/gregw/2007/11/18/1195421880000.html
    if (receiveString == null) {
        // Read the request
        CharArrayWriter data = new CharArrayWriter();
        char buf[] = new char[buf_size];
        int ret;
        while ((ret = in.read(buf, 0, buf_size)) != -1) {
            data.write(buf, 0, ret);
        }
        receiveString = data.toString();
        int begin = receiveString.indexOf("{");
        int end = receiveString.lastIndexOf("}") + 1;
        receiveString = receiveString.substring(begin, end);

        // save the json-rpc data in a special request attribute, in case a jetty 
        // continuation exception (org.mortbay.jetty.RetryRequest) is thrown and this 
        // request is retried by the container
        request.setAttribute("_jabsorb_beenHere", receiveString);
    } else {
        log.debug("jetty continuation resumed...");
    }

    if (log.isDebugEnabled()) {
        log.debug("receive: " + receiveString);
        log.debug("receive: " + prettyPrintJson(receiveString));
    }

    // Process the request
    JSONObject json_req;
    JSONRPCResult json_res;
    try {
        json_req = new JSONObject(receiveString);
        json_res = json_bridge.call(new Object[] { request, response }, json_req);
    } catch (JSONException e) {
        log.error("can't parse call" + receiveString, e);
        json_res = new JSONRPCResult(JSONRPCResult.CODE_ERR_PARSE, null, JSONRPCResult.MSG_ERR_PARSE);
    }

    String sendString = json_res.toString();

    // dump the received string
    if (log.isDebugEnabled()) {
        log.debug("send: " + sendString);
        log.debug("send: " + prettyPrintJson(sendString));
    }

    // Write the response
    byte[] bout = sendString.getBytes("UTF-8");

    // handle gzipping of the response if it is turned on
    if (JSONRPCServlet.GZIP_THRESHOLD != -1) {
        // if the request header says that the browser can take gzip compressed output, then gzip the output
        // but only if the response is large enough to warrant it and if the resultant compressed output is
        // actually smaller.
        if (acceptsGzip(request)) {
            if (bout.length > JSONRPCServlet.GZIP_THRESHOLD) {
                byte[] gzippedOut = gzip(bout);
                log.debug(
                        "gzipping! original size =  " + bout.length + "  gzipped size = " + gzippedOut.length);

                // if gzip didn't actually help, abort
                if (bout.length <= gzippedOut.length) {
                    log.warn("gzipping resulted in a larger output size!  "
                            + "aborting (sending non-gzipped response)... "
                            + "you may want to increase the gzip threshold if this happens a lot!"
                            + " original size = " + bout.length + "  gzipped size = " + gzippedOut.length);
                } else {
                    // go with the gzipped output
                    bout = gzippedOut;
                    response.addHeader("Content-Encoding", "gzip");
                }
            } else {
                log.debug("not gzipping because size is " + bout.length + " (less than the GZIP_THRESHOLD of "
                        + JSONRPCServlet.GZIP_THRESHOLD + " bytes)");
            }
        } else {
            // this should be rare with modern user agents
            log.debug("not gzipping because user agent doesn't accept gzip encoding...");
        }
    }

    // Encode using UTF-8, although We are actually ASCII clean as
    // all unicode data is JSON escaped using backslash u. This is
    // less data efficient for foreign character sets but it is
    // needed to support naughty browsers such as Konqueror and Safari
    // which do not honour the charset set in the response
    response.setContentType("application/json;charset=utf-8");
    OutputStream out = response.getOutputStream();

    response.setIntHeader("Content-Length", bout.length);

    out.write(bout);
    out.flush();
    out.close();
}

From source file:org.jabsorb.ng.JSONRPCServlet.java

/**
 * Called when a JSON-RPC requests comes in. Looks in the session for a
 * JSONRPCBridge and if not found there, uses the global bridge; then passes
 * off the JSON-PRC call to be handled by the JSONRPCBridge found.
 * /*w  w  w  . j a va  2 s.  c  o  m*/
 * @param request
 *            servlet request from browser.
 * @param response
 *            servlet response to browser.
 * 
 * @throws IOException
 *             if an IOException occurs during processing.
 */
@Override
public void service(final HttpServletRequest request, final HttpServletResponse response) throws IOException {

    // Use protected method in case someone wants to override it
    JSONRPCBridge json_bridge = findBridge(request);

    // Decode using the charset in the request if it exists otherwise
    // use UTF-8 as this is what all browser implementations use.
    // The JSON-RPC-Java JavaScript client is ASCII clean so it
    // although here we can correctly handle data from other clients
    // that do not escape non ASCII data
    String charset = request.getCharacterEncoding();
    if (charset == null) {
        charset = "UTF-8";
    }

    BufferedReader in = new BufferedReader(new InputStreamReader(request.getInputStream(), charset));

    String receiveString = (String) request.getAttribute("_jabsorb_beenHere");

    // if JSON data is found in a special request attribute, it means
    // that a continuation was used and this request is being retried
    // as a consequence of a Jetty continuation
    // see http://blogs.webtide.com/gregw/2007/11/18/1195421880000.html
    if (receiveString == null) {
        // Read the request
        CharArrayWriter data = new CharArrayWriter();
        char buf[] = new char[buf_size];
        int ret;
        while ((ret = in.read(buf, 0, buf_size)) != -1) {
            data.write(buf, 0, ret);
        }
        receiveString = data.toString();

        // save the json-rpc data in a special request attribute, in case a
        // jetty
        // continuation exception (org.mortbay.jetty.RetryRequest) is thrown
        // and this
        // request is retried by the container
        request.setAttribute("_jabsorb_beenHere", receiveString);
    } else {
        log.debug("service", "jetty continuation resumed...");
    }

    if (log.isDebugEnabled()) {
        log.debug("service", "receive: " + receiveString);
        log.debug("service", "receive: " + prettyPrintJson(receiveString));
    }

    // Process the request
    JSONObject json_req;
    JSONRPCResult json_res;
    try {
        json_req = new JSONObject(receiveString);
        json_res = json_bridge.call(new Object[] { request, response }, json_req);
    } catch (JSONException e) {
        log.error("service", "can't parse call" + receiveString, e);
        json_res = new JSONRPCResult(JSONRPCResult.CODE_ERR_PARSE, null, JSONRPCResult.MSG_ERR_PARSE);
    }

    String sendString = json_res.toString();

    // dump the received string
    if (log.isDebugEnabled()) {
        log.debug("service", "send: " + sendString);
        log.debug("service", "send: " + prettyPrintJson(sendString));
    }

    // Write the response
    byte[] bout = sendString.getBytes("UTF-8");

    // handle gzipping of the response if it is turned on
    if (JSONRPCServlet.GZIP_THRESHOLD != -1) {
        // if the request header says that the browser can take gzip
        // compressed output, then gzip the output
        // but only if the response is large enough to warrant it and if the
        // resultant compressed output is
        // actually smaller.
        if (acceptsGzip(request)) {
            if (bout.length > JSONRPCServlet.GZIP_THRESHOLD) {
                byte[] gzippedOut = gzip(bout);
                log.debug("service",
                        "gzipping! original size =  " + bout.length + "  gzipped size = " + gzippedOut.length);

                // if gzip didn't actually help, abort
                if (bout.length <= gzippedOut.length) {
                    log.warning("service", "gzipping resulted in a larger output size!  "
                            + "aborting (sending non-gzipped response)... "
                            + "you may want to increase the gzip threshold if this happens a lot!"
                            + " original size = " + bout.length + "  gzipped size = " + gzippedOut.length);
                } else {
                    // go with the gzipped output
                    bout = gzippedOut;
                    response.addHeader("Content-Encoding", "gzip");
                }
            } else {
                log.debug("service", "not gzipping because size is " + bout.length
                        + " (less than the GZIP_THRESHOLD of " + JSONRPCServlet.GZIP_THRESHOLD + " bytes)");
            }
        } else {
            // this should be rare with modern user agents
            log.debug("service", "not gzipping because user agent doesn't accept gzip encoding...");
        }
    }

    // Encode using UTF-8, although We are actually ASCII clean as
    // all unicode data is JSON escaped using backslash u. This is
    // less data efficient for foreign character sets but it is
    // needed to support naughty browsers such as Konqueror and Safari
    // which do not honour the charset set in the response
    response.setContentType("application/json;charset=utf-8");
    OutputStream out = response.getOutputStream();

    response.setIntHeader("Content-Length", bout.length);

    out.write(bout);
    out.flush();
    out.close();
}

From source file:org.apache.hadoop.hbase.rest.Dispatcher.java

protected byte[] readInputBuffer(HttpServletRequest request) throws HBaseRestException {
    try {/*from   w w w.  ja  va2s  .  c om*/
        String resultant = "";
        BufferedReader r = request.getReader();

        int defaultmaxlength = 10 * 1024 * 1024;
        int maxLength = this.conf == null ? defaultmaxlength
                : this.conf.getInt("hbase.rest.input.limit", defaultmaxlength);
        int bufferLength = 640;

        // TODO make s maxLength and c size values in configuration
        if (!r.ready()) {
            Thread.sleep(1000); // If r is not ready wait 1 second
            if (!r.ready()) { // If r still is not ready something is wrong, return
                // blank.
                return new byte[0];
            }
        }
        char[] c;// 40 characters * sizeof(UTF16)
        while (true) {
            c = new char[bufferLength];
            int n = r.read(c, 0, bufferLength);
            if (n == -1)
                break;
            resultant += new String(c, 0, n);
            if (resultant.length() > maxLength) {
                resultant = resultant.substring(0, maxLength);
                break;
            }
        }
        return Bytes.toBytes(resultant.trim());
    } catch (Exception e) {
        throw new HBaseRestException(e);
    }
}

From source file:gate.corpora.DocumentContentImpl.java

/** Contruction from URL and offsets. */
public DocumentContentImpl(URL u, String encoding, Long start, Long end) throws IOException {

    int readLength = 0;
    char[] readBuffer = new char[INTERNAL_BUFFER_SIZE];

    BufferedReader uReader = null;
    InputStream uStream = null;/*from   w w  w. ja v  a 2s  . c  o m*/
    StringBuffer buf = new StringBuffer();

    long s = 0, e = Long.MAX_VALUE;
    if (start != null && end != null) {
        s = start.longValue();
        e = end.longValue();
    }

    try {
        URLConnection conn = u.openConnection();
        uStream = conn.getInputStream();

        if ("gzip".equals(conn.getContentEncoding())) {
            uStream = new GZIPInputStream(uStream);
        }

        if (encoding != null && !encoding.equalsIgnoreCase("")) {
            uReader = new BomStrippingInputStreamReader(uStream, encoding, INTERNAL_BUFFER_SIZE);
        } else {
            uReader = new BomStrippingInputStreamReader(uStream, INTERNAL_BUFFER_SIZE);
        }
        ;

        // 1. skip S characters
        uReader.skip(s);

        // 2. how many character shall I read?
        long toRead = e - s;

        // 3. read gtom source into buffer
        while (toRead > 0 && (readLength = uReader.read(readBuffer, 0, INTERNAL_BUFFER_SIZE)) != -1) {
            if (toRead < readLength) {
                //well, if toRead(long) is less than readLenght(int)
                //then there can be no overflow, so the cast is safe
                readLength = (int) toRead;
            }

            buf.append(readBuffer, 0, readLength);
            toRead -= readLength;
        }
    } finally {
        // 4.close reader
        IOUtils.closeQuietly(uReader);
        IOUtils.closeQuietly(uStream);
    }

    content = new String(buf);
    originalContent = content;
}

From source file:com.xmlcalabash.library.ApacheHttpRequest.java

public void readBodyContentPart(TreeWriter tree, BufferedReader reader, String contentType, int contentLength,
        String charset, String boundary) throws SaxonApiException, IOException {
    String content = null;/*from w ww .jav  a  2 s  . c  o m*/

    // FIXME: this must be wrong, contentLength is in bytes not characters for binary, right?
    if (contentLength >= 0) {
        char buf[] = new char[contentLength];
        int pos = 0;
        int left = contentLength;
        while (left > 0) {
            int len = reader.read(buf, pos, left);
            pos += len;
            left -= len;
        }

        String line = reader.readLine();
        while ("".equals(line)) {
            line = reader.readLine();
        }

        if (!boundary.equals(line)) {
            throw new UnsupportedOperationException(
                    "Expected boundary: " + line + " (expected " + boundary + ")");
        }
        content = new String(buf, 0, contentLength);
    } else {
        content = "";
        String line = reader.readLine();
        while (!boundary.equals(line)) {
            content += line + "\n";
            line = reader.readLine();
        }
    }

    if (xmlContentType(contentType)) {
        // Read it as XML
        StringReader sreader = new StringReader(content);
        SAXSource source = new SAXSource(new InputSource(sreader));
        DocumentBuilder builder = runtime.getProcessor().newDocumentBuilder();
        tree.addSubtree(builder.build(source));
    } else if (textContentType(contentType)) {
        // Read it as text
        tree.addText(content);
    } else {
        // Read it as binary
        byte bytes[] = content.getBytes(charset);
        tree.addText(Base64.encodeBytes(bytes));
    }
}

From source file:com.taobao.diamond.sdkapi.impl.DiamondSDKManagerImpl.java

/**
 * Response/*from  w w w  .jav a 2  s.co  m*/
 * 
 * @param httpMethod
 * @return
 */
String getContent(HttpMethod httpMethod) throws UnsupportedEncodingException {
    StringBuilder contentBuilder = new StringBuilder();
    if (isZipContent(httpMethod)) {
        // 
        InputStream is = null;
        GZIPInputStream gzin = null;
        InputStreamReader isr = null;
        BufferedReader br = null;
        try {
            is = httpMethod.getResponseBodyAsStream();
            gzin = new GZIPInputStream(is);
            isr = new InputStreamReader(gzin, ((HttpMethodBase) httpMethod).getResponseCharSet()); // 
            br = new BufferedReader(isr);
            char[] buffer = new char[4096];
            int readlen = -1;
            while ((readlen = br.read(buffer, 0, 4096)) != -1) {
                contentBuilder.append(buffer, 0, readlen);
            }
        } catch (Exception e) {
            log.error("", e);
        } finally {
            try {
                br.close();
            } catch (Exception e1) {
                // ignore
            }
            try {
                isr.close();
            } catch (Exception e1) {
                // ignore
            }
            try {
                gzin.close();
            } catch (Exception e1) {
                // ignore
            }
            try {
                is.close();
            } catch (Exception e1) {
                // ignore
            }
        }
    } else {
        // 
        String content = null;
        try {
            content = httpMethod.getResponseBodyAsString();
        } catch (Exception e) {
            log.error("", e);
        }
        if (null == content) {
            return null;
        }
        contentBuilder.append(content);
    }
    return StringEscapeUtils.unescapeHtml(contentBuilder.toString());
}

From source file:cn.leancloud.diamond.sdkapi.impl.DiamondSDKManagerImpl.java

/**
 * ?Response??/*from   w ww .ja v a  2  s  .  c om*/
 * 
 * @param httpMethod
 * @return
 */
String getContent(HttpMethod httpMethod) throws UnsupportedEncodingException {
    StringBuilder contentBuilder = new StringBuilder();
    if (isZipContent(httpMethod)) {
        // ???
        InputStream is = null;
        GZIPInputStream gzin = null;
        InputStreamReader isr = null;
        BufferedReader br = null;
        try {
            is = httpMethod.getResponseBodyAsStream();
            gzin = new GZIPInputStream(is);
            isr = new InputStreamReader(gzin, ((HttpMethodBase) httpMethod).getResponseCharSet()); // ?????
            br = new BufferedReader(isr);
            char[] buffer = new char[4096];
            int readlen = -1;
            while ((readlen = br.read(buffer, 0, 4096)) != -1) {
                contentBuilder.append(buffer, 0, readlen);
            }
        } catch (Exception e) {
            log.error("", e);
        } finally {
            try {
                br.close();
            } catch (Exception e1) {
                // ignore
            }
            try {
                isr.close();
            } catch (Exception e1) {
                // ignore
            }
            try {
                gzin.close();
            } catch (Exception e1) {
                // ignore
            }
            try {
                is.close();
            } catch (Exception e1) {
                // ignore
            }
        }
    } else {
        // ???
        String content = null;
        try {
            content = httpMethod.getResponseBodyAsString();
        } catch (Exception e) {
            log.error("???", e);
        }
        if (null == content) {
            return null;
        }
        contentBuilder.append(content);
    }
    return StringEscapeUtils.unescapeHtml(contentBuilder.toString());
}

From source file:com.basetechnology.s0.agentserver.appserver.HandleGet.java

public boolean handleGet() throws IOException, ServletException, AgentAppServerException, AgentServerException,
        InterruptedException, SymbolException, JSONException {
    // Extract out commonly used info
    String path = httpInfo.path;/*w  ww  . j a v a2 s  .co  m*/
    String[] pathParts = httpInfo.pathParts;
    Request request = httpInfo.request;
    HttpServletResponse response = httpInfo.response;
    AgentServer agentServer = httpInfo.agentServer;
    String lcPath = path.toLowerCase();

    if (path.equalsIgnoreCase("/about")) {
        log.info("Getting about info");
        response.setContentType("application/json; charset=utf-8");
        response.setStatus(HttpServletResponse.SC_OK);
        JSONObject aboutJson = new JsonListMap();
        aboutJson.put("name", agentServer.config.get("name"));
        aboutJson.put("software", agentServer.config.get("software"));
        aboutJson.put("version", agentServer.config.get("version"));
        aboutJson.put("description", agentServer.config.get("description"));
        aboutJson.put("website", agentServer.config.get("website"));
        aboutJson.put("contact", agentServer.config.get("contact"));
        setOutput(aboutJson);
    } else if (path.equalsIgnoreCase("/evaluate")) {
        try {
            BufferedReader reader = request.getReader();
            String expressionString = null;
            try {
                StringBuilder builder = new StringBuilder();
                char[] buffer = new char[8192];
                int read;
                while ((read = reader.read(buffer, 0, buffer.length)) > 0) {
                    builder.append(buffer, 0, read);
                }
                expressionString = builder.toString();
            } catch (Exception e) {
                log.info("Exception reading expression text : " + e);
            }

            log.info("Evaluating expression: " + expressionString);
            AgentDefinition dummyAgentDefinition = new AgentDefinition(agentServer);
            AgentInstance dummyAgentInstance = new AgentInstance(dummyAgentDefinition);
            ScriptParser parser = new ScriptParser(dummyAgentInstance);
            ScriptRuntime scriptRuntime = new ScriptRuntime(dummyAgentInstance);
            ExpressionNode expressionNode = parser.parseExpressionString(expressionString);
            Value valueNode = scriptRuntime.evaluateExpression(expressionString, expressionNode);
            String resultString = valueNode.getStringValue();

            response.setContentType("text/plain; charset=utf-8");
            response.setStatus(HttpServletResponse.SC_OK);
            response.getWriter().println(resultString);
        } catch (Exception e) {
            log.info("Evaluate Exception: " + e);
        }
        ((Request) request).setHandled(true);
    } else if (path.equalsIgnoreCase("/run")) {
        try {
            BufferedReader reader = request.getReader();
            String scriptString = null;
            try {
                StringBuilder builder = new StringBuilder();
                char[] buffer = new char[8192];
                int read;
                while ((read = reader.read(buffer, 0, buffer.length)) > 0) {
                    builder.append(buffer, 0, read);
                }
                scriptString = builder.toString();
            } catch (Exception e) {
                log.info("Exception reading script text : " + e);
            }

            log.info("Running script: " + scriptString);
            AgentDefinition dummyAgentDefinition = new AgentDefinition(agentServer);
            AgentInstance dummyAgentInstance = new AgentInstance(dummyAgentDefinition);
            ScriptParser parser = new ScriptParser(dummyAgentInstance);
            ScriptRuntime scriptRuntime = new ScriptRuntime(dummyAgentInstance);
            ScriptNode scriptNode = parser.parseScriptString(scriptString);
            Value valueNode = scriptRuntime.runScript(scriptString, scriptNode);
            String resultString = valueNode.getStringValue();
            log.info("Script result: " + resultString);

            response.setContentType("text/plain; charset=utf-8");
            response.setStatus(HttpServletResponse.SC_OK);
            response.getWriter().println(resultString);
        } catch (Exception e) {
            log.info("Run Exception: " + e);
        }
        ((Request) request).setHandled(true);
    } else if (path.equalsIgnoreCase("/status")) {
        log.info("Getting status info");
        response.setContentType("application/json; charset=utf-8");
        response.setStatus(HttpServletResponse.SC_OK);

        // Sleep a little to assure status reflects any recent operation
        Thread.sleep(100);

        // Get the status info
        JSONObject aboutJson = new JsonListMap();
        AgentScheduler agentScheduler = AgentScheduler.singleton;
        aboutJson.put("status", agentScheduler == null ? "shutdown" : agentScheduler.getStatus());
        aboutJson.put("since", DateUtils.toRfcString(agentServer.startTime));
        aboutJson.put("num_registered_users", agentServer.users.size());
        int numActiveUsers = 0;
        for (NameValue<AgentInstanceList> agentInstanceListNameValue : agentServer.agentInstances)
            if (agentInstanceListNameValue.value.size() > 0)
                numActiveUsers++;
        aboutJson.put("num_active_users", numActiveUsers);
        int num_registered_agents = 0;
        for (NameValue<AgentDefinitionList> agentDefinitionListNameValue : agentServer.agentDefinitions)
            num_registered_agents += agentDefinitionListNameValue.value.size();
        aboutJson.put("num_registered_agents", num_registered_agents);
        int num_active_agents = 0;
        for (NameValue<AgentInstanceList> agentInstanceListNameValue : agentServer.agentInstances)
            num_active_agents += agentInstanceListNameValue.value.size();
        aboutJson.put("num_active_agents", num_active_agents);
        response.getWriter().println(aboutJson.toString(4));
    } else if (path.equalsIgnoreCase("/config")) {
        log.info("Getting configuration settings");
        response.setContentType("application/json; charset=utf-8");
        response.setStatus(HttpServletResponse.SC_OK);

        // Get the config info
        JSONObject configJson = agentServer.config.toJson();

        response.getWriter().println(configJson.toString(4));
    } else if (path.equalsIgnoreCase("/agent_definitions")) {
        checkAdminAccess();
        log.info("Getting list of agent definitions");
        JSONArray agentDefinitionsArrayJson = new JSONArray();
        // Get all agents for all users
        for (NameValue<AgentDefinitionList> userAgentDefinitions : agentServer.agentDefinitions) {
            // Get all agents for this user
            for (AgentDefinition agentDefinition : agentServer.agentDefinitions
                    .get(userAgentDefinitions.name)) {
                // Generate JSON for short summary of agent definition
                JSONObject agentDefinitionJson = new JsonListMap();
                agentDefinitionJson.put("user", agentDefinition.user.id);
                agentDefinitionJson.put("name", agentDefinition.name);
                agentDefinitionJson.put("description", agentDefinition.description);
                agentDefinitionsArrayJson.put(agentDefinitionJson);
            }
        }
        JSONObject agentDefinitionsJson = new JSONObject();
        agentDefinitionsJson.put("agent_definitions", agentDefinitionsArrayJson);
        setOutput(agentDefinitionsJson);
    } else if (path.equalsIgnoreCase("/agents")) {
        checkAdminAccess();
        log.info("Getting list of agent instances for all users");
        JSONArray agentInstancesArrayJson = new JSONArray();
        // Get all agents for all users
        for (NameValue<AgentInstanceList> userAgentInstances : agentServer.agentInstances) {
            // Get all agents for this user
            for (AgentInstance agentInstance : agentServer.agentInstances.get(userAgentInstances.name)) {
                // Generate JSON for short summary of agent instance
                JSONObject agentInstanceJson = new JsonListMap();
                agentInstanceJson.put("user", agentInstance.user.id);
                agentInstanceJson.put("name", agentInstance.name);
                agentInstanceJson.put("definition", agentInstance.agentDefinition.name);
                agentInstanceJson.put("description", agentInstance.description);
                agentInstancesArrayJson.put(agentInstanceJson);
            }
        }
        JSONObject agentInstancesJson = new JSONObject();
        agentInstancesJson.put("agent_instances", agentInstancesArrayJson);
        setOutput(agentInstancesJson);
    } else if (path.equalsIgnoreCase("/field_types")) {
        try {
            log.info("Getting list of field types");
            response.setContentType("application/json; charset=utf-8");
            response.setStatus(HttpServletResponse.SC_OK);
            JSONArray fieldTypesArrayJson = new JSONArray();
            for (String fieldType : Field.types)
                fieldTypesArrayJson.put(fieldType);
            JSONObject fieldTypesJson = new JSONObject();
            fieldTypesJson.put("field_types", fieldTypesArrayJson);
            response.getWriter().println(fieldTypesJson.toString(4));
        } catch (JSONException e) {
            throw new AgentServerException("JSON error generating JSON for agent definition status - " + e);
        }
        return true;
    } else if (path.equalsIgnoreCase("/usage")) {
        log.info("Getting API usage summary text");

        // Get text of API usage summary, api_usage.txt
        // TODO: Where to read the file from... ./doc or???
        String apiUsageText = FileUtils.readFileToString(new File("./doc/api_usage.txt"));

        // Return the text
        response.setContentType("application/json; charset=utf-8");
        response.setStatus(HttpServletResponse.SC_OK);
        response.getWriter().println(apiUsageText);
    } else if (path.equalsIgnoreCase("/users")) {
        checkAdminAccess();
        log.info("Getting list of all user ids");
        JSONArray usersArrayJson = new JSONArray();
        for (NameValue<User> userIdValue : agentServer.users) {
            User user = userIdValue.value;
            JSONObject userJson = new JSONObject();
            userJson.put("id", user.id);
            userJson.put("display_name",
                    user.incognito ? "(Incognito)" : (user.displayName == null ? "" : user.displayName));
            usersArrayJson.put(userJson);
        }
        JSONObject usersJson = new JSONObject();
        usersJson.put("users", usersArrayJson);
        setOutput(usersJson);
    } else if (lcPath.matches("^/users/[a-zA-Z0-9_.@\\-]*$")) {
        User user = checkUserAccess(false);
        log.info("Getting detailed info for a specified user Id: " + user.id);
        setOutput(user.toJson());
    } else if (lcPath.matches("^/users/[a-zA-Z0-9_.@\\-]*/agent_definitions$")) {
        User user = checkUserAccess(false);
        log.info("Getting list of all agent definitions for user Id: " + user.id);

        // Get all agents for this user
        JSONArray agentDefinitionsArrayJson = new JSONArray();
        for (AgentDefinition agentDefinition : agentServer.agentDefinitions.get(user.id)) {
            // Generate JSON for short summary of agent definition
            JSONObject agentDefinitionJson = new JsonListMap();
            agentDefinitionJson.put("user", agentDefinition.user.id);
            agentDefinitionJson.put("name", agentDefinition.name);
            agentDefinitionJson.put("description", agentDefinition.description);
            agentDefinitionsArrayJson.put(agentDefinitionJson);
        }
        JSONObject agentDefinitionsJson = new JSONObject();
        agentDefinitionsJson.put("agent_definitions", agentDefinitionsArrayJson);
        setOutput(agentDefinitionsJson);
    } else if (lcPath.matches("^/users/[a-zA-Z0-9_.@\\-]*/agent_definitions/[a-zA-Z0-9_.@\\-]*$")) {
        User user = checkUserAccess(false);
        String agentName = pathParts[4];
        if (agentName == null)
            throw new AgentAppServerBadRequestException("Missing agent definition name path parameter");
        if (agentName.trim().length() == 0)
            throw new AgentAppServerBadRequestException("Empty agent definition name path parameter");
        if (!agentServer.agentDefinitions.get(user.id).containsKey(agentName))
            throw new AgentAppServerException(HttpServletResponse.SC_NOT_FOUND,
                    "No agent definition with that name for that user");

        log.info("Getting definition for agent definition " + agentName + " for user: " + user.id);
        AgentDefinitionList agentMap = agentServer.agentDefinitions.get(user.id);
        AgentDefinition agentDefinition = agentMap.get(agentName);
        setOutput(agentDefinition.toJson());
    } else if (lcPath.matches("^/users/[a-zA-Z0-9_.@\\-]*/agent_definitions/[a-zA-Z0-9_.@\\-]*/status$")) {
        User user = checkUserAccess(false);
        String agentName = pathParts[4];

        if (agentName == null)
            throw new AgentAppServerBadRequestException("Missing agent definition name path parameter");
        if (agentName.trim().length() == 0)
            throw new AgentAppServerBadRequestException("Empty agent definition name path parameter");
        if (!agentServer.agentDefinitions.get(user.id).containsKey(agentName))
            throw new AgentAppServerException(HttpServletResponse.SC_NOT_FOUND,
                    "No agent definition with that name for that user");

        log.info("Getting status for agent definition " + agentName + " for user: " + user.id);
        AgentDefinitionList agentMap = agentServer.agentDefinitions.get(user.id);
        AgentDefinition agent = agentMap.get(agentName);
        JSONObject statusJson = new JSONObject();
        statusJson.put("user_id", user.id);
        statusJson.put("name", agent.name);
        statusJson.put("created", DateUtils.toRfcString(agent.timeCreated));
        statusJson.put("modified", DateUtils.toRfcString(agent.timeModified));
        int numActiveInstances = 0;
        for (NameValue<AgentInstanceList> agentInstanceListNameValue : agentServer.agentInstances)
            for (AgentInstance agentInstance : agentInstanceListNameValue.value)
                if (agentInstance.agentDefinition == agent)
                    numActiveInstances++;
        statusJson.put("num_active_instances", numActiveInstances);
        setOutput(statusJson);
    } else if (lcPath.matches("^/users/[a-zA-Z0-9_.@\\-]*/agents$")) {
        User user = checkUserAccess(false);
        log.info("Getting list of all agent instances for a user");

        // Get all agents for this user
        JSONArray agentInstancesArrayJson = new JSONArray();
        for (AgentInstance agentInstance : agentServer.agentInstances.get(user.id)) {
            // Generate JSON for short summary of agent instance
            JSONObject agentInstanceJson = new JsonListMap();
            agentInstanceJson.put("user", agentInstance.user.id);
            agentInstanceJson.put("name", agentInstance.name);
            agentInstanceJson.put("definition", agentInstance.agentDefinition.name);
            // TODO: Add the SHA for this instance
            agentInstanceJson.put("description", agentInstance.description);
            agentInstancesArrayJson.put(agentInstanceJson);
        }
        JSONObject agentInstancesJson = new JSONObject();
        agentInstancesJson.put("agent_instances", agentInstancesArrayJson);
        setOutput(agentInstancesJson);
    } else if (lcPath.matches("^/users/[a-zA-Z0-9_.@\\-]*/agents/[a-zA-Z0-9_.@\\-]*$")) {
        User user = checkUserAccess(false);
        String agentName = pathParts[4];
        String stateString = request.getParameter("state");
        boolean includeState = stateString != null && (stateString.equalsIgnoreCase("true")
                || stateString.equalsIgnoreCase("yes") || stateString.equalsIgnoreCase("on"));
        String countString = request.getParameter("count");
        int count = -1;
        if (countString != null && countString.trim().length() > 0)
            count = Integer.parseInt(countString);

        if (agentName == null)
            throw new AgentAppServerBadRequestException("Missing agent instance name path parameter");
        if (agentName.trim().length() == 0)
            throw new AgentAppServerBadRequestException("Empty agent instance name path parameter");
        if (!agentServer.agentInstances.get(user.id).containsKey(agentName))
            throw new AgentAppServerException(HttpServletResponse.SC_NOT_FOUND,
                    "No agent instance with that name for that user");

        log.info("Getting detail info for agent instance " + agentName + " for user: " + user.id);
        AgentInstance agentInstance = agentServer.agentInstances.get(user.id).get(agentName);

        setOutput(agentInstance.toJson(includeState, count));
    } else if (lcPath.matches("^/users/[a-zA-Z0-9_.@\\-]*/agents/[a-zA-Z0-9_.@\\-]*/notifications$")) {
        User user = checkUserAccess(false);
        String agentName = pathParts[4];

        if (agentName == null)
            throw new AgentAppServerBadRequestException("Missing agent instance name path parameter");
        if (agentName.trim().length() == 0)
            throw new AgentAppServerBadRequestException("Empty agent instance name path parameter");
        if (!agentServer.agentInstances.get(user.id).containsKey(agentName))
            throw new AgentAppServerException(HttpServletResponse.SC_NOT_FOUND,
                    "No agent instance with that name for that user");

        log.info("Getting current pending notification for agent instance " + agentName + " for user: "
                + user.id);
        AgentInstanceList agentMap = agentServer.agentInstances.get(user.id);
        AgentInstance agent = agentMap.get(agentName);

        // Build a JSON array of all pending notifications for agent
        JSONArray pendingNotificationsJson = new JSONArray();
        for (String notificationName : agent.notifications) {
            NotificationInstance notificationInstance = agent.notifications.get(notificationName);
            if (notificationInstance.pending) {
                // Generate and return a summary of the notification
                JSONObject notificationSummaryJson = new JsonListMap();
                notificationSummaryJson.put("agent", agent.name);
                notificationSummaryJson.put("name", notificationInstance.definition.name);
                notificationSummaryJson.put("description", notificationInstance.definition.description);
                notificationSummaryJson.put("details", notificationInstance.details.toJsonObject());
                notificationSummaryJson.put("type", notificationInstance.definition.type);
                notificationSummaryJson.put("time", DateUtils.toRfcString(notificationInstance.timeNotified));
                notificationSummaryJson.put("timeout", notificationInstance.timeout);
                pendingNotificationsJson.put(notificationSummaryJson);
            }
        }

        // Build a wrapper object for the array
        JSONObject wrapperJson = new JSONObject();
        wrapperJson.put("pending_notifications", pendingNotificationsJson);

        // Return the wrapped list
        setOutput(wrapperJson);
    } else if (lcPath.matches(
            "^/users/[a-zA-Z0-9_.@\\-]*/agents/[a-zA-Z0-9_.@\\-]*/notifications/[a-zA-Z0-9_.@\\-]*$")) {
        User user = checkUserAccess(false);
        String agentName = pathParts[4];
        // TODO: Maybe if path ends with "/", should be treated as GET of list of pending notifications
        String notificationName = pathParts.length >= 7 ? pathParts[6] : null;
        String responseParam = request.getParameter("response");
        String responseChoice = request.getParameter("response_choice");
        String comment = request.getParameter("comment");

        if (agentName == null)
            throw new AgentAppServerBadRequestException("Missing agent instance name path parameter");
        if (agentName.trim().length() == 0)
            throw new AgentAppServerBadRequestException("Empty agent instance name path parameter");
        if (!agentServer.agentInstances.get(user.id).containsKey(agentName))
            throw new AgentAppServerException(HttpServletResponse.SC_NOT_FOUND,
                    "No agent instance with that name for that user");
        if (notificationName == null)
            throw new AgentAppServerBadRequestException("Missing notification name path parameter");
        if (notificationName.trim().length() == 0)
            throw new AgentAppServerBadRequestException("Empty notification name path parameter");

        // Access the named notification
        AgentInstanceList agentMap = agentServer.agentInstances.get(user.id);
        AgentInstance agent = agentMap.get(agentName);
        NotificationInstance notificationInstance = agent.notifications.get(notificationName);
        if (notificationInstance == null)
            throw new AgentAppServerBadRequestException(
                    "Undefined notification name for agent instance '" + agentName + "': " + notificationName);

        // If no response, simply return info about the notification
        if (responseParam == null) {
            // Generate and return a summary of the notification
            JSONObject notificationSummaryJson = new JsonListMap();
            notificationSummaryJson.put("agent", agent.name);
            notificationSummaryJson.put("name", notificationInstance.definition.name);
            notificationSummaryJson.put("description", notificationInstance.definition.description);
            notificationSummaryJson.put("details", notificationInstance.details.toJsonObject());
            notificationSummaryJson.put("type", notificationInstance.definition.type);
            notificationSummaryJson.put("time", DateUtils.toRfcString(notificationInstance.timeNotified));
            notificationSummaryJson.put("timeout", notificationInstance.timeout);
            setOutput(notificationSummaryJson);
        } else {
            if (responseParam.trim().length() == 0)
                throw new AgentAppServerBadRequestException("Empty response query parameter");
            if (!NotificationInstance.responses.contains(responseParam))
                throw new AgentAppServerBadRequestException("Unknown response keyword query parameter");
            if (!notificationInstance.pending)
                throw new AgentAppServerBadRequestException(
                        "Cannot respond to notification '" + notificationName + "' for agent instance '"
                                + agentName + "' since it is not pending");

            log.info("Respond to a pending notification '" + notificationName + "' for agent instance "
                    + agentName + " for user: " + user.id);

            agent.respondToNotification(notificationInstance, responseParam, responseChoice, comment);

            // Done
            response.setStatus(HttpServletResponse.SC_NO_CONTENT);
        }
    } else if (lcPath.matches("^/users/[a-zA-Z0-9_.@\\-]*/agents/[a-zA-Z0-9_.@\\-]*/output$")) {
        String userId = pathParts[2];
        String agentName = pathParts[4];

        if (userId == null)
            throw new AgentAppServerBadRequestException("Missing user Id path parameter");
        if (userId.trim().length() == 0)
            throw new AgentAppServerBadRequestException("Empty user Id path parameter");
        if (!agentServer.users.containsKey(userId))
            throw new AgentAppServerBadRequestException("Unknown user name or invalid password");
        if (agentName == null)
            throw new AgentAppServerBadRequestException("Missing agent instance name path parameter");
        if (agentName.trim().length() == 0)
            throw new AgentAppServerBadRequestException("Empty agent instance name path parameter");
        if (!agentServer.agentInstances.get(userId).containsKey(agentName))
            throw new AgentAppServerException(HttpServletResponse.SC_NOT_FOUND,
                    "No agent instance with that name for that user");

        // Password not required for "public output" instances
        AgentInstance agent = agentServer.agentInstances.get(userId).get(agentName);
        User user = null;
        if (!agent.publicOutput)
            user = checkUserAccess(false);

        log.info("Getting output for agent instance " + agentName + " for user: " + userId);

        // Build a JSON object equivalent to map of output fields
        JSONObject outputJson = new JsonListMap();
        SymbolValues outputValues = agent.categorySymbolValues.get("outputs");
        for (Symbol outputSymbol : outputValues) {
            String fieldName = outputSymbol.name;
            outputJson.put(fieldName, agent.getOutput(fieldName).toJsonObject());
        }

        setOutput(outputJson);
    } else if (lcPath.matches("^/users/[a-zA-Z0-9_.@\\-]*/agents/[a-zA-Z0-9_.@\\-]*/output_history$")) {
        User user = checkUserAccess(false);
        String agentName = pathParts[4];
        String countString = request.getParameter("count");
        int count = countString == null ? -1 : Integer.parseInt(countString);

        if (agentName == null)
            throw new AgentAppServerBadRequestException("Missing agent instance name path parameter");
        if (agentName.trim().length() == 0)
            throw new AgentAppServerBadRequestException("Empty agent instance name path parameter");
        if (!agentServer.agentInstances.get(user.id).containsKey(agentName))
            throw new AgentAppServerException(HttpServletResponse.SC_NOT_FOUND,
                    "No agent instance with that name for that user");

        log.info("Getting output history for agent instance " + agentName + " for user: " + user.id);
        AgentInstanceList agentMap = agentServer.agentInstances.get(user.id);
        AgentInstance agent = agentMap.get(agentName);

        // Limit or default the user's specified count
        if (count <= 0)
            count = agent.defaultOutputCount;
        if (count > agent.outputLimit)
            count = agent.outputLimit;
        int outputSize = agent.outputHistory.size();
        if (count > outputSize)
            count = outputSize;

        // Compute starting history index
        int start = outputSize - count;

        int n = agent.outputHistory.size();
        if (n > 4) {
            SymbolValues s1 = agent.outputHistory.get(n - 2).output;
            SymbolValues s2 = agent.outputHistory.get(n - 1).output;
            boolean eq = s1.equals(s2);
        }

        // Build a JSON array of output rows
        JSONArray outputJson = new JSONArray();
        for (int i = start; i < outputSize; i++) {
            OutputRecord outputs = agent.outputHistory.get(i);
            outputJson.put(outputs.output.toJson());
        }

        // Wrap the array in an object since that is what output code expects
        JSONObject outputHistory = new JsonListMap();
        outputHistory.put("output_history", outputJson);
        setOutput(outputHistory);
    } else if (lcPath.matches("^/users/[a-zA-Z0-9_.@\\-]*/agents/[a-zA-Z0-9_.@\\-]*/state$")) {
        User user = checkUserAccess(false);
        String agentName = pathParts[4];
        String countString = request.getParameter("count");

        if (agentName == null)
            throw new AgentAppServerBadRequestException("Missing agent instance name path parameter");
        if (agentName.trim().length() == 0)
            throw new AgentAppServerBadRequestException("Empty agent instance name path parameter");
        if (!agentServer.agentInstances.get(user.id).containsKey(agentName))
            throw new AgentAppServerException(HttpServletResponse.SC_NOT_FOUND,
                    "No agent instance with that name for that user");

        int count = -1;
        if (countString != null && countString.trim().length() > 0)
            count = Integer.parseInt(countString);

        log.info(
                "Getting full state and detail info for agent instance " + agentName + " for user: " + user.id);
        AgentInstanceList agentMap = agentServer.agentInstances.get(user.id);
        AgentInstance agentInstance = agentMap.get(agentName);
        setOutput(agentInstance.toJson(true, count));
    } else if (lcPath.matches("^/users/[a-zA-Z0-9_.@\\-]*/agents/[a-zA-Z0-9_.@\\-]*/status$")) {
        User user = checkUserAccess(false);
        String agentName = pathParts[4];
        String stateString = request.getParameter("state");
        boolean includeState = stateString != null && (stateString.equalsIgnoreCase("true")
                || stateString.equalsIgnoreCase("yes") || stateString.equalsIgnoreCase("on"));
        String countString = request.getParameter("count");
        int count = -1;
        if (countString != null && countString.trim().length() > 0)
            count = Integer.parseInt(countString);

        if (agentName == null)
            throw new AgentAppServerBadRequestException("Missing agent instance name path parameter");
        if (agentName.trim().length() == 0)
            throw new AgentAppServerBadRequestException("Empty agent instance name path parameter");
        if (!agentServer.agentInstances.get(user.id).containsKey(agentName))
            throw new AgentAppServerException(HttpServletResponse.SC_NOT_FOUND,
                    "No agent instance with that name for that user");

        log.info("Getting status for agent instance " + agentName + " for user: " + user.id);
        /*      AgentInstanceList agentMap = agentServer.agentInstances.get(user.id);
              AgentInstance agent = agentMap.get(agentName);
              JSONObject statusJson = new JsonListMap();
              statusJson.put("user", user.id);
              statusJson.put("name", agent.name);
              statusJson.put("definition", agent.agentDefinition.name);
              statusJson.put("description", agent.description);
              statusJson.put("status", agent.getStatus());
              statusJson.put("instantiated", DateUtils.toRfcString(agent.timeInstantiated));
              long lastUpdated = agent.timeUpdated;
              statusJson.put("updated", lastUpdated > 0 ? DateUtils.toRfcString(lastUpdated) : "");
              long lastInputsChanged = agent.lastInputsChanged;
              statusJson.put("inputs_changed", lastInputsChanged > 0 ? DateUtils.toRfcString(lastInputsChanged) : "");
              long lastTriggered = agent.lastTriggered;
              statusJson.put("triggered", lastTriggered > 0 ? DateUtils.toRfcString(lastTriggered) : "");
              int outputsSize = agent.outputHistory.size();
              long lastOutput = outputsSize > 0 ? agent.outputHistory.get(outputsSize - 1).time : 0;
              statusJson.put("outputs_changed", lastOutput > 0 ? DateUtils.toRfcString(lastOutput) : "");
                      
              // Done
              setOutput(statusJson);
              */
        AgentInstanceList agentMap = agentServer.agentInstances.get(user.id);
        AgentInstance agentInstance = agentMap.get(agentName);
        setOutput(agentInstance.toJson(includeState, count));
    } else if (lcPath.matches("^/users/[a-zA-Z0-9_.@\\-*]*/website_access$")) {
        User user = checkAdminUserAccess();

        log.info("Getting web site access controls for user: " + user.id);

        // Get the access control list for the user
        ListMap<String, String> accessList = agentServer.getWebSiteAccessControls(user);

        // Put the list in JSON format
        JSONObject accessListJson = new JSONObject();
        for (String url : accessList)
            accessListJson.put(url, accessList.get(url));

        // Done
        setOutput(accessListJson);
    } else {
        throw new AgentAppServerException(HttpServletResponse.SC_NOT_FOUND,
                "Path does not address any existing object");
    }
    return true;
}

From source file:com.xmlcalabash.library.ApacheHttpRequest.java

private void readBodyContent(TreeWriter tree, InputStream bodyStream, HttpMethodBase method)
        throws SaxonApiException, IOException {
    // Find the content type
    String contentType = getContentType(method);
    String charset = method.getResponseCharSet();
    String boundary = null;/*  w ww  .  j  av  a2  s  .  co  m*/

    if (overrideContentType != null) {
        contentType = overrideContentType;
    }

    if (contentType.startsWith("multipart/")) {
        boundary = getContentBoundary(method);

        tree.addStartElement(XProcConstants.c_multipart);
        tree.addAttribute(_content_type, getFullContentType(method));
        tree.addAttribute(_boundary, boundary);
        tree.startContent();

        for (Header header : method.getResponseHeaders()) {
            // FIXME: what about parameters?
            if (header.getName().toLowerCase().equals("transfer-encoding")) {
                // nop;
            } else {
                tree.addStartElement(XProcConstants.c_header);
                tree.addAttribute(_name, header.getName());
                tree.addAttribute(_value, header.getValue());
                tree.startContent();
                tree.addEndElement();
            }
        }

        MIMEReader reader = new MIMEReader(bodyStream, boundary);
        boolean done = false;
        while (reader.readHeaders()) {
            Header pctype = reader.getHeader("Content-Type");
            Header pclen = reader.getHeader("Content-Length");

            contentType = getHeaderValue(pctype);

            charset = getContentCharset(pctype);
            String partType = getHeaderValue(pctype);
            InputStream partStream = null;

            if (pclen != null) {
                int len = Integer.parseInt(getHeaderValue(pclen));
                partStream = reader.readBodyPart(len);
            } else {
                partStream = reader.readBodyPart();
            }

            tree.addStartElement(XProcConstants.c_body);
            tree.addAttribute(_content_type, contentType);
            if (!xmlContentType(contentType) && !textContentType(contentType)) {
                tree.addAttribute(_encoding, "base64");
            }
            tree.startContent();

            if (xmlContentType(partType)) {
                BufferedReader preader = new BufferedReader(new InputStreamReader(partStream, charset));
                // Read it as XML
                SAXSource source = new SAXSource(new InputSource(preader));
                DocumentBuilder builder = runtime.getProcessor().newDocumentBuilder();
                tree.addSubtree(builder.build(source));
            } else if (textContentType(partType)) {
                BufferedReader preader = new BufferedReader(new InputStreamReader(partStream, charset));
                // Read it as text
                char buf[] = new char[bufSize];
                int len = preader.read(buf, 0, bufSize);
                while (len >= 0) {
                    // I'm unsure about this. If I'm reading text and injecting it into XML,
                    // I think I need to change CR/LF pairs (and CR not followed by LF) into
                    // plain LFs.

                    char fbuf[] = new char[bufSize];
                    char flen = 0;
                    for (int pos = 0; pos < len; pos++) {
                        if (buf[pos] == '\r') {
                            if (pos + 1 == len) {
                                // FIXME: Check for CR/LF pairs that cross a buffer boundary!
                                // Assume it's part of a CR/LF pair...
                            } else {
                                if (buf[pos + 1] == '\n') {
                                    // nop
                                } else {
                                    fbuf[flen++] = '\n';
                                }
                            }
                        } else {
                            fbuf[flen++] = buf[pos];
                        }
                    }

                    tree.addText(new String(fbuf, 0, flen));
                    len = preader.read(buf, 0, bufSize);
                }
            } else {
                // Read it as binary
                byte bytes[] = new byte[bufSize];
                int pos = 0;
                int readLen = bufSize;
                int len = partStream.read(bytes, 0, bufSize);
                while (len >= 0) {
                    pos += len;
                    readLen -= len;
                    if (readLen == 0) {
                        tree.addText(Base64.encodeBytes(bytes));
                        pos = 0;
                        readLen = bufSize;
                    }

                    len = partStream.read(bytes, pos, readLen);
                }

                if (pos > 0) {
                    byte lastBytes[] = new byte[pos];
                    System.arraycopy(bytes, 0, lastBytes, 0, pos);
                    tree.addText(Base64.encodeBytes(lastBytes));
                }

                tree.addText("\n"); // FIXME: should we be doing this?
            }

            tree.addEndElement();
        }

        tree.addEndElement();
    } else {
        if (xmlContentType(contentType)) {
            readBodyContentPart(tree, bodyStream, contentType, charset);
        } else {
            tree.addStartElement(XProcConstants.c_body);
            tree.addAttribute(_content_type, getFullContentType(method));
            if (!xmlContentType(contentType) && !textContentType(contentType)) {
                tree.addAttribute(_encoding, "base64");
            }
            tree.startContent();
            readBodyContentPart(tree, bodyStream, contentType, charset);
            tree.addEndElement();
        }
    }
}