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

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

Introduction

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

Prototype

public abstract void releaseConnection();

Source Link

Usage

From source file:gov.loc.ndmso.proxyfilter.RequestProxy.java

/**
 * This method performs the proxying of the request to the target address.
 *
 * @param target     The target address. Has to be a fully qualified address. The request is send as-is to this address.
 * @param hsRequest  The request data which should be send to the
 * @param hsResponse The response data which will contain the data returned by the proxied request to target.
 * @throws java.io.IOException Passed on from the connection logic.
 *//*from   w  ww  .jav  a 2s .c o  m*/
public static void execute(final String target, final String collection, final HttpServletRequest hsRequest,
        final HttpServletResponse hsResponse, MultiThreadedHttpConnectionManager connManager)
        throws IOException {
    // log.info("execute, target is " + target);
    // log.info("response commit state: " + hsResponse.isCommitted());

    if (target == null || "".equals(target) || "".equals(target.trim())) {
        log.error("The target address is not given. Please provide a target address.");
        return;
    }

    // log.info("checking url");
    final URL url;
    try {
        url = new URL(target);
    } catch (MalformedURLException e) {
        // log.error("The provided target url is not valid.", e);
        return;
    }

    // log.info("setting up the host configuration");

    final HostConfiguration config = new HostConfiguration();

    ProxyHost proxyHost = getUseProxyServer((String) hsRequest.getAttribute("use-proxy"));
    if (proxyHost != null)
        config.setProxyHost(proxyHost);

    final int port = url.getPort() != -1 ? url.getPort() : url.getDefaultPort();
    config.setHost(url.getHost(), port, "http");

    // log.info("config is " + config.toString());

    final HttpMethod targetRequest = setupProxyRequest(hsRequest, url);
    if (targetRequest == null) {
        // log.error("Unsupported request method found: " + hsRequest.getMethod());
        return;
    }

    //perform the request to the target server
    final HttpClient client = new HttpClient(connManager);
    //if (log.isInfoEnabled()) {
    // log.info("client state" + client.getState());
    // log.info("client params" + client.getParams().toString());
    // log.info("executeMethod / fetching data ...");
    //}

    final int result = client.executeMethod(config, targetRequest);

    //copy the target response headers to our response
    setupResponseHeaders(targetRequest, hsResponse);

    String binRegex = ".*\\.(?i)(jpg|tif|png|gif|bmp|mp3|mpg)(.*$)*";
    String binRegexRedux = ".*(?i)(\\/thumb)(.*$)*";

    if (target.matches(binRegex) || target.matches(binRegexRedux)) {
        // log.info("binRegex matched: " + target);
        InputStream originalResponseStream = targetRequest.getResponseBodyAsStream();

        if (originalResponseStream != null) {

            if (targetRequest.getResponseHeaders().toString().matches("(?i).*content-type.*")) {
                PrintWriter responseStream = hsResponse.getWriter();
                copyStreamText(targetRequest.getResponseBodyAsString(), responseStream);
            } else {
                OutputStream responseStream = hsResponse.getOutputStream();
                copyStreamBinary(originalResponseStream, responseStream);
            }
        }

    } else {
        // log.info("binRegex NOT matched: " + target);
        String proxyResponseStr = targetRequest.getResponseBodyAsString();
        // the body might be null, i.e. for responses with cache-headers which leave out the body

        if (proxyResponseStr != null) {
            //proxyResponseStr = proxyResponseStr.replaceAll("xqy", "jsp");

            proxyResponseStr = proxyResponseStr.replaceAll("National Library Catalog",
                    "Library of Congress Data Service");
            proxyResponseStr = proxyResponseStr.replaceAll("Library of Congress collections",
                    "Library of Congress bibliographic data");
            proxyResponseStr = proxyResponseStr.replaceAll("Library of Congress Collections",
                    "Library of Congress Bibliographic Data");

            proxyResponseStr = proxyResponseStr.replaceAll("action=\"/", "action=\"/diglib/");
            proxyResponseStr = proxyResponseStr.replaceAll("href=\"/", "href=\"/diglib/");
            proxyResponseStr = proxyResponseStr.replaceAll("href=\"/diglib/loc\\.",
                    "href=\"/diglib/" + collection + "/loc.");
            proxyResponseStr = proxyResponseStr.replaceAll("src=\"/", "src=\"/diglib/");
            proxyResponseStr = proxyResponseStr.replaceAll("value=\"/", "value=\"/diglib/");
            proxyResponseStr = proxyResponseStr.replaceAll("url\\(/", "url\\(/diglib/");
            proxyResponseStr = proxyResponseStr.replaceAll("url\\(\"/", "url\\(\"/diglib/");
            proxyResponseStr = proxyResponseStr.replaceAll("src'\\) == \"/", "src'\\) == \"/diglib/");
            proxyResponseStr = proxyResponseStr.replaceAll("src\", \"/", "src\", \"/diglib/");
            proxyResponseStr = proxyResponseStr.replaceAll("natlibcat@loc.gov", "ndmso@loc.gov");

            proxyResponseStr = proxyResponseStr.replaceAll("/nlc/", "/lcds/");
            proxyResponseStr = proxyResponseStr.replaceAll("/lcwa/", "/lcwanew/");
            //proxyResponseStr = proxyResponseStr.replaceAll("/tohap/", "/x-tohap/");

            proxyResponseStr = proxyResponseStr.replaceAll(".xqy", ".jsp");

            PrintWriter responseStream = hsResponse.getWriter();
            copyStreamText(proxyResponseStr, responseStream);
        }
    }

    // log.info("set up response, result code was " + result);
    targetRequest.releaseConnection();

    // SimpleHttpConnectionManager connManager = (SimpleHttpConnectionManager) client.getHttpConnectionManager();
    // connManager.closeIdleConnections(1000);

    // HttpConnection httpConn = connManager.getConnection(config);
    // httpConn.releaseConnection();

}

