Example usage for org.apache.http.client.methods HttpRequestBase getURI

List of usage examples for org.apache.http.client.methods HttpRequestBase getURI

Introduction

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

Prototype

public URI getURI() 

Source Link

Document

Returns the original request URI.

Usage

From source file:org.plos.crepo.dao.ContentRepoBaseDaoTest.java

@Test
public void executeRequestTest() throws IOException {
    HttpRequestBase httpRequest = mock(HttpRequestBase.class);
    when(httpRequest.getURI()).thenReturn(uri);

    mockCommonCalls(repoAccessConfig, HttpStatus.SC_OK);

    HttpResponse response = contentRepoBaseDao.executeRequest(httpRequest, ErrorType.ErrorFetchingBucketMeta);

    assertNotNull(response);//w  w w. ja  v  a  2s .  co  m
    assertEquals(mockResponse, response);
    assertEquals(uri, httpRequest.getURI());

    verify(repoAccessConfig).open(httpRequest);
    verify(mockResponse).getStatusLine();
    verify(statusLine).getStatusCode();
    verify(repoAccessConfig).open(httpRequest);

}

From source file:bit.changepurse.wdk.http.TestHTTPRequest.java

@Test
public void testParameters() throws ParseException, IOException {
    URL testUrl = newURL(TEST_URL);
    String key = "key";
    String value = "value";
    HTTPRequest request = new HTTPRequest().setMethod(HTTPMethod.POST).setUrl(testUrl).addParameter(key, value);

    HttpRequestBase apacheRequest = request.build();

    assertThat(apacheRequest, instanceOf(HttpPost.class));
    assertThat(apacheRequest.getURI(), equalTo(newURI(testUrl)));
    String entity = EntityUtils.toString(((HttpPost) apacheRequest).getEntity());
    assertThat(entity, containsString(key + "=" + value));
}

From source file:org.plos.crepo.dao.ContentRepoBaseDaoTest.java

@Test
public void executeRequestIOThrowsExcTest() throws IOException {
    HttpRequestBase httpRequest = mock(HttpRequestBase.class);
    when(httpRequest.getURI()).thenReturn(uri);
    IOException exception = mock(IOException.class);
    when(repoAccessConfig.open(isA(HttpRequestBase.class))).thenThrow(exception);

    HttpResponse response = null;/* w  ww . j a v  a2  s . c o m*/

    try {
        response = contentRepoBaseDao.executeRequest(httpRequest, ErrorType.ErrorFetchingBucketMeta);
    } catch (ContentRepoException ex) {
        assertNull(response);
        assertEquals(ErrorType.ErrorFetchingBucketMeta, ex.getErrorType());
        assertTrue(ex.getMessage().contains(uri.toString()));
        assertEquals(exception, ex.getCause());
    }

    verify(repoAccessConfig).open(httpRequest);
    verify(httpRequest, times(2)).getURI();

    assertEquals(uri, httpRequest.getURI());

}

From source file:org.plos.crepo.dao.ContentRepoBaseDaoTest.java

@Test
public void executeRequestThrowsExcTest() throws IOException {
    HttpRequestBase httpRequest = mock(HttpRequestBase.class);
    when(httpRequest.getURI()).thenReturn(uri);

    mockCommonCalls(repoAccessConfig, HttpStatus.SC_BAD_REQUEST);
    mockHttpResponseUtilCalls(mockResponse);

    HttpResponse response = null;/*from   ww w  . java  2 s . c  om*/

    try {
        response = contentRepoBaseDao.executeRequest(httpRequest, ErrorType.ErrorFetchingBucketMeta);
    } catch (ContentRepoException ex) {
        verifyException(ex, response, ErrorType.ErrorFetchingBucketMeta);
    }

    verify(repoAccessConfig).open(httpRequest);
    verify(mockResponse, times(1)).getStatusLine();
    verify(statusLine, times(1)).getStatusCode();
    verify(httpRequest, times(2)).getURI();

    assertNull(response);
    assertEquals(uri, httpRequest.getURI());

}

From source file:com.ibm.team.build.internal.hjplugin.util.HttpUtils.java

/**
 * Print the HTTP request - for debugging purposes
 *//*w  w w.  j av  a 2s .c  o  m*/
@SuppressWarnings("unused")
private static void printRequest(HttpRequestBase request) {
    if (LOGGER.isLoggable(Level.FINER)) {
        StringBuffer logMessage = new StringBuffer();
        logMessage.append(NEW_LINE).append("\t- Method: ").append(request.getMethod()); //$NON-NLS-1$ //
        logMessage.append(NEW_LINE).append("\t- URL: ").append(request.getURI()); //$NON-NLS-1$
        logMessage.append(NEW_LINE).append("\t- Headers: "); //$NON-NLS-1$
        Header[] headers = request.getAllHeaders();
        for (int i = 0; i < headers.length; i++) {
            logMessage.append(NEW_LINE).append("\t\t- ").append(headers[i].getName()).append(": ") //$NON-NLS-1$//$NON-NLS-2$
                    .append(headers[i].getValue());
        }
        LOGGER.finer(logMessage.toString());
    }
}

