Example usage for org.apache.commons.httpclient.methods GetMethod GetMethod

List of usage examples for org.apache.commons.httpclient.methods GetMethod GetMethod

Introduction

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

Prototype

public GetMethod(String uri) 

Source Link

Usage

From source file:eu.flatworld.worldexplorer.layer.bmng.BMNGHTTPProvider.java

@Override
public void run() {
    while (true) {
        while (getQueueSize() != 0) {
            Tile tile = peekTile();//from w w  w .  j av a 2  s. co  m
            synchronized (tile) {
                String s = String.format(BMNGLayer.HTTP_BASE, year, month, tile.getL(), tile.getX(),
                        tile.getY());
                LogX.log(Level.FINER, s);
                GetMethod gm = new GetMethod(s);
                gm.setRequestHeader("User-Agent", WorldExplorer.USER_AGENT);
                try {
                    int response = client.executeMethod(gm);
                    LogX.log(Level.FINEST, NAME + " " + response + " " + s);
                    if (response == HttpStatus.SC_OK) {
                        InputStream is = gm.getResponseBodyAsStream();
                        BufferedImage bi = ImageIO.read(is);
                        is.close();
                        if (bi != null) {
                            tile.setImage(bi);
                        }
                    }
                } catch (Exception ex) {
                    LogX.log(Level.FINER, "", ex);
                } finally {
                    gm.releaseConnection();
                }
                LogX.log(Level.FINEST, NAME + " dequeueing: " + tile);
                unqueueTile(tile);
            }
            firePropertyChange(P_DATAREADY, null, tile);
            Thread.yield();
        }
        firePropertyChange(P_IDLE, false, true);
        try {
            Thread.sleep(QUEUE_SLEEP_TIME);
        } catch (Exception ex) {
        }
        ;
    }
}

From source file:gov.va.vinci.leo.tools.JamServiceTest.java

@Test(expected = Exception.class)
public void doHttpCallExceptionTest() throws IOException {
    HttpClientMock mockHttpClient = new HttpClientMock(100, "test");
    JamService jamService = new JamService("http://localhost/jam");
    jamService.setClient(mockHttpClient);
    String result = jamService.doHttpCall(new GetMethod(
            jamService.getJamServerBaseUrl() + "webservice/removeServiceQueue/" + URLEncoder.encode("mydata")));

    assertFalse(true);/*from w w w.  j  a  v a2 s .  com*/
}

From source file:com.grandcircus.controller.GoogleGeocode.java

/**
 * Given the string of an address, return a Coordinates object that contains
 * the coordinate information extracted from the API response
 *
 * @param address the string representation of a physical address
 * @return Coordinates object containing coordinate information from response
 *//*from  w  w  w.  j av  a  2  s  .  c om*/
public static Coordinates geocode(String address) {
    Coordinates coords = new Coordinates();

    String requestUrl = "";
    try {
        requestUrl = buildURL(address);
    } catch (UnsupportedEncodingException uee) {
        uee.printStackTrace();
    }

    GetMethod getMethod = new GetMethod(requestUrl);
    getMethod.setFollowRedirects(true);

    try {
        httpClient.executeMethod(getMethod);

        // build the response object
        GoogleGeocodeResponse response = new GoogleGeocodeResponse(
                IOUtils.toString(getMethod.getResponseBodyAsStream(), getMethod.getRequestCharSet()));
        /**
         * Parsing can be done via Dom Document as well:
         * GoogleGeocodeResponse response = new GoogleGeocodeResponse(getMethod.getResponseBodyAsStream());
         */

        // only change coordinates from default if request successful
        if (response.successful()) {
            coords = response.getCoords();
        }
    } catch (Exception e) {
        System.out.println("Geocode exception: " + e.toString());
    } finally {
        getMethod.abort();
        getMethod.releaseConnection();
    }

    return coords;
}

From source file:com.hmsinc.epicenter.model.geography.util.GeocoderDotUSClient.java

