Example usage for org.apache.http.client.methods HttpGet getMethod

List of usage examples for org.apache.http.client.methods HttpGet getMethod

Introduction

In this page you can find the example usage for org.apache.http.client.methods HttpGet getMethod.

Prototype

@Override
    public String getMethod() 

Source Link

Usage

From source file:org.opencastproject.loadtest.engage.Main.java

/** Retrieve the list of episodes from the engage server so that we can randomly switch between them. **/
private static LinkedList<String> getListOfEpisodes() {
    TrustedHttpClient trustedClient = getClient();
    String request = engageServerURL + SEARCH_URL;
    HttpGet httpGet = new HttpGet(request);
    logger.debug("Using " + httpGet.getMethod() + " on url " + request + " to get the episode list.");
    HttpResponse response = trustedClient.execute(httpGet);
    if (response.getStatusLine().getStatusCode() == HttpURLConnection.HTTP_OK) {
        logger.debug("The result from trying to get the episode list is " + response.getStatusLine());
    } else {//w w w .  j a  va  2 s.  c o m
        logger.warn("The result from trying to get the episode list is " + response.getStatusLine());
    }

    String xml = "";
    try {
        xml = IOUtils.toString(response.getEntity().getContent(), "UTF-8");
    } catch (IllegalStateException e) {
        logger.error("Could not get episode list from response because of: ", e);
    } catch (IOException e) {
        logger.error("Could not get episode list from response because of: ", e);
    }

    LinkedList<String> episodeList = new LinkedList<String>();
    episodeList = parseEpisodeXml(new ByteArrayInputStream(xml.getBytes()));
    return episodeList;
}

From source file:org.openjena.riot.web.HttpOp.java

/** GET
 *  <p>The acceptHeader string is any legal value for HTTP Accept: field.
 *  <p>The handlers are the set of content types (without charset),
 *  used to dispatch the response body for handling.
 *  <p>A Map entry of ("*",....) is used "no handler found".
 *  <p>HTTP responses 400 and 500 become exceptions.   
 */// ww  w. ja  v  a2 s  .c  o  m
public static void execHttpGet(String url, String acceptHeader, Map<String, HttpResponseHandler> handlers) {
    try {
        long id = counter.incrementAndGet();
        String requestURI = determineRequestURI(url);
        String baseIRI = determineBaseIRI(requestURI);

        HttpGet httpget = new HttpGet(requestURI);
        if (log.isDebugEnabled())
            log.debug(format("[%d] %s %s", id, httpget.getMethod(), httpget.getURI().toString()));
        // Accept
        if (acceptHeader != null)
            httpget.addHeader(HttpNames.hAccept, acceptHeader);

        // Execute
        HttpClient httpclient = new DefaultHttpClient();
        HttpResponse response = httpclient.execute(httpget);
        // Handle response
        httpResponse(id, response, baseIRI, handlers);
        httpclient.getConnectionManager().shutdown();
    } catch (IOException ex) {
        ex.printStackTrace(System.err);
    }
}

From source file:nl.nn.adapterframework.http.HttpResponseMock.java

public InputStream doGet(HttpHost host, HttpGet request, HttpContext context) {
    assertEquals("GET", request.getMethod());
    StringBuilder response = new StringBuilder();
    String lineSeparator = System.getProperty("line.separator");
    response.append(request.toString() + lineSeparator);

    Header[] headers = request.getAllHeaders();
    for (Header header : headers) {
        response.append(header.getName() + ": " + header.getValue() + lineSeparator);
    }//from   w ww.j a v a  2s  .  com

    return new ByteArrayInputStream(response.toString().getBytes());
}

From source file:com.pannous.es.reindex.MySearchResponseJson.java

public JSONObject doGet(String url) throws JSONException {
    HttpGet http = new HttpGet(url);
    try {//from  ww  w  .ja  va 2  s . co m
        addHeaders(http);
        HttpResponse rsp = client.execute(http);
        int ret = rsp.getStatusLine().getStatusCode();
        if (ret / 200 == 1)
            return new JSONObject(readString(rsp.getEntity().getContent(), "UTF-8"));

        throw new RuntimeException("Problem " + ret + " while " + http.getMethod() + " "
                + readString(rsp.getEntity().getContent(), "UTF-8"));
    } catch (Exception ex) {
        throw new RuntimeException(
                "Problem while " + http.getMethod() + ", Error:" + ex.getMessage() + ", url:" + url, ex);
    } finally {
        http.releaseConnection();
    }
}

From source file:org.sahli.asciidoc.confluence.publisher.client.http.HttpRequestFactoryTest.java

@Test
public void getPageByTitleRequest_withValidParameters_returnsValidHttpGet() throws Exception {
    // arrange/*from  w  w w. j  av  a 2s . c o  m*/
    String spaceKey = "~personalSpace";
    String title = "Some page";

    // act
    HttpGet getPageByTitleRequest = this.httpRequestFactory.getPageByTitleRequest(spaceKey, title);

    // assert
    assertThat(getPageByTitleRequest.getMethod(), is("GET"));
    assertThat(getPageByTitleRequest.getURI().toString(),
            is(CONFLUENCE_REST_API_ENDPOINT + "/content?spaceKey=" + spaceKey + "&title=" + "Some+page"));
}

