Example usage for org.apache.commons.httpclient HttpMethodBase getResponseBodyAsStream

List of usage examples for org.apache.commons.httpclient HttpMethodBase getResponseBodyAsStream

Introduction

In this page you can find the example usage for org.apache.commons.httpclient HttpMethodBase getResponseBodyAsStream.

Prototype

@Override
public InputStream getResponseBodyAsStream() throws IOException 

Source Link

Document

Returns the response body of the HTTP method, if any, as an InputStream .

Usage

From source file:com.nextcloud.android.sso.InputStreamBinder.java

private InputStream processRequest(final NextcloudRequest request) throws UnsupportedOperationException,
        com.owncloud.android.lib.common.accounts.AccountUtils.AccountNotFoundException,
        OperationCanceledException, AuthenticatorException, IOException {
    Account account = AccountUtils.getOwnCloudAccountByName(context, request.getAccountName()); // TODO handle case that account is not found!
    if (account == null) {
        throw new IllegalStateException(EXCEPTION_ACCOUNT_NOT_FOUND);
    }/* w  ww  .j  a v  a  2 s.  co m*/

    // Validate token
    if (!isValid(request)) {
        throw new IllegalStateException(EXCEPTION_INVALID_TOKEN);
    }

    // Validate URL
    if (request.getUrl().length() == 0 || request.getUrl().charAt(0) != PATH_SEPARATOR) {
        throw new IllegalStateException(EXCEPTION_INVALID_REQUEST_URL,
                new IllegalStateException("URL need to start with a /"));
    }

    OwnCloudClientManager ownCloudClientManager = OwnCloudClientManagerFactory.getDefaultSingleton();
    OwnCloudAccount ocAccount = new OwnCloudAccount(account, context);
    OwnCloudClient client = ownCloudClientManager.getClientFor(ocAccount, context);

    String requestUrl = client.getBaseUri() + request.getUrl();
    HttpMethodBase method;

    switch (request.getMethod()) {
    case "GET":
        method = new GetMethod(requestUrl);
        break;

    case "POST":
        method = new PostMethod(requestUrl);
        if (request.getRequestBody() != null) {
            StringRequestEntity requestEntity = new StringRequestEntity(request.getRequestBody(),
                    CONTENT_TYPE_APPLICATION_JSON, CHARSET_UTF8);
            ((PostMethod) method).setRequestEntity(requestEntity);
        }
        break;

    case "PUT":
        method = new PutMethod(requestUrl);
        if (request.getRequestBody() != null) {
            StringRequestEntity requestEntity = new StringRequestEntity(request.getRequestBody(),
                    CONTENT_TYPE_APPLICATION_JSON, CHARSET_UTF8);
            ((PutMethod) method).setRequestEntity(requestEntity);
        }
        break;

    case "DELETE":
        method = new DeleteMethod(requestUrl);
        break;

    default:
        throw new UnsupportedOperationException(EXCEPTION_UNSUPPORTED_METHOD);

    }

    method.setQueryString(convertMapToNVP(request.getParameter()));
    method.addRequestHeader("OCS-APIREQUEST", "true");

    client.setFollowRedirects(request.isFollowRedirects());
    int status = client.executeMethod(method);

    // Check if status code is 2xx --> https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#2xx_Success
    if (status >= HTTP_STATUS_CODE_OK && status < HTTP_STATUS_CODE_MULTIPLE_CHOICES) {
        return method.getResponseBodyAsStream();
    } else {
        throw new IllegalStateException(EXCEPTION_HTTP_REQUEST_FAILED,
                new IllegalStateException(String.valueOf(status)));
    }
}

From source file:com.atlantbh.jmeter.plugins.rest.RestSampler.java

/**
 * Method invoked by JMeter when a sample needs to happen. It's actually an
 * indirect call from the main sampler interface. it's resolved in the base
 * class./*from   w w  w . ja  v a 2s  .c  om*/
 * 
 * This is a copy and paste from the HTTPSampler2 - quick and dirty hack as
 * that class is not very extensible. The reason to extend and slightly
 * modify is that I needed to get the body content from a text field in the
 * GUI rather than a file.
 */
