Example usage for org.apache.commons.httpclient HttpMethod getResponseHeaders

List of usage examples for org.apache.commons.httpclient HttpMethod getResponseHeaders

Introduction

In this page you can find the example usage for org.apache.commons.httpclient HttpMethod getResponseHeaders.

Prototype

public abstract Header[] getResponseHeaders();

Source Link

Usage

From source file:arena.httpclient.commons.JakartaCommonsHttpClient.java

public HttpResponse get() throws IOException {
    NameValuePair params[] = new NameValuePair[0];
    if (this.requestParameters != null) {
        List<NameValuePair> paramList = new ArrayList<NameValuePair>();
        for (String key : this.requestParameters.keySet()) {
            String[] multipleValues = this.requestParameters.get(key);
            for (String value : multipleValues) {
                paramList.add(new NameValuePair(key, value));
            }// ww w . j a  v  a  2 s.com
        }
        params = paramList.toArray(new NameValuePair[paramList.size()]);
    }

    HttpMethod method = null;
    if (this.isPost) {
        method = new PostMethod(this.url);
        if (this.encoding != null) {
            method.getParams().setContentCharset(this.encoding);
        }
        ((PostMethod) method).setRequestBody(params);
    } else {
        String url = this.url;
        for (int n = 0; n < params.length; n++) {
            url = URLUtils.addParamToURL(url, params[n].getName(), params[n].getValue(), this.encoding, false);
        }
        method = new GetMethod(url);
        method.setFollowRedirects(true);
        if (this.encoding != null) {
            method.getParams().setContentCharset(this.encoding);
        }
    }

    HttpClient client = new HttpClient(this.manager);
    if (this.connectTimeout != null) {
        client.getHttpConnectionManager().getParams().setConnectionTimeout(this.connectTimeout.intValue());
    }
    if (this.readTimeout != null) {
        client.getHttpConnectionManager().getParams().setSoTimeout(this.readTimeout.intValue());
    }
    client.setState(this.state);
    try {
        int statusCode = client.executeMethod(method);
        return new HttpResponse(getResponseBytes(method), statusCode,
                buildHeaderMap(method.getResponseHeaders()));
    } finally {
        if (method != null) {
            method.releaseConnection();
        }
    }
}

From source file:com.sun.faban.driver.transport.hc3.ApacheHC3Transport.java

private void buildResponseHeaders(HttpMethod method) {
    Header[] respHeaders = method.getResponseHeaders();
    responseHeader = new LinkedHashMap<String, List<String>>(respHeaders.length);
    for (Header header : respHeaders) {
        String name = header.getName().toLowerCase();
        List<String> values = responseHeader.get(name);
        if (values == null) {
            values = new ArrayList<String>();
            responseHeader.put(name, values);
        }/*from   ww w.j  a  v  a 2  s. c o  m*/
        values.add(header.getValue());
    }
}

From source file:edu.unc.lib.dl.admin.controller.RESTProxyController.java

@RequestMapping(value = { "/services/rest", "/services/rest/*", "/services/rest/**/*" })
public final void proxyAjaxCall(HttpServletRequest request, HttpServletResponse response) throws IOException {
    log.debug("Prepending service url " + this.servicesUrl + " to " + request.getRequestURI());
    String url = request.getRequestURI().replaceFirst(".*/services/rest/?", this.servicesUrl);
    if (request.getQueryString() != null)
        url = url + "?" + request.getQueryString();

    OutputStreamWriter writer = new OutputStreamWriter(response.getOutputStream());
    HttpClient client = new HttpClient();
    HttpMethod method = null;
    try {/*from w  w  w .j  a  v  a2  s  . com*/
        log.debug("Proxying ajax request to services REST api via " + request.getMethod());
        // Split this according to the type of request
        if (request.getMethod().equals("GET")) {
            method = new GetMethod(url);
        } else if (request.getMethod().equals("POST")) {
            method = new PostMethod(url);
            // Set any eventual parameters that came with our original
            // request (POST params, for instance)
            Enumeration<String> paramNames = request.getParameterNames();
            while (paramNames.hasMoreElements()) {
                String paramName = paramNames.nextElement();
                ((PostMethod) method).setParameter(paramName, request.getParameter(paramName));
            }
        } else {
            throw new NotImplementedException("This proxy only supports GET and POST methods.");
        }

        // Forward the user's groups along with the request
        method.addRequestHeader(HttpClientUtil.SHIBBOLETH_GROUPS_HEADER, GroupsThreadStore.getGroupString());
        method.addRequestHeader("On-Behalf-Of", GroupsThreadStore.getUsername());

        // Execute the method
        client.executeMethod(method);

        // Set the content type, as it comes from the server
        Header[] headers = method.getResponseHeaders();
        for (Header header : headers) {
            if ("Content-Type".equalsIgnoreCase(header.getName())) {
                response.setContentType(header.getValue());
            }
        }
        try (InputStream responseStream = method.getResponseBodyAsStream()) {
            int b;
            while ((b = responseStream.read()) != -1) {
                response.getOutputStream().write(b);
            }
        }
        response.getOutputStream().flush();
    } catch (HttpException e) {
        writer.write(e.toString());
        throw e;
    } catch (IOException e) {
        e.printStackTrace();
        writer.write(e.toString());
        throw e;
    } finally {
        if (method != null)
            method.releaseConnection();
    }
}

