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

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

Introduction

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

Prototype

public abstract String getPath();

Source Link

Usage

From source file:org.tuleap.mylyn.task.core.internal.client.rest.TuleapRestConnector.java

/**
 * Logs a debug message of the REST request/response.
 *
 * @param method/*from   w  w  w. j av a 2  s.  c  o m*/
 *            The method executed
 * @param bodyReceived
 *            The response body received
 */
private void debugRestCall(HttpMethod method, String bodyReceived) {
    int responseStatus = method.getStatusCode();
    StringBuilder b = new StringBuilder();
    b.append(method.getName());
    b.append(" ").append(method.getPath()); //$NON-NLS-1$
    String qs = method.getQueryString();
    if (qs != null && !qs.isEmpty()) {
        b.append('?').append(method.getQueryString());
    }
    if (method instanceof EntityEnclosingMethod) {
        RequestEntity requestEntity = ((EntityEnclosingMethod) method).getRequestEntity();
        String body = ""; //$NON-NLS-1$
        if (requestEntity.isRepeatable()) {
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            try {
                requestEntity.writeRequest(os);
                body = os.toString("UTF-8"); //$NON-NLS-1$
            } catch (UnsupportedEncodingException e) {
                // Nothing to do
            } catch (IOException e) {
                // Nothing to do
            }
        }
        b.append("\nbody:\n").append(body.replaceAll(//$NON-NLS-1$
                "\"password\" *: *\".*\"", "\"password\":\"(hidden in debug)\"")); //$NON-NLS-1$ //$NON-NLS-2$
    }
    b.append("\n__________\nresponse:\n"); //$NON-NLS-1$
    b.append(method.getStatusLine()).append("\n"); //$NON-NLS-1$
    b.append("body:\n"); //$NON-NLS-1$
    b.append(bodyReceived);
    int status = IStatus.INFO;
    if (responseStatus != HttpURLConnection.HTTP_OK) {
        status = IStatus.ERROR;
    }
    this.logger.log(new Status(status, TuleapCoreActivator.PLUGIN_ID, b.toString()));
}

From source file:org.tuleap.mylyn.task.core.tests.internal.client.rest.MockRestConnector.java

protected ServerRequest getServerRequest(HttpMethod method) {
    Map<String, String> header = new LinkedHashMap<String, String>();
    for (Header h : method.getRequestHeaders()) {
        header.put(h.getName(), h.getValue());
    }/*from  w  ww .j  a va  2 s.  c om*/
    if (method instanceof EntityEnclosingMethod) {
        RequestEntity entity = ((EntityEnclosingMethod) method).getRequestEntity();
        if (entity instanceof StringRequestEntity) {
            return new ServerRequest(method.getName(), method.getPath(), header, method.getQueryString(),
                    ((StringRequestEntity) entity).getContent());
        }
    }
    return new ServerRequest(method.getName(), method.getPath(), header, method.getQueryString());
}

From source file:org.tuleap.mylyn.task.core.tests.internal.client.rest.RestOperationsTest.java

@Test
public void testBasicBehavior() {
    RestOperation op = RestOperation.get("some/url", connector, gson, logger);
    HttpMethod m = op.createMethod();
    assertEquals("GET", m.getName());
    assertEquals("some/url", m.getPath());
    assertEquals("", m.getQueryString());

    op.withQueryParameter("key1", "value1");
    m = op.createMethod();//from   w ww.  ja  v  a2s . c  om
    assertEquals("GET", m.getName());
    assertEquals("some/url", m.getPath());
    assertEquals("key1=value1", m.getQueryString());

    op.withQueryParameter("key1", "value1b");
    m = op.createMethod();
    assertEquals("GET", m.getName());
    assertEquals("some/url", m.getPath());
    assertEquals("key1=value1b", m.getQueryString());

    op.withQueryParameter("key1", "value1", "value1b");
    m = op.createMethod();
    assertEquals("GET", m.getName());
    assertEquals("some/url", m.getPath());
    assertEquals("key1=value1&key1=value1b", m.getQueryString());

    op.withQueryParameter("key2", "value2");
    m = op.createMethod();
    assertEquals("GET", m.getName());
    assertEquals("some/url", m.getPath());
    assertEquals("key1=value1&key1=value1b&key2=value2", m.getQueryString());

    op.withoutQueryParameters("key1");
    m = op.createMethod();
    assertEquals("GET", m.getName());
    assertEquals("some/url", m.getPath());
    assertEquals("key2=value2", m.getQueryString());

    op.withoutQueryParameter();
    m = op.createMethod();
    assertEquals("GET", m.getName());
    assertEquals("some/url", m.getPath());
    assertEquals("", m.getQueryString());
}

From source file:org.tuleap.mylyn.task.core.tests.internal.client.rest.RestResourceTest.java

/**
 * Test the pagination on GET with HEADER_X_PAGINATION_SIZE not set.
 * //w  w w . j  a va  2s.c  om
 * @throws CoreException
 *             The exception to throw
 */
@Test
public void testGetPaginationSizeNotSet() throws CoreException {
    RestResource r = new RestResource("/server/api/v12.5/my/url", RestResource.GET, connector, gson,
            new TestLogger());
    Map<String, String> headers = Maps.newTreeMap();
    headers.put(RestResource.ALLOW, "OPTIONS,GET");
    headers.put(RestResource.ACCESS_CONTROL_ALLOW_METHODS, "OPTIONS,GET");
    connector.setResponse(new ServerResponse(ServerResponse.STATUS_OK, "", headers));
    RestOperation get = r.get();
    HttpMethod method = get.createMethod();
    assertEquals("GET", method.getName());
    assertEquals("/server/api/v12.5/my/url", method.getPath());
    assertEquals("", method.getQueryString());

    headers.put(RestResource.HEADER_X_PAGINATION_LIMIT_MAX, "10");
    connector.setResponse(new ServerResponse(ServerResponse.STATUS_OK, "", headers));
    get = r.get();
    method = get.createMethod();
    assertEquals("GET", method.getName());
    assertEquals("/server/api/v12.5/my/url", method.getPath());
    assertEquals("", method.getQueryString());
}

From source file:org.zdevra.guice.mvc.security.controller.SecureRoleControllerTest.java

@Test
public void shouldDenyRoleNotAuthenticatedAndAllowCorrectUser() throws IOException, ServletException {

    HttpMethod method = doRequest("http://localhost:9191/auth/invalidate");
    method = doRequest("http://localhost:9191/secure/something");

    int code = method.getStatusCode();

    System.out.println("code:" + code);
    assertThat(code, is(HttpServletResponse.SC_MOVED_TEMPORARILY));

    method = doRequest("http://localhost:9191/auth/authenticate");

    method = doRequest("http://localhost:9191/secure/something");
    code = method.getStatusCode();/*from  ww w  .j  a  v  a  2s  .  c o m*/
    assertThat(code, is(HttpServletResponse.SC_OK));
    assertThat(method.getPath(), is("/secure/something"));
    System.out.println("code:" + code);

}