From source file:edu.du.penrose.systems.fedoraProxy.web.bus.OaiAggregator.java

/**
 * Retrieve all sets for a particular aggregate set from an AggregateList object.
 * <br>/*from  w  w w .  j  av  a 2s .c  om*/
 * NOTE: I am using HttpMethod.getResponseBodyAsString() instead of HttpMethod.getResponseBodyAsStream(), to make it easy to parse, 
 * unfortunately it reads the entire response at once. Is this a potential memory overflow? Since we are reading text not large
 * binary files, such as a pdf or an image, hopefully we be OK. TBD
 * <br><br>
 * All resumption token values are removed, since fedoraProxy does not know how to handle a request that uses them (in the instance
 * of a restart due to a network error)
 * 
 * The response if of type...
 * 
 * <?xml version="1.0" encoding="UTF-8"?>
 * <OAI-PMH xmlns="http://www.openarchives.org/OAI/2.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/ http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd">
 * <responseDate>2011-06-06T10:54:51Z</responseDate>
 *  
 *   <request verb="ListRecords" metadataPrefix="oai_dc" resumptionToken="">http://adr.coalliance.org/codu/fez/oai.php</request>
 *   <ListRecords>
 *   <record>
 *     <header>
 *       <identifier>oai:adr.coalliance.org:codu:55689</identifier>
 *       <datestamp>2010-09-20T14:09:34Z</datestamp>
  *       <setSpec>oai:adr.coalliance.org:codu:55690</setSpec>         
   *      </header>
 *      <metadata>
  *         <oai_dc:dc xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/oai_dc/ http://www.openarchives.org/OAI/2.0/oai_dc.xsd" xmlns:oai_dc="http://www.openarchives.org/OAI/2.0/oai_dc/" xmlns:dc="http://purl.org/dc/elements/1.1/">
 *              <dc:title>University of Denver Alumna A. Helen Anderson</dc:title>
 *              <dc:identifier>http://adr.coalliance.org/codu/fez/view/codu:55689</dc:identifier>
  *             <dc:description>B.A., 1914; M.A., 1931</dc:description>
   *             <dc:type>DU Image</dc:type>
   *             <dc:date>Array</dc:date>                        
 *              <dc:subject>Education</dc:subject>
 *              <dc:subject>School integration</dc:subject>
 *              <dc:subject>Busing for school integration</dc:subject>
 *              <dc:publisher>University of Denver</dc:publisher>
 *              <dc:relation>A. Helen Anderson Papers</dc:relation>
 *              <dc:format>http://adr.coalliance.org/codu/fez/eserv/codu:55689/U201.02.0002.0023.00002.tif</dc:format>
 *              <dc:rights>Copyright restrictions may apply. User is responsible for all copyright compliance.</dc:rights>
 *            </oai_dc:dc>
 *        </metadata>
 *      </record>
 *      <record>
 *         .......More aggregate records.
 *      <record>
 *      <resumptionToken></resumptionToken>
 *   </ListRecords>
 * </OAI-PMH>
 * 
 * @param verb the oai verb
 * @param response
 * @param authenicate
 * @param aggregateSetName
 * @param metadataPrefix
 * @param forceNewDate if true the oai <datestamp> is set with todays date, to force retrieval.
 * @throws IOException 
 */