From source file:jsonbroker.library.client.http.HttpDispatcher.java

private HttpRequestBase buildGetRequest(HttpRequestAdapter requestAdapter, Authenticator authenticator) {

    String requestUri = requestAdapter.getRequestUri();

    String host = _networkAddress.getHostAddress();
    int port = _networkAddress.getPort();

    String uri = String.format("http://%s:%d%s", host, port, requestUri);
    log.debug(uri, "uri");

    HttpGet answer = new HttpGet(uri);

    // extra headers ... 
    {//from   w ww .  j av a2 s . co  m
        HashMap<String, String> requestHeaders = requestAdapter.getRequestHeaders();
        for (Map.Entry<String, String> item : requestHeaders.entrySet()) {
            answer.setHeader(item.getKey(), item.getValue());
        }
    }

    if (null != authenticator) {
        String authorization = authenticator.getRequestAuthorization(answer.getMethod(), requestUri, null);
        log.debug(authorization, "authorization");
        if (null != authorization) {
            answer.addHeader("Authorization", authorization);
        }
    }

    return answer;
}

From source file:com.oneguy.recognize.engine.GoogleStreamingEngine.java

byte[] getHttpData(String url) {
    String TAG = "getHttpData";
    HttpGet httpGet = new HttpGet(url);
    byte[] result = null;
    try {//from  w  w w. j  a  va  2s  .  c  om
        HttpClient client = new DefaultHttpClient();
        client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, TIMEOUT);
        client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, TIMEOUT);
        Log.d(TAG, "HTTP GET:" + httpGet.getURI() + " method:" + httpGet.getMethod());
        HttpResponse httpResponse = client.execute(httpGet);
        if (httpResponse.getStatusLine().getStatusCode() == 200) {
            result = EntityUtils.toByteArray(httpResponse.getEntity());
            Log.d(TAG, new String(result));
            StreamResponse response = new StreamResponse(null, Status.SUCCESS);
            response.setResponse(result);
            response.setResponseCode(httpResponse.getStatusLine().getStatusCode());
            Message msg = new Message();
            msg.what = EVENT_RESPONSE;
            msg.obj = response;
            mHandler.sendMessage(msg);
        }
    } catch (ClientProtocolException e) {
        onError(e);
        e.printStackTrace();
    } catch (IOException e) {
        onError(e);
        e.printStackTrace();
    }
    return result;
}

From source file:org.wso2.carbon.appfactory.jenkins.build.RestBasedJenkinsCIConnector.java

/**
 * When jenkins tenant is unloaded the requests cannot be fulfilled. So this
 * method will be used to resend the Get requests
 *
 * @param method       method to be retried
 * @param httpResponse http response of the previous request
 * @return httpStatusCode// w ww .ja v a  2s  . c om
 */
private HttpResponse resendRequest(HttpGet method, HttpResponse httpResponse) throws AppFactoryException {

    int httpStatusCode;
    int retryCount = Integer.parseInt(AppFactoryUtil.getAppfactoryConfiguration()
            .getFirstProperty(JenkinsCIConstants.JENKINS_CLIENT_RETRY_COUNT));
    int retryDelay = Integer.parseInt(AppFactoryUtil.getAppfactoryConfiguration()
            .getFirstProperty(JenkinsCIConstants.JENKINS_CLIENT_RETRY_DELAY));
    log.info("Jenkins client retry count :" + retryCount + " and retry delay in seconds :" + retryDelay
            + " for " + method.getMethod());

    //TODO - Send mail to cloud
    try {
        // retry retryCount times to process the request
        for (int i = 0; i < retryCount; i++) {
            Thread.sleep(MILLISECONDS_PER_SECOND * retryDelay); // sleep retryDelay seconds, giving jenkins
            // time to load the tenant
            if (log.isDebugEnabled()) {
                log.debug("Resending request(" + i + ") started for GET");
            }

            if (httpResponse != null) {
                EntityUtils.consume(httpResponse.getEntity());
            }

            httpResponse = httpClient.execute(method, getHttpContext());
            httpStatusCode = httpResponse.getStatusLine().getStatusCode();

            // In the new jenkins release the response is always 201 or 302
            if (HttpStatus.SC_OK == httpStatusCode || HttpStatus.SC_CREATED == httpStatusCode
                    || HttpStatus.SC_MOVED_TEMPORARILY == httpStatusCode) {
                if (log.isDebugEnabled()) {
                    log.debug("Break resending since " + httpStatusCode);
                }
                break;
            }
            if (log.isDebugEnabled()) {
                log.debug("Resent GET request(" + i + ") failed with response code " + httpStatusCode);
            }
        }
    } catch (IOException e) {
        String msg = "Error while resending the request to URI : " + method.getURI();
        log.error(msg, e);
        throw new AppFactoryException(msg, e);
    } catch (InterruptedException e) {
        String msg = "Error while resending the request to URI : " + method.getURI();
        log.error(msg, e);
        throw new AppFactoryException(msg, e);
    }
    return httpResponse;
}