From source file:com.zimbra.cs.servlet.ZimbraServlet.java

public static void proxyServletRequest(HttpServletRequest req, HttpServletResponse resp, HttpMethod method,
        HttpState state) throws IOException, ServiceException {
    // create an HTTP client with the same cookies
    javax.servlet.http.Cookie cookies[] = req.getCookies();
    String hostname = method.getURI().getHost();
    boolean hasZMAuth = hasZimbraAuthCookie(state);
    if (cookies != null) {
        for (int i = 0; i < cookies.length; i++) {
            if (cookies[i].getName().equals(ZimbraCookie.COOKIE_ZM_AUTH_TOKEN) && hasZMAuth)
                continue;
            state.addCookie(//w  w  w . j a  v a 2s . c om
                    new Cookie(hostname, cookies[i].getName(), cookies[i].getValue(), "/", null, false));
        }
    }
    HttpClient client = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
    if (state != null)
        client.setState(state);

    int hopcount = 0;
    for (Enumeration<?> enm = req.getHeaderNames(); enm.hasMoreElements();) {
        String hname = (String) enm.nextElement(), hlc = hname.toLowerCase();
        if (hlc.equals("x-zimbra-hopcount"))
            try {
                hopcount = Math.max(Integer.parseInt(req.getHeader(hname)), 0);
            } catch (NumberFormatException e) {
            }
        else if (hlc.startsWith("x-") || hlc.startsWith("content-") || hlc.equals("authorization"))
            method.addRequestHeader(hname, req.getHeader(hname));
    }
    if (hopcount >= MAX_PROXY_HOPCOUNT)
        throw ServiceException.TOO_MANY_HOPS(HttpUtil.getFullRequestURL(req));
    method.addRequestHeader("X-Zimbra-Hopcount", Integer.toString(hopcount + 1));
    if (method.getRequestHeader("X-Zimbra-Orig-Url") == null)
        method.addRequestHeader("X-Zimbra-Orig-Url", req.getRequestURL().toString());
    String ua = req.getHeader("User-Agent");
    if (ua != null)
        method.setRequestHeader("User-Agent", ua);

    // dispatch the request and copy over the results
    int statusCode = -1;
    for (int retryCount = 3; statusCode == -1 && retryCount > 0; retryCount--) {
        statusCode = HttpClientUtil.executeMethod(client, method);
    }
    if (statusCode == -1) {
        resp.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE, "retry limit reached");
        return;
    } else if (statusCode >= 300) {
        resp.sendError(statusCode, method.getStatusText());
        return;
    }

    Header[] headers = method.getResponseHeaders();
    for (int i = 0; i < headers.length; i++) {
        String hname = headers[i].getName(), hlc = hname.toLowerCase();
        if (hlc.startsWith("x-") || hlc.startsWith("content-") || hlc.startsWith("www-"))
            resp.addHeader(hname, headers[i].getValue());
    }
    InputStream responseStream = method.getResponseBodyAsStream();
    if (responseStream == null || resp.getOutputStream() == null)
        return;
    ByteUtil.copy(method.getResponseBodyAsStream(), false, resp.getOutputStream(), false);
}