private void executeOaiListAggregateSetRecords(String verb, HttpServletResponse response, boolean authenicate,
        String aggregateSetName, String metadataPrefix, boolean forceNewDate) throws IOException {

    AggregateList aggregateSets = new AggregateList();

    AggregateSet setToRetrieve = aggregateSets.getAggregateSet(aggregateSetName);

    if (setToRetrieve == null) {
        return; // TBD this will create an error. return empty set?
    }
    Iterator<SingleSet> setIterator = setToRetrieve.getIterator();

    HttpClient theClient = new HttpClient();

    boolean firstRecordOfFirstSet = true;
    String header = null;
    String footer = null;
    HttpMethod method = null;
    try {
        while (setIterator.hasNext()) {
            SingleSet singleSet = setIterator.next();

            method = new GetMethod(
                    ADR_OAI_GET_URL + "?verb=" + verb + "&set=" + FedoraProxyConstants.ADR_OAI_SET_SPEC
                            + singleSet.getUniqueID() + "&metadataPrefix=" + metadataPrefix);
            theClient.executeMethod(method);
            String recordList = method.getResponseBodyAsString();

            int listRecordTagLoc = recordList.toLowerCase().indexOf("<listrecords>");
            int listRecordEndTagLoc = recordList.toLowerCase().indexOf("</listrecords>");
            //   int resumptionTagLoc       = recordList.toLowerCase().indexOf( "<resumptiontoken>" );
            //   int resumptionEndTagLoc    = recordList.toLowerCase().indexOf( "<resumptiontoken>" );

            if (listRecordTagLoc < 0 || listRecordEndTagLoc < 0) {
                // TBD need to log error

                String errorMsg = "Error retieving: " + singleSet.getName();
                System.out.println(errorMsg);

                continue; // abort and move attempt next set.
            }

            String justTheRecords = recordList.substring(listRecordTagLoc + 14, listRecordEndTagLoc);
            justTheRecords = justTheRecords.substring(0, justTheRecords.indexOf("<resumptionToken>"));

            if (firstRecordOfFirstSet) {
                header = recordList.substring(0, listRecordTagLoc + 14);

                //               int verbLoc    = header.indexOf( "<verb=" );
                //               int verbEndLod = header.indexOf( '>', verbLoc );
                //               int requestEndLod = header.indexOf( "</request>", verbEndLod );
                //               String textToReplace = header.substring( verbEndLod+1, requestEndLod);
                //                     
                //               header.replace( textToReplace, fedoraProxyConstants.FedoraProxyOAI_URL ); // TBD this will this change!

                // see above notes about <resumptionToken>
                footer = "<resumptionToken></resumptionToken>\n" + recordList.substring(listRecordEndTagLoc);
                firstRecordOfFirstSet = false;

                response.getWriter().print(header);
                System.out.println(header);
            }

            if (forceNewDate) {
                justTheRecords = justTheRecords.replaceAll(
                        "<datestamp>\\d\\d\\d\\d-\\d\\d-\\d\\dT\\d\\d:\\d\\d:\\d\\dZ</datestamp>",
                        "<datestamp>2011-08-04T00:00:00Z</datestamp>");
            }
            response.getWriter().print(justTheRecords);
            System.out.println(justTheRecords);
        }
        System.out.println(footer);
        response.getWriter().print(footer);
    } catch (Exception e) {
        response.sendError(404);
    } finally {
        method.releaseConnection();
    }

}

