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.colombbus.tangara.net.CommandExceptionFactory.java

public static CommunicationCmdException createCommunicationException(HttpMethod method, int code, String msg,
        Throwable th) {/*ww  w .j a v a 2 s .co  m*/
    return new CommunicationCmdException(method.getPath(), code, msg, th);
}

From source file:org.colombbus.tangara.net.TConnection.java

private String executeMethod(HttpMethod method) throws CommandException {
    int statusCode = 0;
    try {/*from w ww. j  ava 2 s . c o m*/
        statusCode = getClient().executeMethod(method);
        if (statusCode == HttpStatus.SC_REQUEST_TIMEOUT) {
            LOG.warn("Request timeout");
        }
        if (statusCode != HttpStatus.SC_OK) {
            String msg = "Method failed: " + method.getStatusLine(); //$NON-NLS-1$
            LOG.error(msg);
            throw new IOException(msg);
        }

        InputStream in = method.getResponseBodyAsStream();
        byte[] buffer = IOUtils.toByteArray(in);

        String response = new String(buffer, encodingCharset);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Response content:\n" + response);
        }
        return response;
    } catch (HttpException httpEx) {
        String msg = String.format("An HTTP error occurs during the " + //$NON-NLS-1$
                "execution of the method %s. The returned HTTP code is %d", //$NON-NLS-1$
                method.getPath(), statusCode);
        LOG.error(msg, httpEx);
        throw CommandExceptionFactory.createCommunicationException(method, CommandException.HTTP_ERR, msg,
                httpEx);
    } catch (IOException ioEx) {
        String msg = String.format("An IO communication error occurs during the execution of the method %s.",
                method.getPath());
        LOG.error(msg, ioEx);
        throw CommandExceptionFactory.createCommunicationException(method, CommandException.HTTP_ERR, msg,
                ioEx);
    } catch (Throwable th) {
        String msg = String.format("An unknown error occurs during the execution of the method %s",
                method.getPath());
        LOG.error(msg, th);
        throw CommandExceptionFactory.createCommunicationException(method, CommandException.HTTP_ERR, msg, th);

    }
}

From source file:org.colombbus.tangara.net.TConnection.java

public synchronized Document sendCommand(Command cmd) throws CommandException {
    HttpMethod method = createMethod(cmd);
    Document doc = null;//from w  w  w  .  j a  v  a2 s  .co m

    try {
        String logMsg = String.format("Prepare executing method %s", //$NON-NLS-1$
                method.getPath());
        LOG.info(logMsg);

        String httpResponse = executeMethod(method);
        doc = buildXmlResponse(method, httpResponse);

        logMsg = String.format("Method succeed %s %s", //$NON-NLS-1$
                method.getPath(), method.getStatusLine().toString());
        LOG.info(logMsg);
    } catch (CommandException cmdEx) {
        LOG.error("Command excetion catched in the command", cmdEx);//$NON-NLS-1$
        throw cmdEx;
    } catch (Throwable th) {
        LOG.error("Unknown error catched in the command", th);//$NON-NLS-1$
        throw new CommandException(method.getPath(), CommandException.UNKNOWN_ERR,
                "Unknown error catched in the command", th);//$NON-NLS-1$
    } finally {
        method.releaseConnection();
    }

    return doc;
}

From source file:org.encuestame.core.util.SocialUtils.java

/**
 * Get TinyUrl./* ww  w  .j a v  a2  s.c  o  m*/
 * @param url
 * @return
 * @throws HttpException
 * @throws IOException
 */
public static String getYourls(final String url) {
    String yourlsShortUrl = url;
    HttpClientParams params = new HttpClientParams();
    params.setConnectionManagerTimeout(EnMePlaceHolderConfigurer.getIntegerProperty("application.timeout"));
    params.setSoTimeout(EnMePlaceHolderConfigurer.getIntegerProperty("application.timeout"));
    HttpClient httpclient = new HttpClient(params); //TODO: time out??
    HttpMethod method = new GetMethod(EnMePlaceHolderConfigurer.getProperty("short.yourls.path"));
    method.setQueryString(new NameValuePair[] { new NameValuePair("url", url),
            new NameValuePair("action", "shorturl"), new NameValuePair("format", "json"),
            new NameValuePair("signature", EnMePlaceHolderConfigurer.getProperty("short.yourls.key")) });
    System.out.println("method--->" + method.getPath());
    System.out.println("method--->" + method.getQueryString());
    try {
        httpclient.executeMethod(method);
        final Object jsonObject = JSONValue.parse(method.getResponseBodyAsString());
        final JSONObject o = (JSONObject) jsonObject;
        //{"message":"Please log in","errorCode":403}"
        Long errorCode = (Long) o.get("errorCode");
        if (errorCode != null) {
            throw new EnMeException("Yourls error: " + errorCode);
        }
        yourlsShortUrl = (String) o.get("shorturl");
    } catch (HttpException e) {
        log.error("HttpException " + e);
        yourlsShortUrl = url;
    } catch (IOException e) {
        log.error("IOException" + e);
        yourlsShortUrl = url;
    } catch (Exception e) {
        //e.printStackTrace();
        log.error("IOException" + e);
        yourlsShortUrl = url;
    } finally {
        RequestSessionMap.setErrorMessage("short url is not well configured");
        method.releaseConnection();
    }
    return yourlsShortUrl;
}

