Example usage for org.apache.commons.httpclient HttpClient HttpClient

List of usage examples for org.apache.commons.httpclient HttpClient HttpClient

Introduction

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

Prototype

public HttpClient() 

Source Link

Usage

From source file:com.sun.syndication.propono.blogclient.metaweblog.MetaWeblogResource.java

/**
 * Get media resource as input stream.//from  w w  w.j  av a  2 s .  co m
 */
public InputStream getAsStream() throws BlogClientException {
    HttpClient httpClient = new HttpClient();
    GetMethod method = new GetMethod(permalink);
    try {
        httpClient.executeMethod(method);
    } catch (Exception e) {
        throw new BlogClientException("ERROR: error reading file", e);
    }
    if (method.getStatusCode() != 200) {
        throw new BlogClientException("ERROR HTTP status=" + method.getStatusCode());
    }
    try {
        return method.getResponseBodyAsStream();
    } catch (Exception e) {
        throw new BlogClientException("ERROR: error reading file", e);
    }
}

From source file:com.miragedev.mononara.core.service.MononaraServiceImpl.java

public MononaraServiceImpl() throws URISyntaxException {
    ResteasyProviderFactory resteasyProviderFactory = ResteasyProviderFactory.getInstance();
    RegisterBuiltin.register(resteasyProviderFactory);
    HttpClient httpClient = new HttpClient();
    URI uri = new URI("http://localhost:8080/kanji-portal/");
    //kanjiPortalResource = ProxyFactory.create(KanjiPortalResource.class, uri, httpClient, resteasyProviderFactory);
    kanjiPortalResource = ProxyFactory.create(KanjiPortalResource.class, "http://www.kanji-portal.com");
}

From source file:eu.eco2clouds.api.bonfire.client.rest.RestClient.java

private static HttpClient getClient(String url, String username, String password) {
    // Disabling strict JAVA SSL security... 
    enableSSLUnsecureTrustStore();//ww  w .  ja v  a2  s  .c  o  m

    HttpClient client = new HttpClient();
    // We set the credentials
    client.getState().setCredentials(new AuthScope(stripURL(url), port, AuthScope.ANY_REALM),
            new UsernamePasswordCredentials(username, password));
    client.getParams().setAuthenticationPreemptive(true);

    return client;
}

From source file:com.universalmind.core.components.mockup.loremipsum.SchmIpsumService.java

@Test
public void testService() {
    String schmipsumSource = "bible";
    Integer paragraphs = 2;/*from  w ww  .  j a va2 s  . c  om*/

    String loremText = null;
    String url = null;
    url = "http://www.schmipsum.com/ipsum/" + schmipsumSource + "/" + (paragraphs * 100);

    HttpMethod method = null;
    HttpClient client = new HttpClient();
    try {
        method = new GetMethod(url);
        int statusCode = client.executeMethod(method);
        byte[] responseBody = method.getResponseBody();

        // Create a response handler
        //ResponseHandler<String> responseHandler = new BasicResponseHandler();
        //String responseBody = httpclient.execute(httpget, responseHandler);

        loremText = parseBaseballIpsum(new String(responseBody));

        Assert.assertNotNull(loremText);
        Assert.assertTrue(loremText.length() > 100);
    } catch (Exception jse) {
        //do nothing
    } finally {
        // When HttpClient instance is no longer needed,
        // shut down the connection manager to ensure
        // immediate deallocation of all system resources
        //httpclient.getConnectionManager().shutdown();
        if (method != null) {
            method.releaseConnection();
        }
    }

}

From source file:com.intuit.tank.http.soap.SOAPRequestTest.java

/**
 * Run the SOAPRequest(HttpClient) constructor test.
 *
 * @throws Exception/*from  w  w  w.j av  a  2  s . com*/
 *
 * @generatedBy CodePro at 12/16/14 4:29 PM
 */
@Test
public void testSOAPRequest_3() throws Exception {
    HttpClient client = new HttpClient();

    SOAPRequest result = new SOAPRequest(client);

    // An unexpected exception was thrown in user code while executing this test:
    //    java.lang.NoClassDefFoundError: Could not initialize class org.apache.commons.httpclient.HttpClient
    assertNotNull(result);
}

From source file:com.intuit.tank.http.xml.XMLRequestTest.java

/**
 * Run the String getKey(String) method test.
 * /*  w  w  w. j a v a 2  s. com*/
 * @throws Exception
 * 
 * @generatedBy CodePro at 12/16/14 4:29 PM
 */
@Test
public void testGetKey_1() throws Exception {
    XMLRequest fixture = new XMLRequest(new HttpClient());
    fixture.handler = new GenericXMLHandler();
    String key = "";

    String result = fixture.getKey(key);
    assertNotNull(result);
}

From source file:com.prashsoft.javakiva.KivaUtil.java

