Example usage for org.apache.commons.httpclient HttpException getMessage

List of usage examples for org.apache.commons.httpclient HttpException getMessage

Introduction

In this page you can find the example usage for org.apache.commons.httpclient HttpException getMessage.

Prototype

public String getMessage() 

Source Link

Document

Returns the detail message string of this throwable.

Usage

From source file:net.neurowork.cenatic.centraldir.workers.xml.XmlTokenImporter.java

public String getXmlToken() {
    String token = null;//from  ww  w  .ja  v  a 2 s  .  c  o  m
    HttpClient httpclient = XMLRestWorker.getHttpClient();
    GetMethod get = new GetMethod(restUrl.getTokenUrl());
    get.addRequestHeader("Accept", "application/xml");

    NameValuePair userParam = new NameValuePair(XMLRestWorker.USERNAME_PARAM, satelite.getUser());
    NameValuePair passwordParam = new NameValuePair(XMLRestWorker.PASSWORD_PARAM, satelite.getPassword());
    NameValuePair[] params = new NameValuePair[] { userParam, passwordParam };

    get.setQueryString(params);
    try {
        int statusCode = httpclient.executeMethod(get);
        if (statusCode != HttpStatus.SC_OK) {
            logger.error("Method failed: " + get.getStatusLine());
        }
        token = XMLRestWorker.parseToken(get.getResponseBodyAsString());
    } catch (HttpException e) {
        logger.error(e.getMessage());
    } catch (IOException e) {
        logger.error(e.getMessage());
    } catch (ParserConfigurationException e) {
        logger.error(e.getMessage());
    } catch (SAXException e) {
        logger.error(e.getMessage());
    } finally {
        get.releaseConnection();
    }
    return token;
}

From source file:net.neurowork.cenatic.centraldir.workers.xml.AbstractXmlImporter.java

protected T findRestEntity(Object id) {
    HttpClient httpclient = XMLRestWorker.getHttpClient();
    GetMethod get = new GetMethod(getRestUrl());
    get.addRequestHeader("Accept", "application/xml");
    T ret = null;//from  w  ww.  j ava  2  s .c  o m
    try {
        int result = httpclient.executeMethod(get);
        logger.info("Response status code: " + result);
        String xmlString = get.getResponseBodyAsString();
        logger.info("Response body: " + xmlString);

        ret = importEntity(xmlString, id);
    } catch (HttpException e) {
        logger.error(e.getMessage());
    } catch (IOException e) {
        logger.error(e.getMessage());
    } catch (ParserConfigurationException e) {
        logger.error(e.getMessage());
    } catch (SAXException e) {
        logger.error(e.getMessage());
    } finally {
        get.releaseConnection();
    }
    return ret;
}

From source file:de.avanux.livetracker.sender.LocationSender.java

private String executeHttpMethod(HttpMethodBase method) {
    String response = null;//from  w ww  .  j  ava2s. co m
    HttpClient client = new HttpClient();
    try {
        // Execute the method.
        int statusCode = client.executeMethod(method);

        if (statusCode != HttpStatus.SC_OK) {
            System.err.println("Method failed: " + method.getStatusLine());
        }

        // Read the response body.
        byte[] responseBody = method.getResponseBody();
        response = new String(responseBody);

    } catch (HttpException e) {
        System.err.println("Fatal protocol violation: " + e.getMessage());
        e.printStackTrace();
    } catch (IOException e) {
        System.err.println("Fatal transport error: " + e.getMessage());
        e.printStackTrace();
    } finally {
        // Release the connection.
        method.releaseConnection();
    }
    return response;
}

From source file:idea.clientRequests.httpClientRequester.java

