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

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

Introduction

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

Prototype

@Override
public Header getResponseHeader(String headerName) 

Source Link

Document

Gets the response header associated with the given name.

Usage

From source file:net.sf.antcontrib.net.httpclient.AbstractMethodTask.java

public void execute() throws BuildException {
    if (httpClient == null) {
        httpClient = new HttpClient();
    }/*from  w w  w . ja  va  2s.  c o  m*/

    HttpMethodBase method = createMethodIfNecessary();
    configureMethod(method);
    try {
        int statusCode = httpClient.executeMethod(method);
        if (statusCodeProperty != null) {
            Property p = (Property) getProject().createTask("property");
            p.setName(statusCodeProperty);
            p.setValue(String.valueOf(statusCode));
            p.perform();
        }

        Iterator it = responseHeaders.iterator();
        while (it.hasNext()) {
            ResponseHeader header = (ResponseHeader) it.next();
            Property p = (Property) getProject().createTask("property");
            p.setName(header.getProperty());
            Header h = method.getResponseHeader(header.getName());
            if (h != null && h.getValue() != null) {
                p.setValue(h.getValue());
                p.perform();
            }

        }
        if (responseDataProperty != null) {
            Property p = (Property) getProject().createTask("property");
            p.setName(responseDataProperty);
            p.setValue(method.getResponseBodyAsString());
            p.perform();
        } else if (responseDataFile != null) {
            FileOutputStream fos = null;
            InputStream is = null;
            try {
                is = method.getResponseBodyAsStream();
                fos = new FileOutputStream(responseDataFile);
                byte buf[] = new byte[10 * 1024];
                int read = 0;
                while ((read = is.read(buf, 0, 10 * 1024)) != -1) {
                    fos.write(buf, 0, read);
                }
            } finally {
                FileUtils.close(fos);
                FileUtils.close(is);
            }
        }
    } catch (IOException e) {
        throw new BuildException(e);
    } finally {
        cleanupResources(method);
    }
}

From source file:com.esri.gpt.framework.http.HttpClientRequest.java

/**
 * Determines basic information associted with an HTTP response.
 * <br/>For example: response status message, Content-Type, Content-Length
 * @param method the HttpMethod that was executed
 *//*from  w w w .  j  ava2  s  .  c  o m*/
private void determineResponseInfo(HttpMethodBase method) {
    this.getResponseInfo().setResponseMessage(method.getStatusText());
    this.getResponseInfo().setResponseHeaders(method.getResponseHeaders());
    Header contentTypeHeader = method.getResponseHeader("Content-Type");
    if (contentTypeHeader != null) {
        HeaderElement values[] = contentTypeHeader.getElements();
        // Expect only one header element to be there, no more, no less
        if (values.length == 1) {
            this.getResponseInfo().setContentType(values[0].getName());
            NameValuePair param = values[0].getParameterByName("charset");
            if (param != null) {
                // If invalid, an UnsupportedEncondingException will result
                this.getResponseInfo().setContentEncoding(param.getValue());
            }
        }
    }
    this.getResponseInfo().setContentLength(method.getResponseContentLength());
}

From source file:edu.ku.brc.util.WebStoreAttachmentMgr.java

private void updateServerTimeDelta(HttpMethodBase method) {
    try {/*  w  w w.ja v  a  2  s  .  c  o  m*/
        Header timestamp = method.getResponseHeader("X-Timestamp");
        if (timestamp == null)
            return;
        long serverTime = Long.parseLong(timestamp.getValue());
        serverTimeDelta = serverTime - getSystemTime();
    } catch (NumberFormatException e) {
        e.printStackTrace();
    }
}

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

private String getContentType(HttpMethodBase method) {
    Header contentTypeHeader = method.getResponseHeader("Content-Type");
    String contentType = getContentType(contentTypeHeader);
    if (contentType == null) {
        // This should never happen either...
        return "application/octet-stream";
    } else {/*from   w w  w  .ja  v  a2  s .c o m*/
        return contentType;
    }
}

From source file:jeeves.utils.XmlRequest.java

