Example usage for org.apache.commons.httpclient.methods GetMethod GetMethod

List of usage examples for org.apache.commons.httpclient.methods GetMethod GetMethod

Introduction

In this page you can find the example usage for org.apache.commons.httpclient.methods GetMethod GetMethod.

Prototype

public GetMethod(String uri) 

Source Link

Usage

From source file:net.sf.j2ep.requesthandlers.BasicRequestHandler.java

/**
 * Will only set the headers.// w  w  w  .  j  a va 2  s.co  m
 * @throws HttpException 
 * 
 * @see net.sf.j2ep.model.RequestHandler#process(javax.servlet.http.HttpServletRequest, java.lang.String)
 */
public HttpMethod process(HttpServletRequest request, String url) throws HttpException {

    HttpMethodBase method = null;

    if (request.getMethod().equalsIgnoreCase("GET")) {
        method = new GetMethod(url);
    } else if (request.getMethod().equalsIgnoreCase("HEAD")) {
        method = new HeadMethod(url);
    } else if (request.getMethod().equalsIgnoreCase("DELETE")) {
        method = new DeleteMethod(url);
    } else {
        return null;
    }

    setHeaders(method, request);
    return method;
}

From source file:net.jadler.JadlerMockerResetIntegrationTest.java

private void assertStatus(int expected) throws IOException {
    final HttpClient client = new HttpClient();
    final GetMethod method = new GetMethod("http://localhost:" + mocker.getStubHttpServerPort() + "/");
    assertThat(client.executeMethod(method), is(expected));
    method.releaseConnection();//from w w  w.j a v  a 2s .com
}

From source file:net.oauth.client.OAuthHttpClient.java

/** Send a message to the service provider and get the response. */
@Override//from   w  w w.j ava2 s  .co  m
protected OAuthMessage invoke(OAuthMessage message) throws Exception {
    String form = OAuth.formEncode(message.getParameters());
    HttpMethod method;
    if ("GET".equals(message.httpMethod)) {
        method = new GetMethod(message.URL);
        method.setQueryString(form);
        // method.addRequestHeader("Authorization", message
        // .getAuthorizationHeader(serviceProvider.userAuthorizationURL));
        method.setFollowRedirects(false);
    } else {
        PostMethod post = new PostMethod(message.URL);
        post.setRequestEntity(new StringRequestEntity(form, OAuth.FORM_ENCODED, null));
        method = post;
    }
    clientPool.getHttpClient(new URL(method.getURI().toString())).executeMethod(method);
    final OAuthMessage response = new HttpMethodResponse(method);
    int statusCode = method.getStatusCode();
    if (statusCode != HttpStatus.SC_OK) {
        Map<String, Object> dump = response.getDump();
        OAuthProblemException problem = new OAuthProblemException(
                (String) dump.get(OAuthProblemException.OAUTH_PROBLEM));
        problem.getParameters().putAll(dump);
        throw problem;
    }
    return response;
}

From source file:mesquite.tol.lib.BaseHttpRequestMaker.java

public static boolean contactServer(String s, String URI, StringBuffer response) {
    HttpClient client = new HttpClient();
    GetMethod method = new GetMethod(URI);
    NameValuePair[] pairs = new NameValuePair[1];
    pairs[0] = new NameValuePair("build",
            StringEscapeUtils.escapeHtml("\t" + s + "\tOS =\t" + System.getProperty("os.name") + "\t"
                    + System.getProperty("os.version") + "\tjava =\t" + System.getProperty("java.version")
                    + "\t" + System.getProperty("java.vendor")));
    method.setQueryString(pairs);/*from  w ww. ja  va  2  s  .  com*/

    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(3, false));

    return executeMethod(client, method, response);
}

From source file:com.comcast.cats.service.util.HttpClientUtil.java