From source file:TTrestclient.TeachTimeRESTclient.java

private void execute_and_dump(HttpRequestBase request) {
    try {//  ww  w  . j a  v  a  2s. co  m
        System.out.println("Metodo: " + request.getMethod());
        System.out.println("URL: " + request.getURI());
        if (request.getFirstHeader("Accept") != null) {
            System.out.println(request.getFirstHeader("Accept"));
        }
        if (request.getMethod().equals("POST")) {
            HttpEntity e = ((HttpPost) request).getEntity();
            System.out.print("Payload: ");
            e.writeTo(System.out);
            System.out.println();
            System.out.println("Tipo payload: " + e.getContentType());
        }
        //eseguiamo la richiesta
        CloseableHttpResponse response = client.execute(request);
        try {
            //preleviamo il contenuto della risposta
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                entity.writeTo(System.out);
                System.out.println();
            }
            //controlliamo lo status
            //if (response.getStatusLine().getStatusCode() != 200) {
            System.out.println("Return status: " + response.getStatusLine().getReasonPhrase() + " ("
                    + response.getStatusLine().getStatusCode() + ")");
            //}

        } finally {
            //chiudiamo la risposta
            response.close();
        }
    } catch (IOException ex) {
        Logger.getLogger(TeachTimeRESTclient.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:ch.cyberduck.core.dav.DAVClient.java

@Override
public <T> T execute(final HttpRequestBase request, final ResponseHandler<T> responseHandler)
        throws IOException {
    if (StringUtils.isNotBlank(request.getURI().getRawQuery())) {
        request.setURI(URI.create(
                String.format("%s%s?%s", uri, request.getURI().getRawPath(), request.getURI().getRawQuery())));
    } else {//from w  w w. j  av  a2 s  .com
        request.setURI(URI.create(String.format("%s%s", uri, request.getURI().getRawPath())));
    }
    return super.execute(request, responseHandler);
}

From source file:com.eviware.soapui.impl.rest.actions.oauth.OltuOAuth2ClientFacade.java

@Override
public void applyAccessToken(OAuth2Profile profile, HttpRequestBase request, String requestContent) {

    String uri = request.getURI().getPath();
    OAuthBearerClientRequest oAuthClientRequest = new OAuthBearerClientRequest(uri)
            .setAccessToken(profile.getAccessToken());

    try {//from   w  w w .  j a va 2 s.c o m
        switch (profile.getAccessTokenPosition()) {
        case QUERY:
            appendAccessTokenToQuery(request, oAuthClientRequest);
            break;
        case BODY:
            appendAccessTokenToBody(request, oAuthClientRequest);
            break;
        case HEADER:
        default:
            appendAccessTokenToHeader(request, oAuthClientRequest);
            break;
        }
    } catch (OAuthSystemException e) {
        SoapUI.logError(e);
    }
}

From source file:bit.changepurse.wdk.http.TestHTTPRequest.java

@Test
public void testBuild() {
    for (HTTPMethod method : HTTPMethod.values()) {
        try {//from  ww  w. j  a  v a2  s .c  om
            HTTPRequest request = new HTTPRequest();
            request.setMethod(method);
            request.setUrl(TEST_URL);
            HttpRequestBase apacheRequest = request.build();
            assertThat(apacheRequest, instanceOf(mapping.get(method)));
            assertThat(apacheRequest.getURI(), equalTo(newURI(TEST_URL)));
        } catch (HTTPException exception) {
            assertThat(method, not(isIn(EnumSet.of(HTTPMethod.GET, HTTPMethod.POST))));
        }
    }
}

From source file:org.cloudifysource.restclient.GSRestClient.java

/**
 * Gets the HTTP response's body as a String.
 *
 * @param response/*from www.  j  a v  a2 s . c o m*/
 *            The HttpResponse object to analyze
 * @param httpMethod
 *            The HTTP request that originated this response
 * @return the body of the given HttpResponse object, as a string
 * @throws ErrorStatusException
 *             Reporting a communication failure
 * @throws IOException
 *             Reporting a failure to read the response's content
 */
public static String getResponseBody(final HttpResponse response, final HttpRequestBase httpMethod)
        throws ErrorStatusException, IOException {

    InputStream instream = null;
    try {
        final HttpEntity entity = response.getEntity();
        if (entity == null) {
            final ErrorStatusException e = new ErrorStatusException(REASON_CODE_COMM_ERR, httpMethod.getURI(),
                    MSG_RESPONSE_ENTITY_NULL);
            logger.log(Level.FINE, MSG_RESPONSE_ENTITY_NULL, e);
            throw e;
        }
        instream = entity.getContent();
        return StringUtils.getStringFromStream(instream);
    } finally {
        if (instream != null) {
            try {
                instream.close();
            } catch (final IOException e) {
                e.printStackTrace();
            }
        }
    }
}