public SampleResult sample() {
    HttpMethodBase httpMethod = null;
    HttpClient client = null;
    InputStream instream = null;
    SampleResult res = new SampleResult();
    try {
        res.setSuccessful(false);
        res.setResponseCode("000");
        res.setSampleLabel(getName());
        res.setURL(getUrl());
        res.setDataEncoding("UTF-8");
        res.setDataType("text/xml");
        res.setMonitor(isMonitor());
        res.sampleStart();

        String urlStr = getUrl().toString();

        String request = getMethod().toString() + " " + urlStr + "\n";
        request += getRequestBody().toString();
        res.setSamplerData(request);

        log.debug("Start : sample " + urlStr);
        log.debug("method " + getMethod());

        httpMethod = createHttpMethod(getMethod(), urlStr);
        // Set any default request headers
        setDefaultRequestHeaders(httpMethod);

        // Setup connection
        client = setupConnection(getUrl(), httpMethod);
        // Handle the various methods
        if (httpMethod instanceof EntityEnclosingMethod) {

            ((EntityEnclosingMethod) httpMethod)
                    .setRequestEntity(new StringRequestEntity(getRequestBody(), "text/xml", "UTF-8"));
            //res.setQueryString(getRequestBody());
            //String postBody = sendData((EntityEnclosingMethod) httpMethod);
            //res.setResponseData(postBody.getBytes());

            //String postBody = "";
            //try { postBody = Base64Util.processStargateRequest(getRequestBody());}
            //catch (Exception e) {postBody =getRequestBody(); e.printStackTrace();}
            //
            //res.setSamplerData(/*res.getSamplerData() + "\r\n*/"ORIGINAL CONTENT:\r\n\r\n" + getRequestBody() + "\r\n\r\nSENT CONTENT:\r\n\r\n" + postBody);
        }
        overrideHeaders(httpMethod);
        res.setRequestHeaders(getConnectionHeaders(httpMethod));

        int statusCode = -1;
        try {
            statusCode = client.executeMethod(httpMethod);
        } catch (RuntimeException e) {
            log.error("Exception when executing '" + httpMethod + "'", e);
            throw e;
        }

        // Request sent. Now get the response:
        instream = httpMethod.getResponseBodyAsStream();
        if (instream != null) {// will be null for HEAD

            Header responseHeader = httpMethod.getResponseHeader(HEADER_CONTENT_ENCODING);
            if (responseHeader != null && ENCODING_GZIP.equals(responseHeader.getValue())) {
                instream = new GZIPInputStream(instream);
            }
            res.setResponseData(readResponse(res, instream, (int) httpMethod.getResponseContentLength()));
        }
        //if (res.getResponseDataAsString().length() != 0)
        //   try{
        //      String response = Base64Util.processStargateResponse(res.getResponseDataAsString());
        //      res.setResponseData(response);    
        //   } catch (Exception e)
        //   {
        //      log.error(e.getMessage());
        //   }

        res.sampleEnd();
        // Done with the sampling proper.

        // Now collect the results into the HTTPSampleResult:

        // res.setSampleLabel(httpMethod.getURI().toString());
        // Pick up Actual path (after redirects)

        res.setResponseCode(Integer.toString(statusCode));
        res.setSuccessful(isSuccessCode(statusCode));

        res.setResponseMessage(httpMethod.getStatusText());

        String ct = null;
        org.apache.commons.httpclient.Header h = httpMethod.getResponseHeader(HEADER_CONTENT_TYPE);
        if (h != null)// Can be missing, e.g. on redirect
        {
            ct = h.getValue();
            res.setContentType(ct);// e.g. text/html; charset=ISO-8859-1
            res.setEncodingAndType(ct);
        }

        String responseHeaders = getResponseHeaders(httpMethod);
        res.setResponseHeaders(responseHeaders);

        /*if (res.isRedirect()) {
           final Header headerLocation = httpMethod.getResponseHeader(HEADER_LOCATION);
        if (headerLocation == null) { // HTTP protocol violation, but
            // avoids NPE
            throw new IllegalArgumentException("Missing location header");
        }
        res.setRedirectLocation(headerLocation.getValue());
        }
                
        // If we redirected automatically, the URL may have changed
        if (getAutoRedirects()) {
        res.setURL(new URL(httpMethod.getURI().toString()));
        }
                
        // Store any cookies received in the cookie manager:
        saveConnectionCookies(httpMethod, res.getURL(), getCookieManager());
                
        // Save cache information
        final CacheManager cacheManager = getCacheManager();
        if (cacheManager != null) {
        cacheManager.saveDetails(httpMethod, res);
        }
                
        // Follow redirects and download page resources if appropriate:
        res = resultProcessing(areFollowingRedirect, frameDepth, res);
        */
        log.debug("End : sample");
        httpMethod.releaseConnection();

        return res;
    } catch (MalformedURLException e) {
        res.sampleEnd();
        log.warn(e.getMessage());
        res.setResponseMessage(e.getMessage());
        return res;
    } catch (IllegalArgumentException e)// e.g. some kinds of invalid URL
    {
        res.sampleEnd();
        //SampleResult err = errorResult(e, res);
        //err.setSampleLabel("Error: " + url.toString());
        log.warn(e.getMessage());
        res.setResponseMessage(e.getMessage());
        return res;
    } catch (IOException e) {
        res.sampleEnd();
        log.warn(e.getMessage());
        res.setResponseMessage(e.getMessage());
        return res;
    } finally {
        JOrphanUtils.closeQuietly(instream);
        if (httpMethod != null) {
            httpMethod.releaseConnection();
            return res;
        }
    }
}

From source file:com.xerox.amazonws.common.AWSQueryConnection.java

/**
 * Make a http request and process the response. This method also performs automatic retries.
*
 * @param method The HTTP method to use (GET, POST, DELETE, etc)
 * @param action the name of the action for this query request
 * @param params map of request params/*from  www.j  a  va 2  s . com*/
 * @param respType the class that represents the desired/expected return type
 */