From source file:davmail.exchange.ExchangeSession.java

protected void checkFormLoginQueryString(HttpMethod logonMethod) throws DavMailAuthenticationException {
    String queryString = logonMethod.getQueryString();
    if (queryString != null && (queryString.contains("reason=2") || queryString.contains("reason=4"))) {
        logonMethod.releaseConnection();
        throwAuthenticationFailed();//from w  ww  . j  av  a  2 s  .  c o  m
    }
}

From source file:davmail.exchange.ExchangeSession.java

/**
 * Create an exchange session for the given URL.
 * The session is established for given userName and password
 *
 * @param url      Exchange url/*from w  w w  .j  a  va 2 s . c o  m*/
 * @param userName user login name
 * @param password user password
 * @throws IOException on error
 */
public ExchangeSession(String url, String userName, String password) throws IOException {
    this.userName = userName;
    try {
        httpClient = DavGatewayHttpClientFacade.getInstance(url);
        // set private connection pool
        DavGatewayHttpClientFacade.createMultiThreadedHttpConnectionManager(httpClient);
        boolean isBasicAuthentication = isBasicAuthentication(httpClient, url);
        // clear cookies created by authentication test
        httpClient.getState().clearCookies();

        // The user may have configured an OTP pre-auth username. It is processed
        // so early because OTP pre-auth may disappear in the Exchange LAN and this
        // helps the user to not change is account settings in mail client at each network change.
        if (preAuthUsername == null) {
            // Searches for the delimiter in configured username for the pre-auth user. 
            // The double-quote is not allowed inside email addresses anyway.
            int doubleQuoteIndex = this.userName.indexOf('"');
            if (doubleQuoteIndex > 0) {
                preAuthUsername = this.userName.substring(0, doubleQuoteIndex);
                this.userName = this.userName.substring(doubleQuoteIndex + 1);
            } else {
                // No doublequote: the pre-auth user is the full username, or it is not used at all.
                preAuthUsername = this.userName;
            }
        }

        DavGatewayHttpClientFacade.setCredentials(httpClient, userName, password);

        // get webmail root url
        // providing credentials
        // manually follow redirect
        HttpMethod method = DavGatewayHttpClientFacade.executeFollowRedirects(httpClient, url);

        if (!this.isAuthenticated()) {
            if (isBasicAuthentication) {
                int status = method.getStatusCode();

                if (status == HttpStatus.SC_UNAUTHORIZED) {
                    method.releaseConnection();
                    throw new DavMailAuthenticationException("EXCEPTION_AUTHENTICATION_FAILED");
                } else if (status != HttpStatus.SC_OK) {
                    method.releaseConnection();
                    throw DavGatewayHttpClientFacade.buildHttpException(method);
                }
                // workaround for basic authentication on /exchange and form based authentication at /owa
                if ("/owa/auth/logon.aspx".equals(method.getPath())) {
                    method = formLogin(httpClient, method, userName, password);
                }
            } else {
                method = formLogin(httpClient, method, userName, password);
            }
        }

        // avoid 401 roundtrips, only if NTLM is disabled and basic authentication enabled
        if (isBasicAuthentication && !DavGatewayHttpClientFacade.hasNTLM(httpClient)) {
            httpClient.getParams().setParameter(HttpClientParams.PREEMPTIVE_AUTHENTICATION, true);
        }

        buildSessionInfo(method);

    } catch (DavMailAuthenticationException exc) {
        LOGGER.error(exc.getMessage());
        throw exc;
    } catch (UnknownHostException exc) {
        BundleMessage message = new BundleMessage("EXCEPTION_CONNECT", exc.getClass().getName(),
                exc.getMessage());
        ExchangeSession.LOGGER.error(message);
        throw new DavMailException("EXCEPTION_DAVMAIL_CONFIGURATION", message);
    } catch (WebdavNotAvailableException exc) {
        throw exc;
    } catch (IOException exc) {
        LOGGER.error(BundleMessage.formatLog("EXCEPTION_EXCHANGE_LOGIN_FAILED", exc));
        throw new DavMailException("EXCEPTION_EXCHANGE_LOGIN_FAILED", exc);
    }
    LOGGER.debug("Session " + this + " created");
}