From source file:org.geotools.data.couchdb.client.CouchDBClient.java

/**
 * /*from w w w.  j av  a 2 s.  c  om*/
 * @param method
 * @return
 * @throws IOException
 */
private CouchDBResponse executeMethod(HttpMethod method) throws IOException {
    IOException expected = null;
    int result = -1;
    try {
        result = httpClient.executeMethod(method);
    } catch (IOException ex) {
        expected = ex;
    }
    CouchDBResponse resp = new CouchDBResponse(method, result, expected);
    if (logger.isLoggable(Level.FINEST)) {
        logger.finest("Request to : " + method.getPath());
        logger.finest("Response status : " + result);
        logger.finest("Response body:\n" + method.getResponseBodyAsString());
    }
    return resp;
}

From source file:org.hydracache.client.transport.HttpTransportTest.java

@Test
public void testDeleteRequestPathGeneration() {
    HttpTransport transport = new HttpTransport();

    transport.establishConnection("localhost", 90);

    RequestMessage requestMessage = new RequestMessage();

    requestMessage.setMethod("delete");
    requestMessage.setPath("testPath");

    HttpMethod method = transport.createHttpMethod(requestMessage);

    assertTrue("Method type is incorret", method instanceof DeleteMethod);
    assertEquals("Path is incorrect", "/testPath", method.getPath());
}

From source file:org.hydracache.client.transport.HttpTransportTest.java

@Test
public void testDeleteRequestPathGenerationWithContext() {
    HttpTransport transport = new HttpTransport();

    transport.establishConnection("localhost", 90);

    RequestMessage requestMessage = new RequestMessage();

    requestMessage.setMethod("delete");
    requestMessage.setPath("testPath");
    requestMessage.setContext("context");

    HttpMethod method = transport.createHttpMethod(requestMessage);

    assertTrue("Method type is incorret", method instanceof DeleteMethod);
    assertEquals("Path is incorrect", "/context/testPath", method.getPath());
}

From source file:org.hydracache.client.transport.HttpTransportTest.java

@Test
public void testGetRequestPathGeneration() {
    HttpTransport transport = new HttpTransport();

    transport.establishConnection("localhost", 90);

    RequestMessage requestMessage = new RequestMessage();

    requestMessage.setMethod("get");
    requestMessage.setPath("testPath");

    HttpMethod method = transport.createHttpMethod(requestMessage);

    assertTrue("Method type is incorret", method instanceof GetMethod);
    assertEquals("Path is incorrect", "/testPath", method.getPath());
}

From source file:org.hydracache.client.transport.HttpTransportTest.java

@Test
public void testGetRequestPathGenerationWithContext() {
    HttpTransport transport = new HttpTransport();

    transport.establishConnection("localhost", 90);

    RequestMessage requestMessage = new RequestMessage();

    requestMessage.setMethod("get");
    requestMessage.setPath("testPath");
    requestMessage.setContext("context");

    HttpMethod method = transport.createHttpMethod(requestMessage);

    assertTrue("Method type is incorret", method instanceof GetMethod);
    assertEquals("Path is incorrect", "/context/testPath", method.getPath());
}

From source file:org.hydracache.client.transport.HttpTransportTest.java

@Test
public void testPutRequestPathGeneration() {
    HttpTransport transport = new HttpTransport();

    transport.establishConnection("localhost", 90);

    RequestMessage requestMessage = new RequestMessage();

    requestMessage.setMethod("PUT");
    requestMessage.setPath("testPath");

    HttpMethod method = transport.createHttpMethod(requestMessage);

    assertTrue("Method type is incorret", method instanceof PutMethod);
    assertEquals("Path is incorrect", "/testPath", method.getPath());
}