protected <T> T makeRequest(HttpMethodBase method, String action, Map<String, String> params, Class<T> respType)
        throws HttpException, IOException, JAXBException {

    // add auth params, and protocol specific headers
    Map<String, String> qParams = new HashMap<String, String>(params);
    qParams.put("Action", action);
    qParams.put("AWSAccessKeyId", getAwsAccessKeyId());
    qParams.put("SignatureVersion", "" + sigVersion);
    qParams.put("Timestamp", httpDate());
    if (headers != null) {
        for (Iterator<String> i = headers.keySet().iterator(); i.hasNext();) {
            String key = i.next();
            for (Iterator<String> j = headers.get(key).iterator(); j.hasNext();) {
                qParams.put(key, j.next());
            }
        }
    }
    // sort params by key
    ArrayList<String> keys = new ArrayList<String>(qParams.keySet());
    Collator stringCollator = Collator.getInstance();
    stringCollator.setStrength(Collator.PRIMARY);
    Collections.sort(keys, stringCollator);

    // build param string
    StringBuilder resource = new StringBuilder();
    if (sigVersion == 0) { // ensure Action, Timestamp come first!
        resource.append(qParams.get("Action"));
        resource.append(qParams.get("Timestamp"));
    } else {
        for (String key : keys) {
            resource.append(key);
            resource.append(qParams.get(key));
        }
    }

    // calculate signature
    String encoded = urlencode(encode(getSecretAccessKey(), resource.toString(), false));

    // build param string, encoding values and adding request signature
    resource = new StringBuilder();
    for (String key : keys) {
        resource.append("&");
        resource.append(key);
        resource.append("=");
        resource.append(urlencode(qParams.get(key)));
    }
    resource.setCharAt(0, '?'); // set first param delimeter
    resource.append("&Signature=");
    resource.append(encoded);

    // finally, build request object
    URL url = makeURL(resource.toString());
    method.setURI(new URI(url.toString(), true));
    method.setRequestHeader(new Header("User-Agent", userAgent));
    if (sigVersion == 0) {
        method.setRequestHeader(new Header("Content-Type", "application/x-www-form-urlencoded; charset=utf-8"));
    }
    Object response = null;
    boolean done = false;
    int retries = 0;
    boolean doRetry = false;
    String errorMsg = "";
    do {
        int responseCode = 600; // default to high value, so we don't think it is valid
        try {
            responseCode = getHttpClient().executeMethod(method);
        } catch (SocketException ex) {
            // these can generally be retried. Treat it like a 500 error
            doRetry = true;
            errorMsg = ex.getMessage();
        }
        // 100's are these are handled by httpclient
        if (responseCode < 300) {
            // 200's : parse normal response into requested object
            if (respType != null) {
                InputStream iStr = method.getResponseBodyAsStream();
                response = JAXBuddy.deserializeXMLStream(respType, iStr);
            }
            done = true;
        } else if (responseCode < 400) {
            // 300's : what to do?
            throw new HttpException("redirect error : " + responseCode);
        } else if (responseCode < 500) {
            // 400's : parse client error message
            String body = getStringFromStream(method.getResponseBodyAsStream());
            throw new HttpException("Client error : " + getErrorDetails(body));
        } else if (responseCode < 600) {
            // 500's : retry...
            doRetry = true;
            String body = getStringFromStream(method.getResponseBodyAsStream());
            errorMsg = getErrorDetails(body);
        }
        if (doRetry) {
            retries++;
            if (retries > maxRetries) {
                throw new HttpException("Number of retries exceeded : " + action + ", " + errorMsg);
            }
            doRetry = false;
            try {
                Thread.sleep((int) Math.pow(2.0, retries) * 1000);
            } catch (InterruptedException ex) {
            }
        }
    } while (!done);
    return (T) response;
}

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