From source file:com.zimbra.cs.dav.service.DavServlet.java

private boolean isProxyRequest(DavContext ctxt, DavMethod m)
        throws IOException, DavException, ServiceException {
    Provisioning prov = Provisioning.getInstance();
    ItemId target = null;/*  w w  w . ja v  a 2 s  . c  om*/
    String extraPath = null;
    String requestPath = ctxt.getPath();
    try {
        if (ctxt.getUser() == null) {
            return false;
        }
        if (requestPath == null || requestPath.length() < 2) {
            return false;
        }
        Account account = prov.getAccountByName(ctxt.getUser());
        if (account == null) {
            return false;
        }
        Mailbox mbox = MailboxManager.getInstance().getMailboxByAccount(account);
        Pair<Folder, String> match = mbox.getFolderByPathLongestMatch(ctxt.getOperationContext(),
                Mailbox.ID_FOLDER_USER_ROOT, requestPath);
        Folder targetFolder = match.getFirst();
        if (!(targetFolder instanceof Mountpoint)) {
            return false;
        }
        Mountpoint mp = (Mountpoint) targetFolder;
        target = new ItemId(mp.getOwnerId(), mp.getRemoteId());
        extraPath = match.getSecond();
    } catch (ServiceException e) {
        ZimbraLog.dav.debug("can't get path", e);
        return false;
    }

    // we don't proxy zero depth PROPFIND, and all PROPPATCH on mountpoints,
    // because the mountpoint object contains WebDAV properties that are
    // private to the user.
    // we also don't proxy DELETE on a mountpoint.
    if (extraPath == null && (m.getName().equals(PropFind.PROPFIND) && ctxt.getDepth() == DavContext.Depth.zero
            || m.getName().equals(PropPatch.PROPPATCH) || m.getName().equals(Delete.DELETE))) {
        return false;
    }

    String prefix = ctxt.getPath();
    if (extraPath != null) {
        prefix = prefix.substring(0, prefix.indexOf(extraPath));
    }
    prefix = HttpUtil.urlEscape(DAV_PATH + "/" + ctxt.getUser() + prefix);

    if (!prefix.endsWith("/")) {
        prefix += "/";
    }

    // make sure the target account exists.
    Account acct = prov.getAccountById(target.getAccountId());
    if (acct == null) {
        return false;
    }
    Server server = prov.getServer(acct);
    if (server == null) {
        return false;
    }

    // get the path to the target mail item
    AuthToken authToken = AuthProvider.getAuthToken(ctxt.getAuthAccount());
    ZMailbox.Options zoptions = new ZMailbox.Options(authToken.toZAuthToken(), AccountUtil.getSoapUri(acct));
    zoptions.setNoSession(true);
    zoptions.setTargetAccount(target.getAccountId());
    zoptions.setTargetAccountBy(Key.AccountBy.id);
    ZMailbox zmbx = ZMailbox.getMailbox(zoptions);
    ZFolder f = zmbx.getFolderById("" + target.toString());
    if (f == null) {
        return false;
    }
    String path = f.getPath();
    String newPrefix = HttpUtil.urlEscape(DAV_PATH + "/" + acct.getName() + f.getPath());

    if (ctxt.hasRequestMessage()) {
        // replace the path in <href> of the request with the path to the target mail item.
        Document req = ctxt.getRequestMessage();
        for (Object hrefObj : req.getRootElement().elements(DavElements.E_HREF)) {
            if (!(hrefObj instanceof Element)) {
                continue;
            }
            Element href = (Element) hrefObj;
            String v = href.getText();
            // prefix matching is not as straightforward as we have jetty redirect from /dav to /home/dav.
            href.setText(newPrefix + "/" + v.substring(v.lastIndexOf('/') + 1));
        }
    }

    // build proxy request
    String url = getProxyUrl(ctxt.getRequest(), server, DAV_PATH)
            + HttpUtil.urlEscape("/" + acct.getName() + path + "/" + (extraPath == null ? "" : extraPath));
    HttpState state = new HttpState();
    authToken.encode(state, false, server.getAttr(Provisioning.A_zimbraServiceHostname));
    HttpClient client = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
    client.setState(state);
    HttpMethod method = m.toHttpMethod(ctxt, url);
    method.setRequestHeader(new Header(DavProtocol.HEADER_USER_AGENT, "Zimbra-DAV/" + BuildInfo.VERSION));
    if (ZimbraLog.dav.isDebugEnabled()) {
        Enumeration<String> headers = ctxt.getRequest().getHeaderNames();
        while (headers.hasMoreElements()) {
            String hdr = headers.nextElement();
            if (!PROXY_REQUEST_HEADERS.contains(hdr) && !IGNORABLE_PROXY_REQUEST_HEADERS.contains(hdr)) {
                ZimbraLog.dav.debug(
                        "Dropping header(s) with name [%s] from proxy request (not in PROXY_REQUEST_HEADERS)",
                        hdr);
            }
        }
    }

    for (String h : PROXY_REQUEST_HEADERS) {
        String hval = ctxt.getRequest().getHeader(h);
        if (hval != null) {
            method.addRequestHeader(h, hval);
        }
    }
    int statusCode = HttpClientUtil.executeMethod(client, method);
    if (ZimbraLog.dav.isDebugEnabled()) {
        for (Header hval : method.getResponseHeaders()) {
            String hdrName = hval.getName();
            if (!PROXY_RESPONSE_HEADERS.contains(hdrName)
                    && !IGNORABLE_PROXY_RESPONSE_HEADERS.contains(hdrName)) {
                ZimbraLog.dav.debug("Dropping header [%s] from proxy response (not in PROXY_RESPONSE_HEADERS)",
                        hval);
            }
        }
    }

    for (String h : PROXY_RESPONSE_HEADERS) {
        for (Header hval : method.getResponseHeaders(h)) {
            String hdrValue = hval.getValue();
            if (DavProtocol.HEADER_LOCATION.equals(h)) {
                int pfxLastSlashPos = prefix.lastIndexOf('/');
                int lastSlashPos = hdrValue.lastIndexOf('/');
                if ((lastSlashPos > 0) && (pfxLastSlashPos > 0)) {
                    hdrValue = prefix.substring(0, pfxLastSlashPos) + hdrValue.substring(lastSlashPos);
                    ZimbraLog.dav.debug("Original [%s] from proxy response new value '%s'", hval, hdrValue);
                }
            }
            ctxt.getResponse().addHeader(h, hdrValue);
        }
    }

    ctxt.getResponse().setStatus(statusCode);
    ctxt.setStatus(statusCode);
    try (InputStream in = method.getResponseBodyAsStream()) {
        switch (statusCode) {
        case DavProtocol.STATUS_MULTI_STATUS:
            // rewrite the <href> element in the response to point to local mountpoint.
            try {
                Document response = W3cDomUtil.parseXMLToDom4jDocUsingSecureProcessing(in);
                Element top = response.getRootElement();
                for (Object responseObj : top.elements(DavElements.E_RESPONSE)) {
                    if (!(responseObj instanceof Element)) {
                        continue;
                    }
                    Element href = ((Element) responseObj).element(DavElements.E_HREF);
                    String v = href.getText();
                    v = URLDecoder.decode(v);
                    // Bug:106438, because v contains URL encoded value(%40) for '@' the comparison fails
                    if (v.startsWith(newPrefix)) {
                        href.setText(prefix + v.substring(newPrefix.length() + 1));
                    }
                }
                if (ZimbraLog.dav.isDebugEnabled()) {
                    ZimbraLog.dav.debug("PROXY RESPONSE:\n%s", new String(DomUtil.getBytes(response), "UTF-8"));
                }
                DomUtil.writeDocumentToStream(response, ctxt.getResponse().getOutputStream());
                ctxt.responseSent();
            } catch (XmlParseException e) {
                ZimbraLog.dav.warn("proxy request failed", e);
                return false;
            }
            break;
        default:
            if (in != null) {
                ByteUtil.copy(in, true, ctxt.getResponse().getOutputStream(), false);
            }
            ctxt.responseSent();
            break;
        }
        return true;
    }
}