private Element doExecute(HttpMethodBase httpMethod) throws IOException, BadXmlResponseEx {
    config.setHost(host, port, Protocol.getProtocol(protocol));

    if (useProxy)
        config.setProxy(proxyHost, proxyPort);

    byte[] data = null;

    try {/*from   ww w  .j  a  va  2 s  .  c  o  m*/
        client.executeMethod(httpMethod);
        data = httpMethod.getResponseBody();

        // HttpClient is unable to automatically handle redirects of entity
        // enclosing methods such as POST and PUT.
        // Get the location header and run the request against it.
        String redirectLocation;
        Header locationHeader = httpMethod.getResponseHeader("location");
        if (locationHeader != null) {
            redirectLocation = locationHeader.getValue();
            httpMethod.setPath(redirectLocation);
            client.executeMethod(httpMethod);
            data = httpMethod.getResponseBody();
        }
        return Xml.loadStream(new ByteArrayInputStream(data));
    }

    catch (JDOMException e) {
        throw new BadXmlResponseEx(new String(data, "UTF8"));
    }

    finally {
        httpMethod.releaseConnection();

        sentData = getSentData(httpMethod);
        receivedData = getReceivedData(httpMethod, data);
    }
}

From source file:fedora.server.security.servletfilters.pubcookie.ConnectPubcookie.java