public static synchronized Object getForObject(String uri, Map<String, String> paramMap) {
    Object responseObject = new Object();

    HttpMethod httpMethod = new GetMethod(uri);

    if ((null != paramMap) && (!paramMap.isEmpty())) {
        httpMethod.setQueryString(getNameValuePair(paramMap));
    }/*from  w  ww .j a  va2s .  c om*/

    Yaml yaml = new Yaml();

    HttpClient client = new HttpClient();
    InputStream responseStream = null;
    Reader inputStreamReader = null;

    try {
        int responseCode = client.executeMethod(httpMethod);

        if (HttpStatus.SC_OK != responseCode) {
            logger.error("[ REQUEST  ] " + httpMethod.getURI().toString());
            logger.error("[ METHOD   ] " + httpMethod.getName());
            logger.error("[ STATUS   ] " + responseCode);
        } else {
            logger.trace("[ REQUEST  ] " + httpMethod.getURI().toString());
            logger.trace("[ METHOD   ] " + httpMethod.getName());
            logger.trace("[ STATUS   ] " + responseCode);
        }

        responseStream = httpMethod.getResponseBodyAsStream();
        inputStreamReader = new InputStreamReader(responseStream, VideoRecorderServiceConstants.UTF);
        responseObject = yaml.load(inputStreamReader);
    } catch (IOException ioException) {
        ioException.printStackTrace();
    } finally {
        cleanUp(inputStreamReader, responseStream, httpMethod);
    }

    return responseObject;
}

From source file:cn.edu.seu.herald.ws.api.impl.RequestGetMethodFactoryImpl.java

@Override
public GetMethod newXmlRequestGetMethod(URI uri) {
    GetMethod getMethod = new GetMethod(uri.toASCIIString());
    getMethod.setRequestHeader(HTTP_HEADER_ACCEPT, MEDIA_TYPE_APPLICATION_XML);
    return getMethod;
}

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

private static final HttpMethodBase setup(HttpClient client, URL url, Map requestParameters,
        Cookie[] requestCookies) {/*from  w  ww .  jav  a 2s  . c  om*/
    LogFactory.getLog(ConnectPubcookie.class).debug(ConnectPubcookie.class.getName() + ".setup()");
    HttpMethodBase method = null;
    if (requestParameters == null) {
        LogFactory.getLog(ConnectPubcookie.class)
                .debug(ConnectPubcookie.class.getName() + ".setup()" + " requestParameters == null");
        method = new GetMethod(url.toExternalForm());
        //GetMethod is superclass to ExpectContinueMethod, so we don't require method.setUseExpectHeader(false);
        LogFactory.getLog(ConnectPubcookie.class)
                .debug(ConnectPubcookie.class.getName() + ".setup()" + " after getting method");
    } else {
        LogFactory.getLog(ConnectPubcookie.class)
                .debug(ConnectPubcookie.class.getName() + ".setup()" + " requestParameters != null");
        method = new PostMethod(url.toExternalForm()); // "http://localhost:8080/"
        LogFactory.getLog(ConnectPubcookie.class)
                .debug(ConnectPubcookie.class.getName() + ".setup()" + " after getting method");

        //XXX method.getParams().setBooleanParameter(HttpMethodParams.USE_EXPECT_CONTINUE, false); //new way
        //XXX method.getParams().setIntParameter(HttpMethodParams.SO_TIMEOUT, 10000);            
        //XXX method.getParams().setVersion(HttpVersion.HTTP_0_9); //or HttpVersion.HTTP_1_0 HttpVersion.HTTP_1_1

        LogFactory.getLog(ConnectPubcookie.class)
                .debug(ConnectPubcookie.class.getName() + ".setup()" + " after setting USE_EXPECT_CONTINUE");

        //PostMethod is subclass of ExpectContinueMethod, so we require here:            
        //((PostMethod)method).setUseExpectHeader(false);
        //client.setTimeout(30000); // increased from 10000 as temp fix; 2005-03-17 wdn5e
        //HttpClientParams httpClientParams = new HttpClientParams();
        //httpClientParams.setBooleanParameter(HttpMethodParams.USE_EXPECT_CONTINUE, true); //old way
        //httpClientParams.setIntParameter(HttpMethodParams.SO_TIMEOUT, 30000);

        LogFactory.getLog(ConnectPubcookie.class).debug(ConnectPubcookie.class.getName() + ".setup()" + " A");

        Part[] parts = new Part[requestParameters.size()];
        Iterator iterator = requestParameters.keySet().iterator();
        for (int i = 0; iterator.hasNext(); i++) {
            String fieldName = (String) iterator.next();
            String fieldValue = (String) requestParameters.get(fieldName);
            StringPart stringPart = new StringPart(fieldName, fieldValue);
            parts[i] = stringPart;
            LogFactory.getLog(ConnectPubcookie.class).debug(ConnectPubcookie.class.getName() + ".setup()"
                    + " part[" + i + "]==" + fieldName + "=" + fieldValue);

            ((PostMethod) method).addParameter(fieldName, fieldValue); //old way
        }

        LogFactory.getLog(ConnectPubcookie.class).debug(ConnectPubcookie.class.getName() + ".setup()" + " B");

        //XXX MultipartRequestEntity multipartRequestEntity = new MultipartRequestEntity(parts, method.getParams());
        // ((PostMethod)method).setRequestEntity(multipartRequestEntity); //new way            
    }
    //method.getParams().setCookiePolicy(CookiePolicy.RFC_2109);
    HttpState state = client.getState();
    for (Cookie cookie : requestCookies) {
        state.addCookie(cookie);
    }
    //method.setFollowRedirects(true); this is disallowed at runtime, so redirect won't be honored

    LogFactory.getLog(ConnectPubcookie.class).debug(ConnectPubcookie.class.getName() + ".setup()" + " C");
    LogFactory.getLog(ConnectPubcookie.class)
            .debug(ConnectPubcookie.class.getName() + ".setup()" + " method==" + method);
    LogFactory.getLog(ConnectPubcookie.class)
            .debug(ConnectPubcookie.class.getName() + ".setup()" + " method==" + method.toString());
    return method;
}