From source file:ch.ksfx.web.services.spidering.http.WebEngine.java

public void loadResource(Resource resource) {
    HttpMethod httpMethod;

    try {//from   w  w w. j  a v  a 2  s.co  m
        if (/*resource.getHttpMethod().equals(GET)*/true) {
            String url = resource.getUrl();

            if (url != null) {
                url = url.replaceAll("&amp;", "&");
                url = url.replaceAll("&quot;", "\"");
            }

            httpMethod = this.httpClientHelper.executeGetMethod(url);
        } else {
            //TODO implement POST functionality
            /*
            NameValuePair[] nameValuePairs = new NameValuePair[resource.getPostData().size()];
            for(int i = 0; i < resource.getPostData().size(); i++) {
            nameValuePairs[i] = new NameValuePair(resource.getPostData().get(i).getName(),
                                                  resource.getPostData().get(i).getValue());
            }
                    
            String url = resource.getURL().toString();
                    
            if (url != null) {
               url = url.replaceAll("&amp;","&");
               url = url.replaceAll("&quot;","\"");
            }
                    
            httpMethod = this.httpClientHelper.executePostMethod(url,
                                                             nameValuePairs);
             */
        }

    } catch (Exception e) {
        resource.setLoadSucceed(false);
        resource.setHttpStatusCode(222);

        logger.log(Level.SEVERE, "Unable to load resource", e);
        return;
    }

    if (httpMethod == null) {
        resource.setLoadSucceed(false);
        return;
    }

    if (httpMethod.getStatusCode() != HttpStatus.SC_OK) {
        try {
            resource.setUrl(httpMethod.getURI().toString());

            for (Header header : httpMethod.getResponseHeaders()) {
                resource.addResponseHeader(new ResponseHeader(header.getName(), header.getValue()));
            }
            resource.setHttpStatusCode(httpMethod.getStatusCode());
        } catch (Exception e) {
            logger.warning(e.getMessage());
            e.printStackTrace();
        }

        return;
    }

    try {
        if (httpMethod.getResponseHeader("Content-Encoding") != null
                && httpMethod.getResponseHeader("Content-Encoding").getValue().contains("gzip")) {
            BufferedInputStream in = new BufferedInputStream(
                    new GZIPInputStream(httpMethod.getResponseBodyAsStream()));
            ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
            byte[] buffer = new byte[1024];
            int length;
            while ((length = in.read(buffer)) >= 0) {
                out.write(buffer, 0, length);
            }

            resource.setRawContent(out.toByteArray());
            resource.setSize(new Long(out.toByteArray().length));
        } else {
            BufferedInputStream in = new BufferedInputStream(httpMethod.getResponseBodyAsStream());
            ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
            byte[] buffer = new byte[1024];
            int length;
            while ((length = in.read(buffer)) >= 0) {
                out.write(buffer, 0, length);
            }

            resource.setRawContent(out.toByteArray());
            resource.setSize(new Long(out.toByteArray().length));
        }

        resource.setHttpStatusCode(httpMethod.getStatusCode());
        resource.setLoadSucceed(true);

        resource.setMimeType(recognizeMimeType(resource, httpMethod));
        if (resource.getMimeType().startsWith("text") || resource.getMimeType().equals("application/json")) {

            resource.setContent(EncodingHelper.encode(resource.getRawContent(), resource.getEncoding(),
                    ((HttpMethodBase) httpMethod).getResponseCharSet()));
        } else {
            resource.setIsBinary(true);
        }

    } catch (IOException e) {
        e.printStackTrace();
        logger.log(Level.SEVERE, "Unable to load resource", e);
    } finally {
        httpMethod.releaseConnection();
    }
}