public Geometry geocode(String address, String city, String state, String zipcode) {

    Validate.notNull(address);/* www  .  j a va2  s.  com*/
    Validate.notNull(city);
    Validate.notNull(state);
    Validate.notNull(zipcode);

    Geometry g = null;

    try {

        final GetMethod get = new GetMethod(geoCoderURL);

        final NameValuePair[] query = { new NameValuePair("address", address), new NameValuePair("city", city),
                new NameValuePair("state", state), new NameValuePair("zipcode", zipcode) };

        get.setQueryString(query);
        httpClient.executeMethod(get);

        final String response = get.getResponseBodyAsString();
        get.releaseConnection();

        if (response != null) {
            final StrTokenizer tokenizer = StrTokenizer.getCSVInstance(response);
            if (tokenizer.size() == 5) {

                final Double latitude = Double.valueOf(tokenizer.nextToken());
                final Double longitude = Double.valueOf(tokenizer.nextToken());

                g = factory.createPoint(new Coordinate(longitude, latitude));
                logger.debug("Geometry: " + g.toString());
            }
        }

    } catch (HttpException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    return g;
}

From source file:com.appdynamics.cloudstack.CloudStackApiClient.java

public String execute() throws Exception {

    HttpClient client = null;//from   ww w .  ja  v a  2 s  . c  o m
    GetMethod get = null;

    try {

        String requestUrl = generateRequestUrl();
        System.out.println(requestUrl);
        client = new HttpClient();
        get = new GetMethod(requestUrl);
        client.executeMethod(get);

        return get.getResponseBodyAsString();
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    } finally {
        get.releaseConnection();
    }

}

From source file:com.rometools.propono.atom.client.ClientAtomService.java

/**
 * Get full entry from service by entry edit URI.
 *//* w w  w  . j a v  a  2  s .c o  m*/
public ClientEntry getEntry(final String uri) throws ProponoException {
    final GetMethod method = new GetMethod(uri);
    authStrategy.addAuthentication(httpClient, method);
    try {
        httpClient.executeMethod(method);
        if (method.getStatusCode() != 200) {
            throw new ProponoException("ERROR HTTP status code=" + method.getStatusCode());
        }
        final Entry romeEntry = Atom10Parser.parseEntry(new InputStreamReader(method.getResponseBodyAsStream()),
                uri, Locale.US);
        if (!romeEntry.isMediaEntry()) {
            return new ClientEntry(this, null, romeEntry, false);
        } else {
            return new ClientMediaEntry(this, null, romeEntry, false);
        }
    } catch (final Exception e) {
        throw new ProponoException("ERROR: getting or parsing entry/media", e);
    } finally {
        method.releaseConnection();
    }
}

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

private Configuration requestConfiguration(String url) {
    log.debug("Requesting configuration ...");
    Configuration configuration = null;
    GetMethod method = new GetMethod(url);
    executeHttpMethod(method);/*from   w w  w .j  ava  2s .  c o  m*/
    try {
        byte[] responseBody = method.getResponseBody();
        if (responseBody.length > 0) {
            String response = new String(responseBody);
            log.debug("Response: " + response);
            configuration = new Configuration(response);
        } else {
            log.error("No configuration received.");
        }
    } catch (IOException e) {
        log.error(e);
    }
    log.debug("... configuration received.");
    return configuration;
}

From source file:com.sun.syndication.propono.atom.client.ClientAtomService.java

/**
 * Get full entry from service by entry edit URI.
 *//*from  w  w  w  .j av a 2 s  .  c  om*/
public ClientEntry getEntry(String uri) throws ProponoException {
    GetMethod method = new GetMethod(uri);
    authStrategy.addAuthentication(httpClient, method);
    try {
        httpClient.executeMethod(method);
        if (method.getStatusCode() != 200) {
            throw new ProponoException("ERROR HTTP status code=" + method.getStatusCode());
        }
        Entry romeEntry = Atom10Parser.parseEntry(new InputStreamReader(method.getResponseBodyAsStream()), uri);
        if (!romeEntry.isMediaEntry()) {
            return new ClientEntry(this, null, romeEntry, false);
        } else {
            return new ClientMediaEntry(this, null, romeEntry, false);
        }
    } catch (Exception e) {
        throw new ProponoException("ERROR: getting or parsing entry/media", e);
    } finally {
        method.releaseConnection();
    }
}

From source file:com.twitter.tokyo.kucho.SeatingList.java

String loadJSON(String url) throws IOException {
    HttpClient client = new HttpClient();
    HttpMethod getMethod = new GetMethod(url);
    int statusCode = client.executeMethod(getMethod);
    if (statusCode != HttpStatus.SC_OK) {
        throw new IOException("Server returned " + statusCode);
    }/* ww  w  . ja va  2 s. c o m*/

    return getMethod.getResponseBodyAsString();
}

From source file:eu.impact_project.iif.t2.client.InfoGenerator.java

/**
 * Gets information about a workflow./*from ww  w  . j  a  v a 2s  .  c  o m*/
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    HttpSession session = request.getSession(true);
    String wfId = request.getParameter("id");
    String urlString = "http://www.myexperiment.org/workflow.xml?id=" + wfId;

    String user = (String) session.getAttribute("user");
    String password = (String) session.getAttribute("password");

    // prepare httpclient for basic authentication
    HttpClient client = new HttpClient();
    client.getParams().setAuthenticationPreemptive(true);
    client.getState().setCredentials(new AuthScope("www.myexperiment.org", 80),
            new UsernamePasswordCredentials(user, password));

    GetMethod get = new GetMethod(urlString);
    get.setDoAuthentication(true);

    client.executeMethod(get);
    InputStream responseBody = get.getResponseBodyAsStream();

    try {

        SAXBuilder builder = new SAXBuilder();

        Document doc = builder.build(responseBody);

        WorkflowDetails details = new WorkflowDetails();

        Element root = doc.getRootElement();
        details.setTitle(root.getChild("title").getTextTrim());
        details.setDescription(root.getChild("description").getTextTrim());
        details.setImageUrl(root.getChild("preview").getTextTrim());

        session.setAttribute("wfDetails", details);

    } catch (JDOMException e) {
        e.printStackTrace();
    }

    RequestDispatcher rd = getServletContext().getRequestDispatcher("/info.jsp");
    rd.forward(request, response);
}