public final void connect(String urlString, Map requestParameters, Cookie[] requestCookies,
        String truststoreLocation, String truststorePassword) {
    log.debug(this.getClass().getName() + ".connect() " + " url==" + urlString + " requestParameters=="
            + requestParameters + " requestCookies==" + requestCookies);
    responseCookies2 = null;//w ww  .j  ava2s.  c  o  m
    URL url = null;
    try {
        url = new URL(urlString);
    } catch (MalformedURLException mue) {
        log.error(this.getClass().getName() + ".connect() " + "bad configured url==" + urlString);
    }

    if (urlString.startsWith("https:") && null != truststoreLocation && !"".equals(truststoreLocation)
            && null != truststorePassword && !"".equals(truststorePassword)) {
        log.debug("setting " + FilterPubcookie.TRUSTSTORE_LOCATION_KEY + " to " + truststoreLocation);
        System.setProperty(FilterPubcookie.TRUSTSTORE_LOCATION_KEY, truststoreLocation);
        log.debug("setting " + FilterPubcookie.TRUSTSTORE_PASSWORD_KEY + " to " + truststorePassword);
        System.setProperty(FilterPubcookie.TRUSTSTORE_PASSWORD_KEY, truststorePassword);

        log.debug("setting " + FilterPubcookie.KEYSTORE_LOCATION_KEY + " to " + truststoreLocation);
        System.setProperty(FilterPubcookie.KEYSTORE_LOCATION_KEY, truststoreLocation);
        log.debug("setting " + FilterPubcookie.KEYSTORE_PASSWORD_KEY + " to " + truststorePassword);
        System.setProperty(FilterPubcookie.KEYSTORE_PASSWORD_KEY, truststorePassword);

        System.setProperty("javax.net.debug", "ssl,handshake,data,trustmanager");

    } else {
        log.debug("DIAGNOSTIC urlString==" + urlString);
        log.debug("didn't set " + FilterPubcookie.TRUSTSTORE_LOCATION_KEY + " to " + truststoreLocation);
        log.debug("didn't set " + FilterPubcookie.TRUSTSTORE_PASSWORD_KEY + " to " + truststorePassword);
    }

    /*
     * log.debug("\n-a-"); Protocol easyhttps = null; try { easyhttps = new
     * Protocol("https", (ProtocolSocketFactory) new
     * EasySSLProtocolSocketFactory(), 443); } catch (Throwable t) {
     * log.debug(t); log.debug(t.getMessage()); if (t.getCause() != null)
     * log.debug(t.getCause().getMessage()); } log.debug("\n-b-");
     * Protocol.registerProtocol("https", easyhttps); log.debug("\n-c-");
     */

    HttpClient client = new HttpClient();
    log.debug(this.getClass().getName() + ".connect() " + " b4 calling setup");
    log.debug(this.getClass().getName() + ".connect() requestCookies==" + requestCookies);
    HttpMethodBase method = setup(client, url, requestParameters, requestCookies);
    log.debug(this.getClass().getName() + ".connect() " + " after calling setup");
    int statusCode = 0;
    try {
        log.debug(this.getClass().getName() + ".connect() " + " b4 calling executeMethod");
        client.executeMethod(method);
        log.debug(this.getClass().getName() + ".connect() " + " after calling executeMethod");
        statusCode = method.getStatusCode();
        log.debug(
                this.getClass().getName() + ".connect() " + "(with configured url) statusCode==" + statusCode);
    } catch (Exception e) {
        log.error(this.getClass().getName() + ".connect() " + "failed original connect, url==" + urlString);
        log.error(e);
        log.error(e.getMessage());
        if (e.getCause() != null) {
            log.error(e.getCause().getMessage());
        }
        e.printStackTrace();
    }

    log.debug(this.getClass().getName() + ".connect() " + " status code==" + statusCode);

    if (302 == statusCode) {
        Header redirectHeader = method.getResponseHeader("Location");
        if (redirectHeader != null) {
            String redirectString = redirectHeader.getValue();
            if (redirectString != null) {
                URL redirectURL = null;
                try {
                    redirectURL = new URL(redirectString);
                    method = setup(client, redirectURL, requestParameters, requestCookies);
                } catch (MalformedURLException mue) {
                    log.error(this.getClass().getName() + ".connect() " + "bad redirect, url==" + urlString);
                }
                statusCode = 0;
                try {
                    client.executeMethod(method);
                    statusCode = method.getStatusCode();
                    log.debug(this.getClass().getName() + ".connect() " + "(on redirect) statusCode=="
                            + statusCode);
                } catch (Exception e) {
                    log.error(this.getClass().getName() + ".connect() " + "failed redirect connect");
                }
            }
        }
    }
    if (statusCode == 200) { // this is either the original, non-302, status code or the status code after redirect
        log.debug(this.getClass().getName() + ".connect() " + "status code 200");
        String content = null;
        try {
            log.debug(this.getClass().getName() + ".connect() " + "b4 gRBAS()");
            content = method.getResponseBodyAsString();
            log.debug(this.getClass().getName() + ".connect() " + "after gRBAS() content==" + content);
        } catch (IOException e) {
            log.error(this.getClass().getName() + ".connect() " + "couldn't get content");
            return;
        }
        if (content == null) {
            log.error(this.getClass().getName() + ".connect() content==null");
            return;
        } else {
            log.debug(this.getClass().getName() + ".connect() content != null, about to new Tidy");
            Tidy tidy = null;
            try {
                tidy = new Tidy();
            } catch (Throwable t) {
                log.debug("new Tidy didn't");
                log.debug(t);
                log.debug(t.getMessage());
                if (t != null) {
                    log.debug(t.getCause().getMessage());
                }
            }
            log.debug(this.getClass().getName() + ".connect() after newing Tidy, tidy==" + tidy);
            byte[] inputBytes = content.getBytes();
            log.debug(this.getClass().getName() + ".connect() A1");
            ByteArrayInputStream inputStream = new ByteArrayInputStream(inputBytes);
            log.debug(this.getClass().getName() + ".connect() A2");
            responseDocument = tidy.parseDOM(inputStream, null); //use returned root node as only output
            log.debug(this.getClass().getName() + ".connect() A3");
        }
        log.debug(this.getClass().getName() + ".connect() " + "b4 getState()");
        HttpState state = client.getState();
        log.debug(this.getClass().getName() + ".connect() state==" + state);
        try {
            responseCookies2 = method.getRequestHeaders();
            log.debug(this.getClass().getName() + ".connect() just got headers");
            for (Header element : responseCookies2) {
                log.debug(this.getClass().getName() + ".connect() header==" + element);
            }
            responseCookies = state.getCookies();
            log.debug(this.getClass().getName() + ".connect() responseCookies==" + responseCookies);
        } catch (Throwable t) {
            log.error(this.getClass().getName() + ".connect() exception==" + t.getMessage());
            if (t.getCause() != null) {
                log.error(this.getClass().getName() + ".connect() cause==" + t.getCause().getMessage());
            }
        }
        completedFully = true;
        log.debug(this.getClass().getName() + ".connect() completedFully==" + completedFully);
    }
}