public void run() throws SaxonApiException {
    super.run();/*from   w ww .ja  v  a 2  s.c  om*/

    XdmNode requestDoc = source.read();
    XdmNode start = S9apiUtils.getDocumentElement(requestDoc);

    if (!c_request.equals(start.getNodeName())) {
        throw new UnsupportedOperationException("Not a c:http-request!");
    }

    // Check for valid attributes
    XdmSequenceIterator iter = start.axisIterator(Axis.ATTRIBUTE);
    boolean ok = true;
    while (iter.hasNext()) {
        XdmNode attr = (XdmNode) iter.next();
        QName name = attr.getNodeName();
        if (_method.equals(name) || _href.equals(name) || _detailed.equals(name) || _status_only.equals(name)
                || _username.equals(name) || _password.equals(name) || _auth_method.equals(name)
                || _send_authorization.equals(name) || _override_content_type.equals(name)) {
            // nop
        } else {
            if (XMLConstants.DEFAULT_NS_PREFIX.equals(name.getNamespaceURI())) {
                throw new XProcException("Unsupported attribute on c:http-request: " + name);
            }
        }
    }

    method = start.getAttributeValue(_method);
    statusOnly = "true".equals(start.getAttributeValue(_status_only));
    detailed = "true".equals(start.getAttributeValue(_detailed));
    overrideContentType = start.getAttributeValue(_override_content_type);

    if (start.getAttributeValue(_href) == null) {
        throw new XProcException("The 'href' attribute must be specified on p:http-request");
    }

    requestURI = start.getBaseURI().resolve(start.getAttributeValue(_href));

    if ("file".equals(requestURI.getScheme())) {
        doFile();
        return;
    }

    client = new HttpClient();

    String timeOutStr = step.getExtensionAttribute(cx_timeout);
    if (timeOutStr != null) {
        HttpMethodParams params = client.getParams();
        params.setSoTimeout(Integer.parseInt(timeOutStr));
    }

    ProxySelector proxySelector = ProxySelector.getDefault();
    List<Proxy> plist = proxySelector.select(requestURI);
    // I have no idea what I'm expected to do if I get more than one...
    if (plist.size() > 0) {
        Proxy proxy = plist.get(0);
        switch (proxy.type()) {
        case DIRECT:
            // nop;
            break;
        case HTTP:
            // This can't cause a ClassCastException, right?
            InetSocketAddress addr = (InetSocketAddress) proxy.address();
            String host = addr.getHostName();
            int port = addr.getPort();
            client.getHostConfiguration().setProxy(host, port);
            break;
        default:
            // FIXME: send out a log message
            break;
        }
    }

    if (start.getAttributeValue(_username) != null) {
        String user = start.getAttributeValue(_username);
        String pass = start.getAttributeValue(_password);
        String meth = start.getAttributeValue(_auth_method);

        if (meth == null || !("basic".equals(meth.toLowerCase()) || "digest".equals(meth.toLowerCase()))) {
            throw XProcException.stepError(3, "Unsupported auth-method: " + meth);
        }

        String host = requestURI.getHost();
        int port = requestURI.getPort();
        AuthScope scope = new AuthScope(host, port);

        UsernamePasswordCredentials cred = new UsernamePasswordCredentials(user, pass);

        client.getState().setCredentials(scope, cred);
    }

    iter = start.axisIterator(Axis.CHILD);
    XdmNode body = null;
    while (iter.hasNext()) {
        XdmNode event = (XdmNode) iter.next();
        // FIXME: What about non-whitespace text nodes?
        if (event.getNodeKind() == XdmNodeKind.ELEMENT) {
            if (body != null) {
                throw new UnsupportedOperationException("Elements follow c:multipart or c:body");
            }

            if (XProcConstants.c_header.equals(event.getNodeName())) {
                headers.add(new Header(event.getAttributeValue(_name), event.getAttributeValue(_value)));
            } else if (XProcConstants.c_multipart.equals(event.getNodeName())
                    || XProcConstants.c_body.equals(event.getNodeName())) {
                body = event;
            } else {
                throw new UnsupportedOperationException("Unexpected request element: " + event.getNodeName());
            }
        }
    }

    HttpMethodBase httpResult;

    if (method == null) {
        throw new XProcException("Method must be specified.");
    }

    if ("get".equals(method.toLowerCase())) {
        httpResult = doGet();
    } else if ("post".equals(method.toLowerCase())) {
        httpResult = doPost(body);
    } else if ("put".equals(method.toLowerCase())) {
        httpResult = doPut(body);
    } else if ("head".equals(method.toLowerCase())) {
        httpResult = doHead();
    } else {
        throw new UnsupportedOperationException("Unrecognized http method: " + method);
    }

    TreeWriter tree = new TreeWriter(runtime);
    tree.startDocument(requestURI);

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

        String contentType = getContentType(httpResult);
        if (overrideContentType != null) {
            if ((xmlContentType(contentType) && overrideContentType.startsWith("image/"))
                    || (contentType.startsWith("text/") && overrideContentType.startsWith("image/"))
                    || (contentType.startsWith("image/") && xmlContentType(overrideContentType))
                    || (contentType.startsWith("image/") && overrideContentType.startsWith("text/"))
                    || (contentType.startsWith("multipart/") && !overrideContentType.startsWith("multipart/"))
                    || (!contentType.startsWith("multipart/")
                            && overrideContentType.startsWith("multipart/"))) {
                throw XProcException.stepError(30);
            }

            //System.err.println(overrideContentType + " overrides " + contentType);
            contentType = overrideContentType;
        }

        if (detailed) {
            tree.addStartElement(XProcConstants.c_response);
            tree.addAttribute(_status, "" + statusCode);
            tree.startContent();

            for (Header header : httpResult.getResponseHeaders()) {
                // I don't understand why/how HeaderElement parsing works. I get very weird results.
                // So I'm just going to go the long way around...
                String h = header.toString();
                int cp = h.indexOf(":");
                String name = header.getName();
                String value = h.substring(cp + 1).trim();

                tree.addStartElement(XProcConstants.c_header);
                tree.addAttribute(_name, name);
                tree.addAttribute(_value, value);
                tree.startContent();
                tree.addEndElement();
            }

            if (statusOnly) {
                // Skip reading the result
            } else {
                // Read the response body.
                InputStream bodyStream = httpResult.getResponseBodyAsStream();
                readBodyContent(tree, bodyStream, httpResult);
            }

            tree.addEndElement();
        } else {
            if (statusOnly) {
                // Skip reading the result
            } else {
                // Read the response body.
                InputStream bodyStream = httpResult.getResponseBodyAsStream();
                readBodyContent(tree, bodyStream, httpResult);
            }
        }
    } catch (Exception e) {
        throw new XProcException(e);
    } finally {
        // Release the connection.
        httpResult.releaseConnection();
    }

    tree.endDocument();

    XdmNode resultNode = tree.getResult();

    result.write(resultNode);
}

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