public static Object getBeanResponse(String urlSuffix, String urlMethod, String urlParams) {

    Object bean = null;/*  w ww . ja va 2 s  . c om*/

    String url = createKivaAPIUrl(urlSuffix, urlMethod, urlParams);

    // Create an instance of HttpClient.
    HttpClient client = new HttpClient();

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

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

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

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

        // Read the response body.

        InputStream is = method.getResponseBodyAsStream();

        BufferedReader in = new BufferedReader(new InputStreamReader(is));

        String datastr = null;
        StringBuffer sb = new StringBuffer();

        String inputLine;

        while ((inputLine = in.readLine()) != null)
            sb.append(inputLine);

        in.close();
        is.close();

        String response = sb.toString();

        // Deal with the response.
        JSONObject jsonObject = JSONObject.fromObject(response);

        bean = JSONObject.toBean(jsonObject);

    } 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();
    } catch (Exception e) {
        System.err.println("Fatal general error: " + e.getMessage());
        e.printStackTrace();
    } finally {
        // Release the connection.
        method.releaseConnection();
    }

    return bean;

}

From source file:att.jaxrs.server.TagService.java

@GET
@Path("/tags/{name}/")
public Response getTag(@PathParam("name") Long name) {
    System.out.println("----invoking getTag, tag id is: " + name);

    GetMethod get = new GetMethod(Constants.SELECT_WITH_NAME_TAG_RESOURCE + name);

    TagCollection tag = new TagCollection();
    try {/*from  w ww.j  a va2  s  . co  m*/

        HttpClient httpClient = new HttpClient();
        try {
            int result = httpClient.executeMethod(get);
            System.out.println(Constants.RESPONSE_STATUS_CODE + result);
            tag = Marshal.unmarshal(TagCollection.class, get.getResponseBodyAsStream());

        } finally {
            get.releaseConnection();
        }
    } catch (Exception e) {

    }
    return Response.ok(tag).build();
}

From source file:edu.unc.lib.dl.ui.service.DjatokaContentService.java

public void getMetadata(String simplepid, String datastream, OutputStream outStream,
        HttpServletResponse response, int retryServerError) {
    HttpClientParams params = new HttpClientParams();
    params.setContentCharset("UTF-8");
    HttpClient client = new HttpClient();
    client.setParams(params);/*w ww .  j  a  v  a 2  s .  c o m*/

    StringBuilder path = new StringBuilder(applicationPathSettings.getDjatokaPath());
    path.append("resolver?url_ver=Z39.88-2004&rft_id=")
            .append(applicationPathSettings.getFedoraPathWithoutDefaultPort()).append("/objects/")
            .append(simplepid).append("/datastreams/").append(datastream).append("/content")
            .append("&svc_id=info:lanl-repo/svc/getMetadata");

    GetMethod method = new GetMethod(path.toString());
    try {
        client.executeMethod(method);
        if (method.getStatusCode() == HttpStatus.SC_OK) {
            if (response != null) {
                response.setHeader("Content-Type", "application/json");
                response.setHeader("content-disposition", "inline");

                FileIOUtil.stream(outStream, method);
            }
        } else {
            if ((method.getStatusCode() == 500 || method.getStatusCode() == 404) && retryServerError > 0) {
                this.getMetadata(simplepid, datastream, outStream, response, retryServerError - 1);
            } else {
                LOG.error("Unexpected failure: " + method.getStatusLine().toString());
                LOG.error("Path was: " + method.getURI().getURI());
            }
        }
    } catch (ClientAbortException e) {
        if (LOG.isDebugEnabled())
            LOG.debug("User client aborted request to stream jp2 metadata for " + simplepid, e);
    } catch (Exception e) {
        LOG.error("Problem retrieving metadata for " + path, e);
    } finally {
        method.releaseConnection();
    }
}

From source file:com.bluexml.xforms.demo.Util.java

/**
 * Authenticates a user with an Alfresco instance.
 * //from w  w w .  j a  v  a 2s  .  c o  m
 * @param host
 *            the address (protocol, hostname, port number) of the host where the BlueXML XForms
 *            webscript is deployed, with NO trailing slash. If NULL, defaults to localhost:8080
 * @param userName
 *            the user name to test, which should be known to Alfresco
 * @param password
 *            the plain text password to test
 * @return true if the authentication succeeded, false if the authentication failed or if an
 *         exception occurred
 */
public static boolean authenticate(String host, String userName, String password) {
    PostMethod post = new PostMethod(host + "service/xforms/auth");

    post.setParameter("username", userName);
    post.setParameter("password", password);

    HttpClient client = new HttpClient();
    try {
        client.executeMethod(post);
    } catch (HttpException e) {
        e.printStackTrace();
        return false;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
    String result;
    try {
        result = post.getResponseBodyAsString();
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
    if (result == null) {
        return false;
    }
    result = result.trim();
    return result.equals("success");
}