From source file:net.praqma.jenkins.rqm.request.RQMHttpClient.java

public int login() throws HttpException, IOException, GeneralSecurityException {
    log.finest("Register trusting ssl");
    registerTrustingSSL();/*from w w w . j a  v  a  2s . c  o  m*/

    GetMethod methodPart1 = new GetMethod(url.toString() + contextRoot + "/auth/authrequired");

    int response = executeMethod(methodPart1);

    followRedirects(methodPart1, response);

    GetMethod methodPart2 = new GetMethod(this.url.toString() + contextRoot + "/authenticated/identity");

    response = executeMethod(methodPart2);

    followRedirects(methodPart2, response);

    HttpMethodBase authenticationMethod = null;
    Header authenticateHeader = methodPart2.getResponseHeader("WWW-Authenticate"); //$NON-NLS-1$

    if ((response == HttpURLConnection.HTTP_UNAUTHORIZED) && (authenticateHeader != null)
            && (authenticateHeader.getValue().toLowerCase().indexOf("basic realm") == 0)) {

        super.getState().setCredentials(new AuthScope(host, port),
                new UsernamePasswordCredentials(getUsr(), getPassword()));

        String authMethod = this.url.toString() + "/authenticated/identity";
        authenticationMethod = new GetMethod(authMethod);
        response = super.executeMethod(methodPart2);
    } else {
        authenticationMethod = new PostMethod(this.url.toString() + contextRoot + "/j_security_check");
        NameValuePair[] nvps = new NameValuePair[2];
        nvps[0] = new NameValuePair("j_username", getUsr());
        nvps[1] = new NameValuePair("j_password", getPassword());
        ((PostMethod) (authenticationMethod)).addParameters(nvps);
        ((PostMethod) (authenticationMethod)).addRequestHeader("Content-Type",
                "application/x-www-form-urlencoded; charset=utf-8");
        response = executeMethod(authenticationMethod);
        Header location = authenticationMethod.getResponseHeader("X-com-ibm-team-repository-web-auth-msg");
        if (location != null && location.getValue().indexOf("authfailed") >= 0) {
            response = HttpURLConnection.HTTP_UNAUTHORIZED;
        }
    }

    if ((response != HttpURLConnection.HTTP_OK) && (response != HttpURLConnection.HTTP_MOVED_TEMP)) {
        if (response != HttpURLConnection.HTTP_UNAUTHORIZED) {
            String body = "";
            try {
                body = authenticationMethod.getResponseBodyAsString();
            } catch (Exception e) {
                log.severe(String.format("Failed to login, with response code %s %n Response body: %n %s",
                        response, body));
            }
            log.severe(String.format("Failed to login, with response code %s %n Response body: %n %s", response,
                    body));
        }
    } else {
        followRedirects(authenticationMethod, response);
        String methodText = String.format(
                "%s%s/service/com.ibm.team.repository.service.internal.webuiInitializer.IWebUIInitializerRestService/initializationData",
                url.toString(), contextRoot);
        GetMethod get3 = new GetMethod(methodText);
        response = executeMethod(get3);
        followRedirects(get3, response);
    }

    log.finest("Response was: " + response);
    return response;
}

From source file:com.progress.codeshare.esbservice.http.HTTPService.java