From source file:com.intuit.tank.http.BaseRequest.java

public void sendRequest(BaseResponse response, @Nonnull HttpMethod method, String requestBody) {
    String uri = null;/*from ww  w  .  j av a  2 s  . c  om*/
    long waitTime = 0L;
    try {
        this.response = response;
        uri = method.getURI().toString();
        logger.debug(
                LogUtil.getLogMessage("About to POST request to " + uri + " with requestBody  " + requestBody,
                        LogEventType.Informational));
        logRequest(uri, requestBody, method.getName(), headerInformation, httpclient, false);
        BaseRequestHandler.setHeaders(method, headerInformation);
        long startTime = System.currentTimeMillis();
        timestamp = new Date(startTime);
        httpclient.executeMethod(method);

        // read response body
        byte[] responseBody = new byte[0];
        // check for no content headers
        if (method.getStatusCode() != 203 && method.getStatusCode() != 202 && method.getStatusCode() != 204) {
            try {
                InputStream httpInputStream = method.getResponseBodyAsStream();
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                int curByte = httpInputStream.read();
                while (curByte >= 0) {
                    out.write(curByte);
                    curByte = httpInputStream.read();
                }
                responseBody = out.toByteArray();
            } catch (Exception e) {
                logger.warn("could not get response body: " + e);
            }
        }
        long endTime = System.currentTimeMillis();
        BaseRequestHandler.processResponse(responseBody, startTime, endTime, response, method.getStatusText(),
                method.getStatusCode(), method.getResponseHeaders(), httpclient.getState());
        waitTime = endTime - startTime;
    } catch (Exception ex) {
        logger.error(LogUtil.getLogMessage(
                "Could not do " + method.getName() + " to url " + uri + " |  error: " + ex.toString(),
                LogEventType.IO), ex);
        throw new RuntimeException(ex);
    } finally {
        method.releaseConnection();
    }
    if (waitTime != 0) {
        doWaitDueToLongResponse(waitTime, uri);
    }
}