From source file:mitm.common.security.crl.HTTPCRLDownloadHandler.java

private Collection<? extends CRL> downloadCRLs(URI uri, TaskScheduler watchdog)
        throws IOException, HttpException, CRLException, FileNotFoundException {
    Collection<? extends CRL> crls = null;

    HttpClient httpClient = new HttpClient();

    HttpConnectionManagerParams params = httpClient.getHttpConnectionManager().getParams();

    params.setConnectionTimeout((int) downloadParameters.getConnectTimeout());
    params.setSoTimeout((int) downloadParameters.getReadTimeout());

    if (proxyInjector != null) {
        try {//  w  w  w . j  a  v  a 2  s .  c om
            proxyInjector.setProxy(httpClient);
        } catch (ProxyException e) {
            throw new IOException(e);
        }
    }

    HttpMethod getMethod = new GetMethod(uri.toString());

    getMethod.setFollowRedirects(true);
    getMethod.setRequestHeader("User-Agent", NetUtils.HTTP_USER_AGENT);

    /* 
     * Add watchdog that will interrupt the thread on timeout. we want the abort to fire first so add 50% 
     */
    Task threadWatchdogTask = new ThreadInterruptTimeoutTask(Thread.currentThread(), watchdog.getName());
    watchdog.addTask(threadWatchdogTask, (long) (downloadParameters.getTotalTimeout() * 1.5));

    /* 
     * Add watchdog that will abort the HTTPMethod on timeout. we want to close the input first so add 20% 
     */
    Task httpMethodAbortTimeoutTask = new HTTPMethodAbortTimeoutTask(getMethod, watchdog.getName());
    watchdog.addTask(httpMethodAbortTimeoutTask, (long) (downloadParameters.getTotalTimeout() * 1.2));

    try {
        logger.debug("Setting up a connection to: " + uri);

        int statusCode = 0;

        try {
            statusCode = httpClient.executeMethod(getMethod);
        } catch (IllegalArgumentException e) {
            /* 
             * HttpClient can throw IllegalArgumentException when the host is not set 
             */
            throw new CRLException(e);
        }

        if (statusCode != HttpStatus.SC_OK) {
            throw new IOException("Error getting CRL. Message: " + getMethod.getStatusLine());
        }

        InputStream urlStream = getMethod.getResponseBodyAsStream();

        if (urlStream == null) {
            throw new IOException("Response body is null.");
        }

        /* 
         * add a timeout watchdog on the input 
         */
        Task inputWatchdogTask = new InputStreamTimeoutTask(urlStream, watchdog.getName());

        watchdog.addTask(inputWatchdogTask, downloadParameters.getTotalTimeout());

        /*
         * we want to set a max on the number of bytes to download. We do not want
         * a rogue server to provide us with a 1 TB CRL.
         */
        InputStream limitInputStream = new SizeLimitedInputStream(urlStream, downloadParameters.getMaxBytes());

        ReadableOutputStreamBuffer output = new ReadableOutputStreamBuffer(memThreshold);

        try {
            IOUtils.copy(limitInputStream, output);

            if (threadWatchdogTask.hasRun() || httpMethodAbortTimeoutTask.hasRun()
                    || inputWatchdogTask.hasRun()) {
                /* a timeout has occurred */
                throw new IOException(TIMEOUT_ERROR + uri);
            }

            try {
                InputStream input = output.getInputStream();

                try {
                    crls = CRLUtils.readCRLs(input);
                } finally {
                    IOUtils.closeQuietly(input);
                }

                if (crls == null || crls.size() == 0) {
                    logger.debug("No CRLs found in the downloaded stream.");
                }
            } catch (CertificateException e) {
                throw new CRLException(e);
            } catch (NoSuchProviderException e) {
                throw new CRLException(e);
            } catch (SecurityFactoryFactoryException e) {
                throw new CRLException(e);
            }
        } finally {
            /* 
             * we need to close ReadableOutputStreamBuffer to prevent temp file leak 
             */
            IOUtils.closeQuietly(output);
        }
    } finally {
        getMethod.releaseConnection();
    }

    return crls;
}