public void service(final XQServiceContext ctx) throws XQServiceException {

    try {/*from  w w w .j a  v a 2  s .c  o m*/
        final XQMessageFactory factory = ctx.getMessageFactory();

        final XQParameters params = ctx.getParameters();

        final int messagePart = params.getIntParameter(PARAM_MESSAGE_PART, XQConstants.PARAM_STRING);

        final String method = params.getParameter(PARAM_METHOD, XQConstants.PARAM_STRING);

        final String uri = params.getParameter(PARAM_URI, XQConstants.PARAM_STRING);

        while (ctx.hasNextIncoming()) {
            final XQEnvelope env = ctx.getNextIncoming();

            final XQMessage origMsg = env.getMessage();

            final XQMessage newMsg = factory.createMessage();

            final HttpClient client = new HttpClient();

            final Iterator headerIterator = origMsg.getHeaderNames();

            if (METHOD_DELETE.equals(method)) {
                final HttpMethodBase req = new DeleteMethod(uri);

                /*
                 * Copy all XQ headers and extract HTTP headers and
                 * parameters
                 */
                while (headerIterator.hasNext()) {
                    final String header = (String) headerIterator.next();

                    newMsg.setHeaderValue(header, origMsg.getHeaderValue(header));

                    final Matcher matcher = PATTERN_HEADER.matcher(header);

                    if (matcher.find())
                        req.addRequestHeader(matcher.group(1),
                                (String) origMsg.getHeaderValue(matcher.group()));

                }

                client.executeMethod(req);

                /* Transform all HTTP to XQ headers */
                final Header[] headers = req.getResponseHeaders();

                for (int i = 0; i < headers.length; i++)
                    newMsg.setHeaderValue(PREFIX_HEADER + headers[i].getName(), headers[i].getValue());

                final XQPart newPart = newMsg.createPart();

                newPart.setContentId("Result");

                newPart.setContent(new String(req.getResponseBody()),
                        req.getResponseHeader("Content-Type").getValue());

                newMsg.addPart(newPart);
            } else if (METHOD_GET.equals(method)) {
                final HttpMethodBase req = new GetMethod();

                final List paramList = new ArrayList();

                /*
                 * Copy all XQ headers and extract HTTP headers and
                 * parameters
                 */
                while (headerIterator.hasNext()) {
                    final String header = (String) headerIterator.next();

                    newMsg.setHeaderValue(header, origMsg.getHeaderValue(header));

                    final Matcher headerMatcher = PATTERN_HEADER.matcher(header);

                    if (headerMatcher.find()) {
                        req.addRequestHeader(headerMatcher.group(1),
                                (String) origMsg.getHeaderValue(headerMatcher.group()));

                        continue;
                    }

                    final Matcher paramMatcher = PATTERN_PARAM.matcher(header);

                    if (paramMatcher.find())
                        paramList.add(new NameValuePair(paramMatcher.group(1),
                                (String) origMsg.getHeaderValue(paramMatcher.group())));

                }

                req.setQueryString((NameValuePair[]) paramList.toArray(new NameValuePair[] {}));

                client.executeMethod(req);

                /* Transform all HTTP to XQ headers */
                final Header[] headers = req.getResponseHeaders();

                for (int i = 0; i < headers.length; i++)
                    newMsg.setHeaderValue(PREFIX_HEADER + headers[i].getName(), headers[i].getValue());

                final XQPart newPart = newMsg.createPart();

                newPart.setContentId("Result");

                newPart.setContent(new String(req.getResponseBody()),
                        req.getResponseHeader("Content-Type").getValue());

                newMsg.addPart(newPart);
            } else if (METHOD_POST.equals(method)) {
                final PostMethod req = new PostMethod(uri);

                /*
                 * Copy all XQ headers and extract HTTP headers and
                 * parameters
                 */
                while (headerIterator.hasNext()) {
                    final String header = (String) headerIterator.next();

                    newMsg.setHeaderValue(header, origMsg.getHeaderValue(header));

                    final Matcher headerMatcher = PATTERN_HEADER.matcher(header);

                    if (headerMatcher.find()) {
                        req.addRequestHeader(headerMatcher.group(1),
                                (String) origMsg.getHeaderValue(headerMatcher.group()));

                        continue;
                    }

                    final Matcher paramMatcher = PATTERN_PARAM.matcher(header);

                    if (paramMatcher.find())
                        req.addParameter(new NameValuePair(paramMatcher.group(1),
                                (String) origMsg.getHeaderValue(paramMatcher.group())));

                }

                final XQPart origPart = origMsg.getPart(messagePart);

                req.setRequestEntity(new StringRequestEntity((String) origPart.getContent(),
                        origPart.getContentType(), null));

                client.executeMethod(req);

                /* Transform all HTTP to XQ headers */
                final Header[] headers = req.getResponseHeaders();

                for (int i = 0; i < headers.length; i++)
                    newMsg.setHeaderValue(PREFIX_HEADER + headers[i].getName(), headers[i].getValue());

                final XQPart newPart = newMsg.createPart();

                newPart.setContentId("Result");

                newPart.setContent(new String(req.getResponseBody()),
                        req.getResponseHeader("Content-Type").getValue());

                newMsg.addPart(newPart);
            } else if (METHOD_PUT.equals(method)) {
                final EntityEnclosingMethod req = new PutMethod(uri);

                /* Copy all XQ headers and extract HTTP headers */
                while (headerIterator.hasNext()) {
                    final String header = (String) headerIterator.next();

                    newMsg.setHeaderValue(header, origMsg.getHeaderValue(header));

                    final Matcher matcher = PATTERN_HEADER.matcher(header);

                    if (matcher.find())
                        req.addRequestHeader(matcher.group(1),
                                (String) origMsg.getHeaderValue(matcher.group()));

                }

                final XQPart origPart = origMsg.getPart(messagePart);

                req.setRequestEntity(new StringRequestEntity((String) origPart.getContent(),
                        origPart.getContentType(), null));

                client.executeMethod(req);

                /* Transform all HTTP to XQ headers */
                final Header[] headers = req.getResponseHeaders();

                for (int i = 0; i < headers.length; i++)
                    newMsg.setHeaderValue(PREFIX_HEADER + headers[i].getName(), headers[i].getValue());

                final XQPart newPart = newMsg.createPart();

                newPart.setContentId("Result");

                newPart.setContent(new String(req.getResponseBody()),
                        req.getResponseHeader("Content-Type").getValue());

                newMsg.addPart(newPart);
            }

            env.setMessage(newMsg);

            final Iterator addressIterator = env.getAddresses();

            if (addressIterator.hasNext())
                ctx.addOutgoing(env);

        }

    } catch (final Exception e) {
        throw new XQServiceException(e);
    }

}