public boolean doHttpget(String csUrl) {
    boolean bStatus = false;
    m_responseBody = null;/*from   www .  ja v a  2 s . c  o  m*/
    HttpClient client = new HttpClient();
    GetMethod method = new GetMethod(csUrl);

    // Provide custom retry handler is necessary
    DefaultHttpMethodRetryHandler handler = new DefaultHttpMethodRetryHandler(3, false);
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, handler);
    try {
        // Execute the method.
        int statusCode = client.executeMethod(method);
        if (statusCode != HttpStatus.SC_OK) {
            System.err.println("Method failed: " + method.getStatusLine());
        }
        // Read the response body.
        m_responseBody = method.getResponseBody();

        bStatus = true;
    } catch (HttpException e) {
        System.err.println("Fatal protocol violation: " + e.getMessage());
        e.printStackTrace();
    } catch (IOException e) {
        System.err.println("Fatal transport error: " + e.getMessage());
        e.printStackTrace();
    } finally {
        // Release the connection.
        method.releaseConnection();
    }
    return bStatus;
}

From source file:net.neurowork.cenatic.centraldir.workers.xml.SectorXmlImporter.java

public Sector buscarSector(int sectorCode) {
    HttpClient httpclient = XMLRestWorker.getHttpClient();
    GetMethod get = new GetMethod(restUrl.getSectorUrl());
    get.addRequestHeader("Accept", "application/xml");
    Sector ret = null;/*w  w w . j  ava2s. c om*/
    try {
        int result = httpclient.executeMethod(get);
        logger.info("Response status code: " + result);
        String xmlString = get.getResponseBodyAsString();
        logger.info("Response body: " + xmlString);

        ret = importSector(xmlString, sectorCode);
    } catch (HttpException e) {
        logger.error(e.getMessage());
    } catch (IOException e) {
        logger.error(e.getMessage());
    } catch (ParserConfigurationException e) {
        logger.error(e.getMessage());
    } catch (SAXException e) {
        logger.error(e.getMessage());
    } finally {
        get.releaseConnection();
    }
    return ret;
}

From source file:com.npower.dm.msm.tools.PackageCheckerImpl.java

public PackageMetaInfo getPackageMetaInfo(String url) throws DMException {
    HttpClient client = new HttpClient();
    HttpMethod method = new GetMethod(url);
    // Provide custom retry handler is necessary
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(3, false));

    PackageMetaInfo metaInfo = new PackageMetaInfo();
    metaInfo.setUrl(url);/*from  w w w  . jav  a 2 s .  c om*/
    try {
        // Execute the method.
        int statusCode = client.executeMethod(method);
        metaInfo.setServerStatus(statusCode);
        if (statusCode != HttpStatus.SC_OK) {
            // System.err.println("Method failed: " + method.getStatusLine());
        }

        // Read the response body.
        byte[] responseBody = method.getResponseBody();
        System.out.println(new String(responseBody));

        Header mimeType = method.getResponseHeader("Content-Type");
        if (mimeType != null) {
            metaInfo.setMimeType(mimeType.getValue());
        }

        Header contentLength = method.getResponseHeader("Content-Length");
        if (contentLength != null && StringUtils.isNotEmpty(contentLength.getValue())) {
            metaInfo.setSize(Integer.parseInt(contentLength.getValue()));
        }

    } catch (HttpException e) {
        metaInfo.setErrorMessage(e.getMessage());
    } catch (IOException e) {
        metaInfo.setErrorMessage(e.getMessage());
    } finally {
        // Release the connection.
        method.releaseConnection();
    }
    return metaInfo;
}

From source file:eu.eco2clouds.accounting.testbedclient.ClientHC.java

/**
 * Connects to an testbed URL to get the XML representations of Host Info
 * And it returns it as a null/*from w  w w . jav  a2  s.  c  om*/
 * @param testbed object representing the testbed from which we want to know the host info
 * @return A XML string with the Host info, if there was any HTTP error, it returns empty String... 
 */