From source file:fr.mobilit.neo4j.server.service.nantes.CycleRentTest.java

@BeforeClass
public void setUp() throws Exception {
    super.setUp(true);
    HttpClient client = new HttpClient();
    GetMethod get = null;// w  w  w  .  j  av a  2 s  . com
    try {
        // we do the http call and parse the xml response
        get = new GetMethod(CycleRentImpl.IMPORT_URL);
        client.executeMethod(get);
        javax.xml.stream.XMLInputFactory factory = javax.xml.stream.XMLInputFactory.newInstance();
        javax.xml.stream.XMLStreamReader parser = factory.createXMLStreamReader(get.getResponseBodyAsStream());
        ArrayList<String> currentXMLTags = new ArrayList<String>();
        int depth = 0;
        while (true) {
            int event = parser.next();
            if (event == javax.xml.stream.XMLStreamConstants.END_DOCUMENT) {
                break;
            }
            switch (event) {
            case javax.xml.stream.XMLStreamConstants.START_ELEMENT:
                currentXMLTags.add(depth, parser.getLocalName());
                String tagPath = currentXMLTags.toString();
                // here we have a match, so we construct the POI
                if (tagPath.equals("[carto, markers, marker]")) {
                    numberOfStations++;
                }
                depth++;
                break;
            case javax.xml.stream.XMLStreamConstants.END_ELEMENT:
                depth--;
                currentXMLTags.remove(depth);
                break;
            default:
                break;
            }
        }
    } catch (Exception e) {
        throw new RuntimeException(e.getMessage(), e.getCause());
    } finally {
        get.releaseConnection();
    }
}

From source file:com.epam.wilma.gepard.test.operation.OperationModeSwitch.java

/**
 * Set Wilma operation mode (Wilma/stub/proxy).
 * @param tc is the caller Test Case/*from   w ww.j av a2 s. c  o m*/
 * @param url to Wilma
 * @return with the response code
 * @throws Exception in case error happens
 */
public String switchOperationMode(final WilmaTestLogDecorator tc, final String url) throws Exception {
    tc.logStep("Switching operation mode in Wilma to: " + url.split("switch/")[1]);
    tc.logGetRequestEvent(url);

    HttpClient httpClient = new HttpClient();
    GetMethod httpGet = new GetMethod(url);
    int statusCode = httpClient.executeMethod(httpGet);
    String responseCode = "status code: " + statusCode + "\n";
    if (statusCode != STATUS_OK) {
        tc.naTestCase("Operation mode switch failed.");
    }
    tc.logComment("Operation mode is: " + url.split("switch/")[1]);
    return responseCode;
}

From source file:net.sf.ehcache.constructs.web.filter.SpeedTest.java

/**
 * Time to get 200 Cached Pages/*from w w w .ja  v a 2  s . c om*/
 * StopWatch time: 947ms
 */
public void testSpeedHttpClientNotCached() throws IOException {
    StopWatch stopWatch = new StopWatch();
    String url = "http://localhost:9080/Login.jsp";
    HttpClient httpClient = new HttpClient();
    HttpMethod httpMethod = new GetMethod(url);
    stopWatch.getElapsedTime();
    for (int i = 0; i < 200; i++) {
        httpClient.executeMethod(httpMethod);
        httpMethod.getResponseBodyAsStream();
    }
    long time = stopWatch.getElapsedTime();
    LOG.info("Time for 200 uncached page requests: " + time);
}