From source file:com.atlantbh.jmeter.plugins.oauth.OAuthSampler.java

@Override
public SampleResult sample() {
    HttpMethodBase httpMethod = null;
    HttpClient client = null;/*from   w w w  .j a  v  a  2  s. com*/
    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.setSamplerData(getRequestBody());
        res.setMonitor(isMonitor());
        res.sampleStart();

        String urlStr = getUrl().toString();
        log.debug("Start : sample " + urlStr);
        log.debug("method " + getMethod());

        httpMethod = createHttpMethod(getMethod(), urlStr);
        setDefaultRequestHeaders(httpMethod);
        client = setupConnection(getUrl(), httpMethod);
        if (httpMethod instanceof EntityEnclosingMethod) {
            ((EntityEnclosingMethod) httpMethod)
                    .setRequestEntity(new StringRequestEntity(getRequestBody(), "text/xml", "UTF-8"));
        }
        overrideHeaders(httpMethod, urlStr, getMethod());
        res.setRequestHeaders(getConnectionHeaders(httpMethod));

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

        instream = httpMethod.getResponseBodyAsStream();
        if (instream != null) {

            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()));
        }

        res.sampleEnd();

        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) {
            ct = h.getValue();
            res.setContentType(ct);
            res.setEncodingAndType(ct);
        }

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

        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) {
        res.sampleEnd();
        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.google.enterprise.connector.sharepoint.spiimpl.SPDocument.java

/**
 * For downloading the contents of the documents using its URL. Used with
 * content feeds only./*w ww. j  ava  2 s  .c o m*/
 *
 * @return {@link SPContent} containing the status, content type and
 *    content stream.
 * @throws RepositoryException
 */
@VisibleForTesting
SPContent downloadContents() throws RepositoryException {
    InputStream docContentStream = null;
    String docContentType = null;

    if (null == sharepointClientContext) {
        LOGGER.log(Level.SEVERE,
                "Failed to download document content because the connector context is not found!");
        return new SPContent(SPConstants.CONNECTIVITY_FAIL, docContentType, docContentStream);
    }
    LOGGER.config("Document URL [ " + contentDwnldURL + " is getting processed for contents");
    if (isEmptyDocument()) {
        LOGGER.config("Document URL [" + contentDwnldURL + "] is empty document");
        return new SPContent("empty", docContentType, docContentStream);
    }
    int responseCode = 0;
    boolean downloadContent = true;
    if (getFileSize() > 0 && sharepointClientContext.getTraversalContext() != null) {
        if (getFileSize() > sharepointClientContext.getTraversalContext().maxDocumentSize()) {
            // Set the flag to download content to be false so that no
            // content is downloaded as the CM itself will drop it.
            downloadContent = false;
            LOGGER.log(Level.WARNING,
                    "Dropping content of document : " + getUrl() + " with docId : " + docId + " of size "
                            + getFileSize() + " as it exceeds the allowed max document size "
                            + sharepointClientContext.getTraversalContext().maxDocumentSize());
        }
    }
    if (downloadContent) {
        final String docURL = Util.encodeURL(contentDwnldURL);
        final HttpMethodBase method;
        try {
            method = new GetMethod(docURL);
            responseCode = sharepointClientContext.checkConnectivity(docURL, method);
            if (responseCode != 200) {
                LOGGER.warning("Unable to get contents for document '" + getUrl()
                        + "'. Received the response code: " + responseCode);
                return new SPContent(Integer.toString(responseCode), responseCode, docContentType,
                        docContentStream);
            }

            InputStream contentStream = method.getResponseBodyAsStream();
            if (contentStream != null) {
                docContentStream = new FilterInputStream(contentStream) {
                    @Override
                    public void close() throws IOException {
                        try {
                            super.close();
                        } finally {
                            method.releaseConnection();
                        }
                    }
                };
            }
        } catch (Throwable t) {
            String msg = new StringBuffer("Unable to fetch contents from URL: ").append(url).toString();
            LOGGER.log(Level.WARNING, "Unable to fetch contents from URL: " + url, t);
            throw new RepositoryDocumentException(msg, t);
        }
        // checks if the give URL is for .msg file if true set the mimetype
        // directly to application/vnd.ms-outlook as mimetype returned by the
        // header is incorrect for .msg files
        if (!contentDwnldURL.endsWith(MSG_FILE_EXTENSION)) {
            final Header contentType = method.getResponseHeader(SPConstants.CONTENT_TYPE_HEADER);
            if (contentType != null) {
                docContentType = contentType.getValue();
            } else {
                LOGGER.info("The content type returned for doc : " + toString() + " is : null ");
            }
        } else {
            docContentType = MSG_FILE_MIMETYPE;
        }

        if (LOGGER.isLoggable(Level.FINEST)) {
            LOGGER.fine("The content type for doc : " + toString() + " is : " + docContentType);
        }

        if (sharepointClientContext.getTraversalContext() != null && docContentType != null) {
            // TODO : This is to be revisited later where a better
            // approach to skip documents or only content is
            // available
            int mimeTypeSupport = sharepointClientContext.getTraversalContext()
                    .mimeTypeSupportLevel(docContentType);
            if (mimeTypeSupport == 0) {
                docContentStream = null;
                LOGGER.log(Level.WARNING, "Dropping content of document : " + getUrl() + " with docId : "
                        + docId + " as the mimetype : " + docContentType + " is not supported");
            } else if (mimeTypeSupport < 0) {
                // Since the mimetype is in list of 'ignored' mimetype
                // list, mark it to be skipped from sending
                String msg = new StringBuffer("Skipping the document with docId : ").append(getDocId())
                        .append(" doc URL: ").append(getUrl())
                        .append(" as the mimetype is in the 'ignored' mimetypes list ").toString();
                // Log it to the excluded_url log
                sharepointClientContext.logExcludedURL(msg);
                throw new SkippedDocumentException(msg);
            }
        }
    }

    return new SPContent(SPConstants.CONNECTIVITY_SUCCESS, responseCode, docContentType, docContentStream);
}