public String getHostInfo(Testbed testbed) {
    // Create an instance of HttpClient.
    HttpClient client = new HttpClient();

    // Create a method instance.
    GetMethod method = new GetMethod(testbed.getUrl());

    // Provide custom retry handler is necessary
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(3, false));

    String response = "";

    try {
        // Execute the method.
        int statusCode = client.executeMethod(method);

        if (statusCode != HttpStatus.SC_OK) { //TODO test for this case... 
            logger.warn("Get host information of testbed: " + testbed.getName() + " failed: "
                    + method.getStatusLine());
        } else {
            // Read the response body.
            byte[] responseBody = method.getResponseBody();
            response = new String(responseBody);
        }

    } catch (HttpException e) {
        logger.warn("Fatal protocol violation: " + e.getMessage());
        e.printStackTrace();
    } catch (IOException e) {
        logger.warn("Fatal transport error: " + e.getMessage());
        e.printStackTrace();
    } finally {
        // Release the connection.
        method.releaseConnection();
    }

    return response;
}

From source file:fr.openwide.talendalfresco.rest.client.RestHttpTest.java

public void testRestAuthentication() {
    // Create an instance of HttpClient.
    HttpClient client = new HttpClient();

    // Create a method instance.
    GetMethod method = new GetMethod("http://www.google.com");

    // Provide custom retry handler is necessary
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(3, false));

    try {/*from w ww.jav a 2  s . co m*/
        // Execute the method.
        int statusCode = client.executeMethod(method);

        assertEquals(statusCode, HttpStatus.SC_OK);

        // Read the response body.
        byte[] responseBody = method.getResponseBody();

        assertTrue(responseBody != null && responseBody.length != 0);

        // Deal with the response.
        // Use caution: ensure correct character encoding and is not binary data
        System.out.println(new String(responseBody));

    } catch (HttpException e) {
        fail("Fatal protocol violation: " + e.getMessage());
        e.printStackTrace();
    } catch (IOException e) {
        fail("Fatal transport error: " + e.getMessage());
        e.printStackTrace();
    } finally {
        // Release the connection.
        method.releaseConnection();
    }
}

From source file:net.bioclipse.cir.business.CirManager.java

public ICDKMolecule getByName(String name) throws BioclipseException {
    HttpClient client = new HttpClient();
    String fullURL = SERVICE + name + "/sdf";
    System.out.println(("URL: " + fullURL));
    GetMethod method = new GetMethod(fullURL);
    addMethodHeaders(method, new HashMap<String, String>() {
        {//  w w  w.j a  va  2  s  .  c  om
            put("Accept", "text/plain");
        }
    });
    try {
        client.executeMethod(method);
        String content = method.getResponseBodyAsString();
        if (content.contains("http://www.w3.org/1999/xhtml")) {
            throw new BioclipseException("HTML page returned: probably multiple search hits");
        }
        InputStream reader = method.getResponseBodyAsStream();
        return cdk.loadMolecule(reader, (IChemFormat) MDLV2000Format.getInstance(), null, null);
    } catch (HttpException exception) {
        throw new BioclipseException("Error while accessing CIR: " + exception.getMessage(), exception);
    } catch (IOException exception) {
        throw new BioclipseException("Error while reading the CIR response: " + exception.getMessage(),
                exception);
    }
}

From source file:net.neurowork.cenatic.centraldir.workers.xml.CapacidadXmlImporter.java

public Capacidad buscarCapacidad(int capacityCode) {
    HttpClient httpclient = XMLRestWorker.getHttpClient();
    GetMethod get = new GetMethod(restUrl.getCapacidadUrl());
    get.addRequestHeader("Accept", "application/xml");
    Capacidad ret = null;// w w w. j a  va 2  s  .c o m
    try {
        int result = httpclient.executeMethod(get);
        if (logger.isTraceEnabled())
            logger.trace("Response status code: " + result);
        String xmlString = get.getResponseBodyAsString();
        if (logger.isTraceEnabled())
            logger.trace("Response body: " + xmlString);

        ret = importCapacidad(xmlString, capacityCode);
    } catch (HttpException e) {
        logger.error(e.getMessage());
    } catch (IOException e) {
        logger.error(e.getMessage());
    } catch (ParserConfigurationException e) {
        logger.error(e.getMessage());
    } catch (SAXException e) {
        logger.error(e.getMessage());
    } finally {
        get.releaseConnection();
    }
    return ret;
}