From source file:com.boyuanitsm.pay.alipay.util.httpClient.HttpProtocolHandler.java

/**
 * Http//  w ww . j av  a 2  s . co  m
 * 
 * @param request ?
 * @param strParaFileName ???
 * @param strFilePath 
 * @return 
 * @throws HttpException, IOException 
 */
public HttpResponse execute(HttpRequest request, String strParaFileName, String strFilePath)
        throws HttpException, IOException {
    HttpClient httpclient = new HttpClient(connectionManager);

    // 
    int connectionTimeout = defaultConnectionTimeout;
    if (request.getConnectionTimeout() > 0) {
        connectionTimeout = request.getConnectionTimeout();
    }
    httpclient.getHttpConnectionManager().getParams().setConnectionTimeout(connectionTimeout);

    // 
    int soTimeout = defaultSoTimeout;
    if (request.getTimeout() > 0) {
        soTimeout = request.getTimeout();
    }
    httpclient.getHttpConnectionManager().getParams().setSoTimeout(soTimeout);

    // ConnectionManagerconnection
    httpclient.getParams().setConnectionManagerTimeout(defaultHttpConnectionManagerTimeout);

    String charset = request.getCharset();
    charset = charset == null ? DEFAULT_CHARSET : charset;
    HttpMethod method = null;

    //get??
    if (request.getMethod().equals(HttpRequest.METHOD_GET)) {
        method = new GetMethod(request.getUrl());
        method.getParams().setCredentialCharset(charset);

        // parseNotifyConfig??GETrequestQueryString
        method.setQueryString(request.getQueryString());
    } else if (strParaFileName.equals("") && strFilePath.equals("")) {
        //post??
        method = new PostMethod(request.getUrl());
        ((PostMethod) method).addParameters(request.getParameters());
        method.addRequestHeader("Content-Type",
                "application/x-www-form-urlencoded; text/html; charset=" + charset);
    } else {
        //post?
        method = new PostMethod(request.getUrl());
        List<Part> parts = new ArrayList<Part>();
        for (int i = 0; i < request.getParameters().length; i++) {
            parts.add(new StringPart(request.getParameters()[i].getName(),
                    request.getParameters()[i].getValue(), charset));
        }
        //?strParaFileName???
        parts.add(new FilePart(strParaFileName, new FilePartSource(new File(strFilePath))));

        // 
        ((PostMethod) method).setRequestEntity(
                new MultipartRequestEntity(parts.toArray(new Part[0]), new HttpMethodParams()));
    }

    // Http HeaderUser-Agent
    method.addRequestHeader("User-Agent", "Mozilla/4.0");
    HttpResponse response = new HttpResponse();

    try {
        httpclient.executeMethod(method);
        if (request.getResultType().equals(HttpResultType.STRING)) {
            response.setStringResult(method.getResponseBodyAsString());
        } else if (request.getResultType().equals(HttpResultType.BYTES)) {
            response.setByteResult(method.getResponseBody());
        }
        response.setResponseHeaders(method.getResponseHeaders());
    } catch (UnknownHostException ex) {

        return null;
    } catch (IOException ex) {

        return null;
    } catch (Exception ex) {

        return null;
    } finally {
        method.releaseConnection();
    }
    return response;
}

