Example usage for javax.servlet ServletRequestWrapper getInputStream

List of usage examples for javax.servlet ServletRequestWrapper getInputStream

Introduction

In this page you can find the example usage for javax.servlet ServletRequestWrapper getInputStream.

Prototype

public ServletInputStream getInputStream() throws IOException 

Source Link

Document

The default behavior of this method is to return getInputStream() on the wrapped request object.

Usage

From source file:org.deegree.enterprise.servlet.SimpleProxyServlet.java

/**
 * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest,
 *      javax.servlet.http.HttpServletResponse)
 *///from   w  ww  . j  ava2 s  .  c o m
@Override
protected void doPost(HttpServletRequest origReq, HttpServletResponse response)
        throws ServletException, IOException {
    // wrap request to enable access of the requests InputStream more
    // than one time
    ServletRequestWrapper request = new ServletRequestWrapper(origReq);
    OutputStream os = null;
    try {
        if (LOG.getLevel() == ILogger.LOG_DEBUG) {
            // because this is an expensive operation it just will
            // performed if debug level is set too DEBUG
            InputStream reqIs = request.getInputStream();
            StringBuffer sb = new StringBuffer(10000);
            int c = 0;
            while ((c = reqIs.read()) > -1) {
                sb.append((char) c);
            }
            reqIs.close();
            LOG.logDebug("Request: " + sb);
        }
        OGCWebServiceRequest req = OGCRequestFactory.create(request);

        String hostAddr = host.get(req.getServiceName());
        LOG.logDebug("forward URL: " + hostAddr);
        if (hostAddr == null) {
            throw new Exception(Messages.getMessage("PROXY_SERVLET_UNDEFINED_HOST", req.getServiceName()));
        }

        // determine charset for setting request content type
        // use system charset if no charset can be determined
        // from incoming request
        String charset = origReq.getCharacterEncoding();
        LOG.logDebug("request character encoding: ", charset);
        if (charset == null) {
            charset = CharsetUtils.getSystemCharset();
            LOG.logDebug("use sytem character encoding: ", charset);
        }

        HttpClient client = new HttpClient();
        client = WebUtils.enableProxyUsage(client, new URL(hostAddr));
        PostMethod post = new PostMethod(hostAddr);
        post.setRequestHeader("Content-type", "text/xml; charset=" + charset);
        post.setRequestEntity(new InputStreamRequestEntity(request.getInputStream()));
        client.executeMethod(post);

        LOG.logDebug("Content-type: ", post.getResponseHeader("Content-type"));

        os = response.getOutputStream();
        os.write(post.getResponseBody());
    } catch (Exception e) {
        e.printStackTrace();
        response.setContentType("text/plain; charset=" + CharsetUtils.getSystemCharset());
        os.write(StringTools.stackTraceToString(e).getBytes());
    } finally {
        try {
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}