From source file:davmail.exchange.dav.DavExchangeSession.java

/**
 * Exchange 2003: get mailPath from welcome page
 *
 * @param method current http method// ww w. j  a va 2 s .  c o  m
 * @return mail path from body
 */
protected String getMailpathFromWelcomePage(HttpMethod method) {
    String welcomePageMailPath = null;
    // get user mail URL from html body (multi frame)
    BufferedReader mainPageReader = null;
    try {
        mainPageReader = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream()));
        //noinspection StatementWithEmptyBody
        String line;
        while ((line = mainPageReader.readLine()) != null && line.toLowerCase().indexOf(BASE_HREF) == -1) {
        }
        if (line != null) {
            // Exchange 2003
            int start = line.toLowerCase().indexOf(BASE_HREF) + BASE_HREF.length();
            int end = line.indexOf('\"', start);
            String mailBoxBaseHref = line.substring(start, end);
            URL baseURL = new URL(mailBoxBaseHref);
            welcomePageMailPath = URIUtil.decode(baseURL.getPath());
            LOGGER.debug("Base href found in body, mailPath is " + welcomePageMailPath);
        }
    } catch (IOException e) {
        LOGGER.error("Error parsing main page at " + method.getPath(), e);
    } finally {
        if (mainPageReader != null) {
            try {
                mainPageReader.close();
            } catch (IOException e) {
                LOGGER.error("Error parsing main page at " + method.getPath());
            }
        }
        method.releaseConnection();
    }
    return welcomePageMailPath;
}

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