From source file:com.mirth.connect.connectors.http.HttpMessageDispatcher.java

private void submitHttpRequest(String address, MessageObject mo) throws Exception {
    HttpMethod httpMethod = null;

    try {// w w w.ja va2  s.  c om
        httpMethod = buildHttpRequest(replacer.replaceValues(address, mo), mo);

        // authentication

        if (connector.isDispatcherUseAuthentication()) {
            List<String> authenticationPreferences = new ArrayList<String>();

            if ("Digest".equalsIgnoreCase(connector.getDispatcherAuthenticationType())) {
                authenticationPreferences.add(AuthPolicy.DIGEST);
                logger.debug("using Digest authentication");
            } else {
                authenticationPreferences.add(AuthPolicy.BASIC);
                logger.debug("using Basic authentication");
            }

            client.getParams().setAuthenticationPreemptive(true);
            client.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authenticationPreferences);
            Credentials credentials = new UsernamePasswordCredentials(
                    replacer.replaceValues(connector.getDispatcherUsername(), mo),
                    replacer.replaceValues(connector.getDispatcherPassword(), mo));
            client.getState().setCredentials(
                    new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM), credentials);
            logger.debug("using authentication with credentials: " + credentials);
        }

        client.getParams().setSoTimeout(
                NumberUtils.toInt(replacer.replaceValues(connector.getDispatcherSocketTimeout()), 30000));

        // execute the method
        logger.debug(
                "executing method: type=" + httpMethod.getName() + ", uri=" + httpMethod.getURI().toString());
        int statusCode = client.executeMethod(httpMethod);
        logger.debug("received status code: " + statusCode);

        String response = null;

        if (connector.isDispatcherIncludeHeadersInResponse()) {
            HttpMessageConverter converter = new HttpMessageConverter();
            response = converter.httpResponseToXml(httpMethod.getStatusLine().toString(),
                    httpMethod.getResponseHeaders(), httpMethod.getResponseBodyAsString());
        } else {
            response = httpMethod.getResponseBodyAsString();
        }

        if (statusCode < HttpStatus.SC_BAD_REQUEST) {
            messageObjectController.setSuccess(mo, response, null);

            // send to reply channel
            if ((connector.getDispatcherReplyChannelId() != null)
                    && !connector.getDispatcherReplyChannelId().equals("sink")) {
                new VMRouter().routeMessageByChannelId(connector.getDispatcherReplyChannelId(), response, true);
            }
        } else {
            alertController.sendAlerts(connector.getChannelId(), Constants.ERROR_404,
                    "Received error response from HTTP server.", null);
            messageObjectController.setError(mo, Constants.ERROR_404, response, null, null);
        }
    } catch (Exception e) {
        throw e;
    } finally {
        if (httpMethod != null) {
            httpMethod.releaseConnection();
        }
    }
}

From source file:io.hops.hopsworks.api.jobs.JobService.java

/**
 * Get the job ui for the specified job.
 * This act as a proxy to get the job ui from yarn
 * <p>//  w ww .  j a  v  a  2s  . c  o  m
 * @param appId
 * @param param
 * @param sc
 * @param req
 * @return
 */