public void gorun() throws SaxonApiException {
    super.gorun();

    XdmNode requestDoc = source.read(stepContext);
    XdmNode start = S9apiUtils.getDocumentElement(requestDoc);

    if (!c_request.equals(start.getNodeName())) {
        throw XProcException.stepError(40);
    }//from  w  w  w  .j  a v  a  2s .c o m

    // Check for valid attributes
    XdmSequenceIterator iter = start.axisIterator(Axis.ATTRIBUTE);
    boolean ok = true;
    while (iter.hasNext()) {
        XdmNode attr = (XdmNode) iter.next();
        QName name = attr.getNodeName();
        if (_method.equals(name) || _href.equals(name) || _detailed.equals(name) || _status_only.equals(name)
                || _username.equals(name) || _password.equals(name) || _auth_method.equals(name)
                || _send_authorization.equals(name) || _override_content_type.equals(name)) {
            // nop
        } else {
            if (XMLConstants.DEFAULT_NS_PREFIX.equals(name.getNamespaceURI())) {
                throw new XProcException(step.getNode(),
                        "Unsupported attribute on c:request for p:http-request: " + name);
            }
        }
    }

    String send = step.getExtensionAttribute(cx_send_binary);
    encodeBinary = !"true".equals(send);

    method = start.getAttributeValue(_method);
    statusOnly = "true".equals(start.getAttributeValue(_status_only));
    detailed = "true".equals(start.getAttributeValue(_detailed));
    overrideContentType = start.getAttributeValue(_override_content_type);

    if (method == null) {
        throw XProcException.stepError(6);
    }

    if (statusOnly && !detailed) {
        throw XProcException.stepError(4);
    }

    if (start.getAttributeValue(_href) == null) {
        throw new XProcException(step.getNode(),
                "The 'href' attribute must be specified on c:request for p:http-request");
    }

    requestURI = start.getBaseURI().resolve(start.getAttributeValue(_href));

    if ("file".equals(requestURI.getScheme())) {
        doFile();
        return;
    }

    // What about cookies
    String saveCookieKey = step.getExtensionAttribute(cx_save_cookies);
    String useCookieKeys = step.getExtensionAttribute(cx_use_cookies);
    String cookieKey = step.getExtensionAttribute(cx_cookies);

    if (saveCookieKey == null) {
        saveCookieKey = cookieKey;
    }

    if (useCookieKeys == null) {
        useCookieKeys = cookieKey;
    }

    client = new HttpClient();
    client.getParams().setCookiePolicy(CookiePolicy.RFC_2109);
    client.getParams().setParameter("http.protocol.single-cookie-header", true);

    HttpState state = client.getState();

    if (useCookieKeys != null) {
        for (String key : useCookieKeys.split("\\s+")) {
            for (Cookie cookie : runtime.getCookies(key)) {
                state.addCookie(cookie);
            }
        }
    }

    String timeOutStr = step.getExtensionAttribute(cx_timeout);
    if (timeOutStr != null) {
        HttpMethodParams params = client.getParams();
        params.setSoTimeout(Integer.parseInt(timeOutStr));
    }

    ProxySelector proxySelector = ProxySelector.getDefault();
    List<Proxy> plist = proxySelector.select(requestURI);
    // I have no idea what I'm expected to do if I get more than one...
    if (plist.size() > 0) {
        Proxy proxy = plist.get(0);
        switch (proxy.type()) {
        case DIRECT:
            // nop;
            break;
        case HTTP:
            // This can't cause a ClassCastException, right?
            InetSocketAddress addr = (InetSocketAddress) proxy.address();
            String host = addr.getHostName();
            int port = addr.getPort();
            client.getHostConfiguration().setProxy(host, port);
            break;
        default:
            // FIXME: send out a log message
            break;
        }
    }

    if (start.getAttributeValue(_username) != null) {
        String user = start.getAttributeValue(_username);
        String pass = start.getAttributeValue(_password);
        String meth = start.getAttributeValue(_auth_method);

        if (meth == null || !("basic".equals(meth.toLowerCase()) || "digest".equals(meth.toLowerCase()))) {
            throw XProcException.stepError(3, "Unsupported auth-method: " + meth);
        }

        String host = requestURI.getHost();
        int port = requestURI.getPort();
        AuthScope scope = new AuthScope(host, port);

        UsernamePasswordCredentials cred = new UsernamePasswordCredentials(user, pass);

        client.getState().setCredentials(scope, cred);

        if ("basic".equals(meth.toLowerCase())) {
            client.getParams().setAuthenticationPreemptive(true);
        }
    }

    iter = start.axisIterator(Axis.CHILD);
    XdmNode body = null;
    while (iter.hasNext()) {
        XdmNode event = (XdmNode) iter.next();
        // FIXME: What about non-whitespace text nodes?
        if (event.getNodeKind() == XdmNodeKind.ELEMENT) {
            if (body != null) {
                throw new UnsupportedOperationException("Elements follow c:multipart or c:body");
            }

            if (XProcConstants.c_header.equals(event.getNodeName())) {
                String name = event.getAttributeValue(_name);
                if (name == null) {
                    continue; // this can't happen, right?
                }
                if (name.toLowerCase().equals("content-type")) {
                    // We'll deal with the content-type header later
                    headerContentType = event.getAttributeValue(_value).toLowerCase();
                } else {
                    headers.add(new Header(event.getAttributeValue(_name), event.getAttributeValue(_value)));
                }
            } else if (XProcConstants.c_multipart.equals(event.getNodeName())
                    || XProcConstants.c_body.equals(event.getNodeName())) {
                body = event;
            } else {
                throw new UnsupportedOperationException("Unexpected request element: " + event.getNodeName());
            }
        }
    }

    String lcMethod = method.toLowerCase();

    // You can only have a body on PUT or POST
    if (body != null && !("put".equals(lcMethod) || "post".equals(lcMethod))) {
        throw XProcException.stepError(5);
    }

    HttpMethodBase httpResult;

    if ("get".equals(lcMethod)) {
        httpResult = doGet();
    } else if ("post".equals(lcMethod)) {
        httpResult = doPost(body);
    } else if ("put".equals(lcMethod)) {
        httpResult = doPut(body);
    } else if ("head".equals(lcMethod)) {
        httpResult = doHead();
    } else if ("delete".equals(lcMethod)) {
        httpResult = doDelete();
    } else {
        throw new UnsupportedOperationException("Unrecognized http method: " + method);
    }

    TreeWriter tree = new TreeWriter(runtime);
    tree.startDocument(requestURI);

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

        // Deal with cookies
        if (saveCookieKey != null) {
            runtime.clearCookies(saveCookieKey);

            state = client.getState();
            Cookie[] cookies = state.getCookies();
            for (Cookie cookie : cookies) {
                runtime.addCookie(saveCookieKey, cookie);
            }
        }

        String contentType = getContentType(httpResult);
        if (overrideContentType != null) {
            if ((xmlContentType(contentType) && overrideContentType.startsWith("image/"))
                    || (contentType.startsWith("text/") && overrideContentType.startsWith("image/"))
                    || (contentType.startsWith("image/") && xmlContentType(overrideContentType))
                    || (contentType.startsWith("image/") && overrideContentType.startsWith("text/"))
                    || (contentType.startsWith("multipart/") && !overrideContentType.startsWith("multipart/"))
                    || (!contentType.startsWith("multipart/")
                            && overrideContentType.startsWith("multipart/"))) {
                throw XProcException.stepError(30);
            }

            //System.err.println(overrideContentType + " overrides " + contentType);
            contentType = overrideContentType;
        }

        if (detailed) {
            tree.addStartElement(XProcConstants.c_response);
            tree.addAttribute(_status, "" + statusCode);
            tree.startContent();

            for (Header header : httpResult.getResponseHeaders()) {
                // I don't understand why/how HeaderElement parsing works. I get very weird results.
                // So I'm just going to go the long way around...
                String h = header.toString();
                int cp = h.indexOf(":");
                String name = header.getName();
                String value = h.substring(cp + 1).trim();

                tree.addStartElement(XProcConstants.c_header);
                tree.addAttribute(_name, name);
                tree.addAttribute(_value, value);
                tree.startContent();
                tree.addEndElement();
            }

            if (statusOnly) {
                // Skip reading the result
            } else {
                // Read the response body.
                InputStream bodyStream = httpResult.getResponseBodyAsStream();
                if (bodyStream != null) {
                    readBodyContent(tree, bodyStream, httpResult);
                }
            }

            tree.addEndElement();
        } else {
            if (statusOnly) {
                // Skip reading the result
            } else {
                // Read the response body.
                InputStream bodyStream = httpResult.getResponseBodyAsStream();
                if (bodyStream != null) {
                    readBodyContent(tree, bodyStream, httpResult);
                } else {
                    throw XProcException.dynamicError(6);
                }
            }
        }
    } catch (Exception e) {
        throw new XProcException(e);
    } finally {
        // Release the connection.
        httpResult.releaseConnection();
    }

    tree.endDocument();

    XdmNode resultNode = tree.getResult();

    result.write(stepContext, resultNode);
}