public void loadResource(Resource resource) {
    HttpMethod httpMethod;

    try {/*from  ww w. j  a  va 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:it.greenvulcano.gvesb.virtual.http.HTTPCallOperation.java

/**
 * @see it.greenvulcano.gvesb.virtual.CallOperation#perform(it.greenvulcano.gvesb.buffer.GVBuffer)
 *//* www  . j  av  a  2 s  . c  o  m*/
@Override
public GVBuffer perform(GVBuffer gvBuffer) throws ConnectionException, CallException, InvalidDataException {
    logger.debug("BEGIN perform(GVBuffer gvBuffer)");
    HttpMethod method = null;
    try {
        String currMethodURI = null;
        Map<String, Object> params = GVBufferPropertiesHelper.getPropertiesMapSO(gvBuffer, true);

        String currHost = PropertiesHandler.expand(host, params, gvBuffer);
        String currPort = PropertiesHandler.expand(port, params, gvBuffer);
        logger.debug("Server Host: " + currHost + " - Port: " + currPort);
        httpClient.getHostConfiguration().setHost(currHost, Integer.parseInt(currPort), protocol);

        auth.setAuthentication(httpClient, host, Integer.parseInt(currPort), gvBuffer, params);
        proxy.setProxy(httpClient, gvBuffer, params);

        currMethodURI = PropertiesHandler.expand(contextPath + methodURI, params, gvBuffer);
        logger.debug("MethodURI[escaped:" + uriEscaped + "]=[" + currMethodURI + "]");
        switch (methodName) {
        case OPTIONS:
            method = new OptionsMethod();
            break;
        case GET:
            method = new GetMethod();
            break;
        case HEAD:
            method = new HeadMethod();
            break;
        case POST:
            method = new PostMethod();
            break;
        case PUT:
            method = new PutMethod();
            break;
        case DELETE:
            method = new DeleteMethod();
            break;
        default:
            throw new CallException("GV_CALL_SERVICE_ERROR",
                    new String[][] { { "service", gvBuffer.getService() }, { "system", gvBuffer.getSystem() },
                            { "id", gvBuffer.getId().toString() },
                            { "message", "Unknown method = " + methodName } });
        }
        method.setURI(new URI(currMethodURI, uriEscaped));

        if ((refDP != null) && (refDP.length() > 0)) {
            logger.debug("Calling configured Data Provider: " + refDP);
            DataProviderManager dataProviderManager = DataProviderManager.instance();
            IDataProvider dataProvider = dataProviderManager.getDataProvider(refDP);
            try {
                dataProvider.setContext(method);
                dataProvider.setObject(gvBuffer);
                method = (HttpMethod) dataProvider.getResult();
            } finally {
                dataProviderManager.releaseDataProvider(refDP, dataProvider);
            }
        }

        int status = httpClient.executeMethod(method);
        gvBuffer.setProperty(RESPONSE_STATUS, String.valueOf(status));
        String statusTxt = method.getStatusText();
        gvBuffer.setProperty(RESPONSE_MESSAGE, (statusTxt != null ? statusTxt : "NULL"));
        Header[] responseHeaders = method.getResponseHeaders();
        for (Header header : responseHeaders) {
            String headerName = RESPONSE_HEADER_PREFIX + header.getName();
            String value = header.getValue();
            if (value == null) {
                value = "";
            }
            gvBuffer.setProperty(headerName, value);
        }
        String cType = "text/html";
        Header cTypeHeader = method.getResponseHeader("Content-Type");
        if (cTypeHeader != null) {
            String cTypeValue = cTypeHeader.getValue();
            if (cTypeValue != null) {
                cType = cTypeValue;
            }
        }
        logger.debug("Response content-type: " + cType);
        ContentType contentType = new ContentType(cType);
        byte[] responseBody = method.getResponseBody();
        Object object = responseBody;
        if (contentType.getPrimaryType().equals("multipart")) {
            object = handleMultipart(responseBody, cType);
        }
        gvBuffer.setObject(object);
    } catch (CallException exc) {
        throw exc;
    } catch (Exception exc) {
        logger.error("ERROR perform(GVBuffer gvBuffer)", exc);
        throw new CallException("GV_CALL_SERVICE_ERROR",
                new String[][] { { "service", gvBuffer.getService() }, { "system", gvBuffer.getSystem() },
                        { "id", gvBuffer.getId().toString() }, { "message", exc.getMessage() } },
                exc);
    } finally {
        try {
            if (method != null) {
                method.releaseConnection();
            }
        } catch (Exception exc) {
            logger.warn("Error while releasing connection", exc);
        }
        logger.debug("END perform(GVBuffer gvBuffer)");
    }
    return gvBuffer;
}

From source file:edu.ucsb.eucalyptus.cloud.ws.WalrusManager.java

public static void callWalrusHeartBeat(String account, String instid, String API) throws Throwable {
    HttpClient client = null;/*from   w  w  w  .ja  v a  2  s .com*/
    HttpMethod method = null;
    NameValuePair[] queryString = null;
    if (account == null || instid == null) {
        LOG.debug(API + ":callWalrusHeartBeat error: #account=" + account + "#instid=" + instid + "#");
        return;
    }
    try {
        client = new HttpClient();
        String URL = "http://127.0.0.1/sbx_svr/rest/EBS/walrusheartbeat";
        queryString = new NameValuePair[] { new NameValuePair("account", account),
                new NameValuePair("instanceid", instid) };
        method = new PostMethod(URL);
        method.addRequestHeader(new Header("Connection", "close"));
        method.setQueryString(queryString);
        int statusCode = client.executeMethod(method);
        if (statusCode == HttpStatus.SC_OK) {
            // Read the response body.
            StringBuffer stb = new StringBuffer();
            InputStream ins = method.getResponseBodyAsStream();
            InputStreamReader insReader = new InputStreamReader(ins);
            BufferedReader br = new BufferedReader(insReader);
            String buffText = br.readLine();
            while (null != buffText) {
                stb.append(buffText);
                buffText = br.readLine();
            }
            if (stb.length() == 0 || StringUtils.isEmpty(stb.toString())) {
                LOG.debug(API + ":callWalrusHeartBeat: Http Response Body is empty!");
            }
        } else {
            LOG.debug(API + ":callWalrusHeartBeat: Http Response Error:" + statusCode + "#account=" + account
                    + "#instid=" + instid);
        }
    } catch (Throwable t) {
        LOG.debug(API + ":callWalrusHeartBeat: Http Response Error: #account=" + account + "#instid=" + instid
                + "#" + t.toString());
        throw t;
    } finally {
        try {
            if (method != null)
                method.releaseConnection();
        } catch (Throwable t) {
            /*NOP*/}
        ;
    }
}

From source file:com.fluidops.iwb.HTMLProvider.HTMLProvider.java

/**
 * Due to some complications with the new CKAN RDF integration, this method
 * will add triples generated from the JSON representation that are missing
 * in the RDF. Hopefully just a temporary solution - maybe the RDF will be
 * updated.//from  ww  w  .  jav a  2s . c  o m
 * 
 * @throws RepositoryException
 */
private List<Statement> jsonFallBack(String host, HttpClient client, URI subject) throws RepositoryException {
    logger.debug("Executing JSON fallback for: " + host);
    HttpMethod method = new GetMethod(host);
    method.setFollowRedirects(true);

    List<Statement> res = new LinkedList<Statement>();
    try {
        int status = client.executeMethod(method);

        if (status == HttpStatus.OK_200) {
            InputStream response = method.getResponseBodyAsStream();
            String content = GenUtil.readUrl(response);
            JSONObject ob = (JSONObject) getJson(content);

            // Resources (Distributions)
            JSONArray resources = ob.getJSONArray("resources");

            for (int i = 0; i < resources.length(); i++) {
                JSONObject resource = (JSONObject) resources.get(i);

                // generate a unique timestamp
                long timestamp = System.currentTimeMillis() + i;

                // HINT:
                // the method ProviderUtils.objectAsUri() is a safe
                // replacement for
                // the ValueFactory.createUri(). It may, however, return
                // null. At
                // this position we're null safe, as subject is a valid URI
                // and
                // the timestamp does not break the URI
                URI distributionUri = ProviderUtils.objectAsUri(subject.toString() + "/" + timestamp);

                // HINT:
                // again, ProviderUtils.createStatement() is used to
                // generate statements
                // when all the three components are known
                res.add(ProviderUtils.createStatement(subject, Vocabulary.DCAT.HAS_DISTRIBUTION,
                        distributionUri));
                res.add(ProviderUtils.createStatement(distributionUri, RDF.TYPE, Vocabulary.DCAT.DISTRIBUTION));

                String accessURL = resource.getString("url");
                String format = resource.getString("format");

                // HINT:
                // the method ProviderUtils.createUriStatement() can be used
                // to create
                // statements with a URI in object position whenever
                // subject+predicate
                // (or only the predicate) are known
                if (!StringUtil.isNullOrEmpty(accessURL))
                    res.add(ProviderUtils.createUriStatement(distributionUri, Vocabulary.DCAT.ACCESSURL,
                            accessURL));

                // HINT:
                // the method ProviderUtils.createLiteralStatement() can be
                // used to create
                // statements with literal in object position whenever
                // subject+predicate
                // (or only the predicate) are known
                if (!StringUtil.isNullOrEmpty(format))
                    res.add(ProviderUtils.createLiteralStatement(distributionUri, Vocabulary.DCTERMS.FORMAT,
                            format));
            }

            // tags
            JSONArray tags = ob.getJSONArray("tags");
            for (int i = 0; i < tags.length(); i++) {
                String tag = tags.getString(i);

                if (!StringUtil.isNullOrEmpty(tag))
                    res.add(ProviderUtils.createLiteralStatement(subject, Vocabulary.DCAT.KEYWORD, tag));

                // HINT:
                // below, we use ProviderUtils.objectToURIInNamespace to
                // create a
                // URI in some target namespace; this method works for any
                // object based on
                // its toString() representation; it is null safe, assuming
                // the object's
                // string representation is neither empty nor null
                if (!(tag.startsWith("lod") || tag.contains("-") || tag.startsWith("rdf")))
                    res.add(ProviderUtils.createStatement(subject, Vocabulary.DCAT.THEME,
                            ProviderUtils.objectToURIInNamespace(Vocabulary.DCAT.NAMESPACE, tag)));
            }

            response.close();
        } else {
            logger.warn("Bad response from server, JSON fallback failed (status " + status + ", Url: " + host
                    + ")");
        }
    } catch (Exception e) {
        logger.warn(e.getMessage());
        res.clear(); // do not return partial result
        // ignore warning (affects only a single dataset)
    } finally {
        method.releaseConnection();
    }

    return res;
}