@GET
@Path("/{appId}/prox/{path: .+}")
@Produces(MediaType.WILDCARD)
@AllowedProjectRoles({ AllowedProjectRoles.DATA_OWNER, AllowedProjectRoles.DATA_SCIENTIST })
public Response getProxy(@PathParam("appId") final String appId, @PathParam("path") final String param,
        @Context SecurityContext sc, @Context HttpServletRequest req) {

    Response response = checkAccessRight(appId);
    if (response != null) {
        return response;
    }
    try {
        String trackingUrl;
        if (param.matches("http([a-zA-Z,:,/,.,0-9,-])+:([0-9])+(.)+")) {
            trackingUrl = param;
        } else {
            trackingUrl = "http://" + param;
        }
        trackingUrl = trackingUrl.replace("@hwqm", "?");
        if (!hasAppAccessRight(trackingUrl)) {
            LOGGER.log(Level.SEVERE, "A user is trying to access an app outside their project!");
            return Response.status(Response.Status.FORBIDDEN).build();
        }
        org.apache.commons.httpclient.URI uri = new org.apache.commons.httpclient.URI(trackingUrl, false);

        HttpClientParams params = new HttpClientParams();
        params.setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
        params.setBooleanParameter(HttpClientParams.ALLOW_CIRCULAR_REDIRECTS, true);
        HttpClient client = new HttpClient(params);

        final HttpMethod method = new GetMethod(uri.getEscapedURI());
        Enumeration<String> names = req.getHeaderNames();
        while (names.hasMoreElements()) {
            String name = names.nextElement();
            String value = req.getHeader(name);
            if (PASS_THROUGH_HEADERS.contains(name)) {
                //yarn does not send back the js if encoding is not accepted
                //but we don't want to accept encoding for the html because we
                //need to be able to parse it
                if (!name.toLowerCase().equals("accept-encoding") || trackingUrl.contains(".js")) {
                    method.setRequestHeader(name, value);
                }
            }
        }
        String user = req.getRemoteUser();
        if (user != null && !user.isEmpty()) {
            method.setRequestHeader("Cookie", PROXY_USER_COOKIE_NAME + "=" + URLEncoder.encode(user, "ASCII"));
        }

        client.executeMethod(method);
        Response.ResponseBuilder responseBuilder = noCacheResponse
                .getNoCacheResponseBuilder(Response.Status.OK);
        for (Header header : method.getResponseHeaders()) {
            responseBuilder.header(header.getName(), header.getValue());
        }
        //method.getPath().contains("/allexecutors") is needed to replace the links under Executors tab
        //which are in a json response object
        if (method.getResponseHeader("Content-Type") == null
                || method.getResponseHeader("Content-Type").getValue().contains("html")
                || method.getPath().contains("/allexecutors")) {
            final String source = "http://" + method.getURI().getHost() + ":" + method.getURI().getPort();
            if (method.getResponseHeader("Content-Length") == null) {
                responseBuilder.entity(new StreamingOutput() {
                    @Override
                    public void write(OutputStream out) throws IOException, WebApplicationException {
                        Writer writer = new BufferedWriter(new OutputStreamWriter(out));
                        InputStream stream = method.getResponseBodyAsStream();
                        Reader in = new InputStreamReader(stream, "UTF-8");
                        char[] buffer = new char[4 * 1024];
                        String remaining = "";
                        int n;
                        while ((n = in.read(buffer)) != -1) {
                            StringBuilder strb = new StringBuilder();
                            strb.append(buffer, 0, n);
                            String s = remaining + strb.toString();
                            remaining = s.substring(s.lastIndexOf(">") + 1, s.length());
                            s = hopify(s.substring(0, s.lastIndexOf(">") + 1), param, appId, source);
                            writer.write(s);
                        }
                        writer.flush();
                    }
                });
            } else {
                String s = hopify(method.getResponseBodyAsString(), param, appId, source);
                responseBuilder.entity(s);
                responseBuilder.header("Content-Length", s.length());
            }

        } else {
            responseBuilder.entity(new StreamingOutput() {
                @Override
                public void write(OutputStream out) throws IOException, WebApplicationException {
                    InputStream stream = method.getResponseBodyAsStream();
                    org.apache.hadoop.io.IOUtils.copyBytes(stream, out, 4096, true);
                    out.flush();
                }
            });
        }
        return responseBuilder.build();
    } catch (Exception e) {
        LOGGER.log(Level.SEVERE, "exception while geting job ui " + e.getLocalizedMessage(), e);
        return noCacheResponse.getNoCacheResponseBuilder(Response.Status.NOT_FOUND).build();
    }

}