From source file:com.snaker.DownloadManager.java

private Runnable createRunnable(final Downloader d) {
    final Task task = d.getTask();
    return new Runnable() {
        @Override/*from   w w  w .  j av a 2 s.  c  o  m*/
        public void run() {
            logger.info("start download:" + d.getUrl());
            HttpClient client = clients.get(task.getId());
            DownloadHandler handler = d.getHandler();
            HttpMethodBase m = null;
            d.setStatus(Status.STARTED);
            d.setStartTime(System.currentTimeMillis());
            try {
                String url = d.getUrl();
                if (d.isGet()) {
                    GetMethod get = new GetMethod(url);
                    m = get;
                } else {
                    final String requestCharset = d.getRequestCharset();
                    PostMethod post = new PostMethod(url) {
                        public String getRequestCharSet() {
                            if (requestCharset != null)
                                return requestCharset;
                            else
                                return super.getRequestCharSet();
                        }

                        public boolean getFollowRedirects() {
                            return d.isFollowRedirects();
                        }
                    };
                    if (requestCharset != null) {
                        post.setRequestHeader("ContentType",
                                "application/x-www-form-urlencoded;charset=" + requestCharset);
                    }
                    DownloadParams parms = d.getParms();
                    if (parms != null)
                        post.setRequestBody(parms.toNVP());
                    m = post;
                }
                { // set the headers
                    m.setRequestHeader("User-Agent",
                            "Mozilla/5.0 (Windows NT 5.1; rv:8.0.1) Gecko/20100101 Firefox/8.0.1");
                    m.setRequestHeader("Accept",
                            "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
                    m.setRequestHeader("Accept-Language", "en-us,zh-cn;q=0.5");
                    m.setRequestHeader("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
                    m.setRequestHeader("Referer", url);
                }
                client.executeMethod(m);
                //check status
                int sc = m.getStatusCode();
                d.setStatusCode(sc);

                if (isBadStatusCode(sc)) {
                    logger.error("download failed,url:" + d.getUrl() + ",Status Code:" + sc);
                    d.setStatus(Status.FAILED);
                    d.setDescription(m.getStatusText());
                    return;
                } else if (sc == 404 || sc == 410) {
                    d.setStatus(Status.FINISHED);
                    d.setDescription("NOT FOUND");
                    return;
                }

                long size = m.getResponseContentLength();
                d.setFileSize(size);

                // get File Name
                if (d.getFileName() == null) {
                    Header h = m.getResponseHeader("Content-Disposition");
                    String fileName = null;
                    if (h != null) {
                        String f = h.getValue();
                        int tag = f.indexOf("filename=");
                        if (tag != -1 && tag != f.length() - 1)
                            fileName = f.substring(tag + 1);
                    }

                    if (fileName == null || fileName.length() == 0) {
                        int tag1 = url.lastIndexOf("/");
                        int tag2 = url.lastIndexOf("?");
                        if (tag1 != -1 && tag1 != url.length() - 1) {
                            if (tag2 > tag1) {
                                fileName = url.substring(tag1 + 1, tag2);
                            } else {
                                fileName = url.substring(tag1 + 1);
                            }
                        }
                    }
                    d.setFileName(fileName);
                }

                // set the all headers
                Header[] headers = m.getResponseHeaders();
                if (headers != null) {
                    for (Header header : headers) {
                        d.addResponseHeader(header.getName(), header.getValue());
                    }
                }
                d.setStatus(Status.RUNNING);
                // recv the body
                if (handler == null) {
                    byte[] content = m.getResponseBody();
                    int len = content.length;
                    d.setFileSize(len);
                    d.setReceived(len);
                    d.setResponseCharset(m.getResponseCharSet());
                    d.setResponseBody(content);
                } else {
                    InputStream is = m.getResponseBodyAsStream();
                    handler.start(d);
                    byte[] buffer = new byte[102400];
                    long count = 0;
                    while (true) {
                        int r = is.read(buffer);
                        if (r > 0) {
                            count += r;
                            d.setReceived(count);
                            handler.handle(buffer, r);
                        } else {
                            break;
                        }
                    }
                    is.close();
                }
                d.setStatus(Status.FINISHED);
            } catch (Exception e) {
                logger.error("download failed,url:" + d.getUrl(), e);
                d.setStatus(Status.FAILED);
                d.setDescription(e.getMessage());
            } finally {
                m.releaseConnection();
                if (handler != null) {
                    handler.stop();
                }
                downloadings.remove(d);
                d.setEndTime(System.currentTimeMillis());
                while (downloaded.size() >= maxDownloadedCount) {
                    downloaded.poll();
                }
                downloaded.offer(d);
                task.downloadFininshed(d);
            }
        }
    };
}

From source file:com.day.cq.wcm.foundation.impl.Rewriter.java

/**
 * Process a page./*from   w ww .  ja  v a 2 s  .co m*/
 */
public void rewrite(HttpServletRequest request, HttpServletResponse response) throws IOException {

    try {
        targetURL = new URI(target);
    } catch (URISyntaxException e) {
        IOException ioe = new IOException("Bad URI syntax: " + target);
        ioe.initCause(e);
        throw ioe;
    }
    setHostPrefix(targetURL);

    HttpClient httpClient = new HttpClient();
    HttpState httpState = new HttpState();
    HostConfiguration hostConfig = new HostConfiguration();
    HttpMethodBase httpMethod;

    // define host
    hostConfig.setHost(targetURL.getHost(), targetURL.getPort());

    // create http method
    String method = (String) request.getAttribute("cq.ext.app.method");
    if (method == null) {
        method = request.getMethod();
    }
    method = method.toUpperCase();
    boolean isPost = "POST".equals(method);
    String urlString = targetURL.getPath();
    StringBuffer query = new StringBuffer();
    if (targetURL.getQuery() != null) {
        query.append("?");
        query.append(targetURL.getQuery());
    }
    //------------ GET ---------------
    if ("GET".equals(method)) {
        // add internal props
        Iterator<String> iter = extraParams.keySet().iterator();
        while (iter.hasNext()) {
            String name = iter.next();
            String value = extraParams.get(name);
            if (query.length() == 0) {
                query.append("?");
            } else {
                query.append("&");
            }
            query.append(Text.escape(name));
            query.append("=");
            query.append(Text.escape(value));
        }
        if (passInput) {
            // add request params
            @SuppressWarnings("unchecked")
            Enumeration<String> e = request.getParameterNames();
            while (e.hasMoreElements()) {
                String name = e.nextElement();
                if (targetParamName.equals(name)) {
                    continue;
                }
                String[] values = request.getParameterValues(name);
                for (int i = 0; i < values.length; i++) {
                    if (query.length() == 0) {
                        query.append("?");
                    } else {
                        query.append("&");
                    }
                    query.append(Text.escape(name));
                    query.append("=");
                    query.append(Text.escape(values[i]));
                }
            }

        }
        httpMethod = new GetMethod(urlString + query);
        //------------ POST ---------------
    } else if ("POST".equals(method)) {
        PostMethod m = new PostMethod(urlString + query);
        httpMethod = m;
        String contentType = request.getContentType();
        boolean mp = contentType != null && contentType.toLowerCase().startsWith("multipart/");
        if (mp) {
            //------------ MULTPART POST ---------------
            List<Part> parts = new LinkedList<Part>();
            Iterator<String> iter = extraParams.keySet().iterator();
            while (iter.hasNext()) {
                String name = iter.next();
                String value = extraParams.get(name);
                parts.add(new StringPart(name, value));
            }
            if (passInput) {
                // add request params
                @SuppressWarnings("unchecked")
                Enumeration<String> e = request.getParameterNames();
                while (e.hasMoreElements()) {
                    String name = e.nextElement();
                    if (targetParamName.equals(name)) {
                        continue;
                    }
                    String[] values = request.getParameterValues(name);
                    for (int i = 0; i < values.length; i++) {
                        parts.add(new StringPart(name, values[i]));
                    }
                }
            }
            m.setRequestEntity(
                    new MultipartRequestEntity(parts.toArray(new Part[parts.size()]), m.getParams()));
        } else {
            //------------ NORMAL POST ---------------
            // add internal props
            Iterator<String> iter = extraParams.keySet().iterator();
            while (iter.hasNext()) {
                String name = iter.next();
                String value = extraParams.get(name);
                m.addParameter(name, value);
            }
            if (passInput) {
                // add request params
                @SuppressWarnings("unchecked")
                Enumeration e = request.getParameterNames();
                while (e.hasMoreElements()) {
                    String name = (String) e.nextElement();
                    if (targetParamName.equals(name)) {
                        continue;
                    }
                    String[] values = request.getParameterValues(name);
                    for (int i = 0; i < values.length; i++) {
                        m.addParameter(name, values[i]);
                    }
                }
            }
        }
    } else {
        log.error("Unsupported method ''{0}''", method);
        throw new IOException("Unsupported http method " + method);
    }
    log.debug("created http connection for method {0} to {1}", method, urlString + query);

    // add some request headers
    httpMethod.addRequestHeader("User-Agent", request.getHeader("User-Agent"));
    httpMethod.setFollowRedirects(!isPost);
    httpMethod.getParams().setSoTimeout(soTimeout);
    httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(connectionTimeout);

    // send request
    httpClient.executeMethod(hostConfig, httpMethod, httpState);
    String contentType = httpMethod.getResponseHeader("Content-Type").getValue();

    log.debug("External app responded: {0}", httpMethod.getStatusLine());
    log.debug("External app contenttype: {0}", contentType);

    // check response code
    int statusCode = httpMethod.getStatusCode();
    if (statusCode >= HttpURLConnection.HTTP_BAD_REQUEST) {
        PrintWriter writer = response.getWriter();
        writer.println("External application returned status code: " + statusCode);
        return;
    } else if (statusCode == HttpURLConnection.HTTP_MOVED_TEMP
            || statusCode == HttpURLConnection.HTTP_MOVED_PERM) {
        String location = httpMethod.getResponseHeader("Location").getValue();
        if (location == null) {
            response.sendError(HttpURLConnection.HTTP_NOT_FOUND);
            return;
        }
        response.sendRedirect(rewriteURL(location, false));
        return;
    }

    // open input stream
    InputStream in = httpMethod.getResponseBodyAsStream();

    // check content type
    if (contentType != null && contentType.startsWith("text/html")) {
        rewriteHtml(in, contentType, response);
    } else {
        // binary mode
        if (contentType != null) {
            response.setContentType(contentType);
        }
        OutputStream outs = response.getOutputStream();

        try {
            byte buf[] = new byte[8192];
            int len;

            while ((len = in.read(buf)) != -1) {
                outs.write(buf, 0, len);
            }
        } finally {
            if (in != null) {
                in.close();
            }
        }
    }
}

From source file:org.apache.asterix.test.aql.TestsUtils.java

public static InputStream executeQuery(String str, OutputFormat fmt) throws Exception {
    final String url = "http://localhost:19002/query";

    HttpMethodBase method = null;
    if (str.length() + url.length() < MAX_URL_LENGTH) {
        //Use GET for small-ish queries
        method = new GetMethod(url);
        method.setQueryString(new NameValuePair[] { new NameValuePair("query", str) });
    } else {/*from ww  w.j av  a  2 s .  co m*/
        //Use POST for bigger ones to avoid 413 FULL_HEAD
        method = new PostMethod(url);
        ((PostMethod) method).setRequestEntity(new StringRequestEntity(str));
    }

    //Set accepted output response type
    method.setRequestHeader("Accept", fmt.mimeType());
    // Provide custom retry handler is necessary
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(3, false));
    executeHttpMethod(method);
    return method.getResponseBodyAsStream();
}

From source file:org.apache.axis2.transport.http.AbstractHTTPSender.java

protected void processResponse(HttpMethodBase httpMethod, MessageContext msgContext) throws IOException {
    obtainHTTPHeaderInformation(httpMethod, msgContext);

    InputStream in = httpMethod.getResponseBodyAsStream();
    if (in == null) {
        throw new AxisFault(Messages.getMessage("canNotBeNull", "InputStream"));
    }//from ww w. j a va 2  s .co m
    Header contentEncoding = httpMethod.getResponseHeader(HTTPConstants.HEADER_CONTENT_ENCODING);
    if (contentEncoding != null) {
        if (contentEncoding.getValue().equalsIgnoreCase(HTTPConstants.COMPRESSION_GZIP)) {
            in = new GZIPInputStream(in);
            // If the content-encoding is identity we can basically ignore it.
        } else if (!"identity".equalsIgnoreCase(contentEncoding.getValue())) {
            throw new AxisFault(
                    "HTTP :" + "unsupported content-encoding of '" + contentEncoding.getValue() + "' found");
        }
    }

    OperationContext opContext = msgContext.getOperationContext();
    if (opContext != null) {
        opContext.setProperty(MessageContext.TRANSPORT_IN, in);
    }
}

From source file:org.apache.axis2.transport.http.impl.httpclient3.HTTPSenderImpl.java

protected void processResponse(HttpMethodBase httpMethod, MessageContext msgContext) throws IOException {
    obtainHTTPHeaderInformation(httpMethod, msgContext);

    InputStream in = httpMethod.getResponseBodyAsStream();
    if (in == null) {
        throw new AxisFault(Messages.getMessage("canNotBeNull", "InputStream"));
    }/* w  w  w .  jav a2  s . com*/
    Header contentEncoding = httpMethod.getResponseHeader(HTTPConstants.HEADER_CONTENT_ENCODING);
    if (contentEncoding != null) {
        if (contentEncoding.getValue().equalsIgnoreCase(HTTPConstants.COMPRESSION_GZIP)) {
            in = new GZIPInputStream(in);
            // If the content-encoding is identity we can basically ignore
            // it.
        } else if (!"identity".equalsIgnoreCase(contentEncoding.getValue())) {
            throw new AxisFault(
                    "HTTP :" + "unsupported content-encoding of '" + contentEncoding.getValue() + "' found");
        }
    }

    OperationContext opContext = msgContext.getOperationContext();
    if (opContext != null) {
        opContext.setProperty(MessageContext.TRANSPORT_IN, in);
    }
}