Example usage for org.apache.commons.httpclient HttpStatus SC_OK

List of usage examples for org.apache.commons.httpclient HttpStatus SC_OK

Introduction

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

Prototype

int SC_OK

To view the source code for org.apache.commons.httpclient HttpStatus SC_OK.

Click Source Link

Document

<tt>200 OK</tt> (HTTP/1.0 - RFC 1945)

Usage

From source file:edu.wisc.my.portlets.bookmarks.domain.CollectionFolder.java

/**
 * Returns an immutable sorted view of the values of the children Map. The sorting is done
 * using the current childComparator. Warning, this is has a time cost of 2n log(n)
 * on every call.//from   w  w  w  .j  av a  2s.c o m
 * 
 * @return An immutable sorted view of the folder's children.
 */
public List<Entry> getSortedChildren() {
    List<Entry> children = new ArrayList<Entry>();
    log.debug("children: " + children.size());

    HttpClient client = new HttpClient();
    GetMethod get = null;

    try {

        log.debug("getting url " + url);
        get = new GetMethod(url);
        int rc = client.executeMethod(get);
        if (rc != HttpStatus.SC_OK) {
            log.error("HttpStatus:" + rc);
        }

        DocumentBuilderFactory domBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = domBuilderFactory.newDocumentBuilder();

        InputStream in = get.getResponseBodyAsStream();
        builder = domBuilderFactory.newDocumentBuilder();
        Document doc = builder.parse(in);
        get.releaseConnection();

        Element e = (Element) doc.getElementsByTagName("rdf:RDF").item(0);
        log.debug("got root " + e);
        NodeList n = e.getElementsByTagName("item");
        log.debug("found items " + n.getLength());
        for (int i = 0; i < n.getLength(); i++) {
            Bookmark bookmark = new Bookmark();
            Element l = (Element) n.item(i);
            bookmark.setName(((Element) l.getElementsByTagName("title").item(0)).getTextContent());
            bookmark.setUrl(((Element) l.getElementsByTagName("link").item(0)).getTextContent());
            if (l.getElementsByTagName("description").getLength() > 0) {
                bookmark.setNote(((Element) l.getElementsByTagName("description").item(0)).getTextContent());
            }
            children.add(bookmark);
            log.debug("added bookmark " + bookmark.getName() + " " + bookmark.getUrl());
        }

    } catch (HttpException e) {
        log.error("Error parsing delicious", e);
    } catch (IOException e) {
        log.error("Error parsing delicious", e);
    } catch (ParserConfigurationException e) {
        log.error("Error parsing delicious", e);
    } catch (SAXException e) {
        log.error("Error parsing delicious", e);
    } finally {
        if (get != null)
            get.releaseConnection();
    }

    log.debug("children: " + children.size());
    return children;
}

From source file:com.braindrainpain.docker.httpsupport.HttpClientService.java

public JsonArray getJsonElementsFromUrl(String url, String memberName) {
    JsonArray result = null;//from  w w w  .  j  a  v  a 2 s .c om
    try {
        GetMethod get = new GetMethod(url);
        if (httpClient.executeMethod(get) == HttpStatus.SC_OK) {
            String jsonString = get.getResponseBodyAsString();
            LOG.info("RECIEVED: " + jsonString);
            result = parseJson(jsonString, memberName);
            LOG.info("Build result: " + result);
        }
    } catch (IOException e) {
        // Wrap into a runtime. There is nothing useful to do here
        // when this happens.
        LOG.error("cannot fetch the tags from " + url);
        throw new RuntimeException("Cannot fetch the tags from " + url, e);
    }

    return result;
}

From source file:com.mikenimer.familydam.web.jobs.FacebookLikesJob.java

@Override
protected JobResult queryFacebook(Node facebookData, String username, String userPath, String nextUrl)
        throws RepositoryException, IOException, JSONException {
    String accessToken = facebookData.getProperty("accessToken").getString();
    String expiresIn = facebookData.getProperty("expiresIn").getString();
    String signedRequest = facebookData.getProperty("signedRequest").getString();

    String userId = "me";
    if (facebookData.hasProperty("userId")) {
        userId = facebookData.getProperty("userId").getString();
    }//from  w ww .  j  a  va 2  s . c  o  m

    String _url = nextUrl;
    if (nextUrl == null) {
        _url = "https://graph.facebook.com/" + userId + "/likes?access_token=" + accessToken;
    }
    URL url = new URL(_url);

    HttpClient client = new HttpClient();
    GetMethod method = new GetMethod(_url);
    int statusCode = client.executeMethod(method);

    if (statusCode != HttpStatus.SC_OK) {
        return JobResult.FAILED;
    }

    // Read the response body.
    String jsonStr = method.getResponseBodyAsString();
    return saveData(username, jsonStr, FACEBOOKPATH, "like");
}

From source file:com.jackbe.mapreduce.RESTClient.java

public void executeREST(String encodedValue) {
    String result = null;//from w  w  w .j av a 2 s  .  c o  m
    String postPath = protocol + host + ":" + port + path;
    System.out.println("post path = " + postPath);
    PostMethod pm = new PostMethod(postPath);
    System.out.println("10 Encoded value = \n" + encodedValue);
    if (encodedValue != null) {
        //httpMethod = new PostMethod(protocol + host + ":" + port + path);
        //InputStream is = new ByteArrayInputStream(encodedValue.getBytes());
        //pm.setRequestBody(is);
        pm.setRequestBody(encodedValue);
        System.out.println("Validate = " + pm.validate());
        //pm.setQueryString(encodedValue);

        //pm.setHttp11(false);

        log.debug("Invoking REST service: " + protocol + host + ":" + port + path + " " + pm.toString());

    } else {
        throw new RuntimeException("EncodedValue can't be null");
    }

    try {
        client.executeMethod(pm);

        if (pm.getStatusCode() != HttpStatus.SC_OK) {
            log.error("HTTP Error status connecting to Presto: " + pm.getStatusCode());
            log.error("HTTP Error message connecting to Presto: " + pm.getStatusText());
            return;
        } else {
            if (log.isDebugEnabled()) {
                log.debug("Status code: " + pm.getStatusCode());
                Header contentTypeHeader = pm.getResponseHeader("content-type");
                log.debug("Mimetype: " + contentTypeHeader.getValue());
            }
        }

        result = pm.getResponseBodyAsString();
        // log.debug(httpMethod.getStatusText());
        if (log.isDebugEnabled())
            log.debug("Response: " + result);
    } catch (Exception e) {
        log.error("Exception executing REST call: " + e, e);
    } finally {
        pm.releaseConnection();
    }
}

From source file:com.mikenimer.familydam.web.jobs.FacebookPhotosJob.java

@Override
protected JobResult queryFacebook(Node facebookData, String username, String userPath, String nextUrl)
        throws RepositoryException, IOException, JSONException {
    String accessToken = facebookData.getProperty("accessToken").getString();
    String expiresIn = facebookData.getProperty("expiresIn").getString();
    String signedRequest = facebookData.getProperty("signedRequest").getString();

    String userId = "me";
    if (facebookData.hasProperty("userId")) {
        userId = facebookData.getProperty("userId").getString();
    }// w ww  . j  av a 2s.co m

    String _url = nextUrl;
    if (nextUrl == null) {
        _url = "https://graph.facebook.com/" + userId + "/photos?access_token=" + accessToken;
    }
    URL url = new URL(_url);

    HttpClient client = new HttpClient();
    GetMethod method = new GetMethod(_url);
    int statusCode = client.executeMethod(method);

    if (statusCode != HttpStatus.SC_OK) {
        return JobResult.FAILED;
    }

    // Read the response body.
    String jsonStr = method.getResponseBodyAsString();
    return saveData(username, jsonStr, FACEBOOKPATH, "photo");
}

From source file:net.sf.sail.webapp.dao.sds.impl.SdsCurnitMapGetter.java

private HttpGetCurnitMapRequest generateCurnitMapRequest(SdsOffering sdsOffering) {
    final String url;
    if (sdsOffering.getRetrieveContentUrl() != null) {
        url = "/offering/" + sdsOffering.getSdsObjectId() + "/curnitmap" + "?sailotrunk.otmlurl="
                + sdsOffering.getRetrieveContentUrl();
    } else {//from  w  w  w  .  ja  va 2  s . c o  m
        url = "/offering/" + sdsOffering.getSdsObjectId() + "/curnitmap";
    }

    return new HttpGetCurnitMapRequest(AbstractHttpRestCommand.REQUEST_HEADERS_ACCEPT,
            AbstractHttpRestCommand.EMPTY_STRING_MAP, url, HttpStatus.SC_OK);
}

From source file:com.mikenimer.familydam.web.jobs.FacebookStatusJob.java

@Override
protected JobResult queryFacebook(Node facebookData, String username, String userPath, String nextUrl)
        throws RepositoryException, IOException, JSONException {
    String accessToken = facebookData.getProperty("accessToken").getString();
    String expiresIn = facebookData.getProperty("expiresIn").getString();
    String signedRequest = facebookData.getProperty("signedRequest").getString();

    String userId = "me";
    if (facebookData.hasProperty("userId")) {
        userId = facebookData.getProperty("userId").getString();
    }/*from   w  w w .  j  a  v  a  2  s  .  c  o  m*/

    String _url = nextUrl;
    if (nextUrl == null) {
        _url = "https://graph.facebook.com/" + userId + "/statuses?access_token=" + accessToken;
    }
    URL url = new URL(_url);

    HttpClient client = new HttpClient();
    GetMethod method = new GetMethod(_url);
    int statusCode = client.executeMethod(method);

    if (statusCode != HttpStatus.SC_OK) {
        return JobResult.FAILED;
    }

    // Read the response body.
    String jsonStr = method.getResponseBodyAsString();
    return saveData(username, jsonStr, FACEBOOKPATH, "status");
}

From source file:com.owncloud.android.lib.resources.files.ExistenceCheckRemoteOperation.java

@Override
protected RemoteOperationResult run(OwnCloudClient client) {
    if (!isOnline()) {
        return new RemoteOperationResult(RemoteOperationResult.ResultCode.NO_NETWORK_CONNECTION);
    }/* ww w. j  a v a2s  .c o m*/
    RemoteOperationResult result = null;
    HeadMethod head = null;
    try {
        head = new HeadMethod(client.getWebdavUri() + WebdavUtils.encodePath(mPath));
        int status = client.executeMethod(head, TIMEOUT, TIMEOUT);
        client.exhaustResponse(head.getResponseBodyAsStream());
        boolean success = (status == HttpStatus.SC_OK && !mSuccessIfAbsent)
                || (status == HttpStatus.SC_NOT_FOUND && mSuccessIfAbsent);
        result = new RemoteOperationResult(success, status, head.getResponseHeaders());
        Log_OC.d(TAG,
                "Existence check for " + client.getWebdavUri() + WebdavUtils.encodePath(mPath)
                        + " targeting for " + (mSuccessIfAbsent ? " absence " : " existence ")
                        + "finished with HTTP status " + status + (!success ? "(FAIL)" : ""));

    } catch (Exception e) {
        result = new RemoteOperationResult(e);
        Log_OC.e(TAG,
                "Existence check for " + client.getWebdavUri() + WebdavUtils.encodePath(mPath)
                        + " targeting for " + (mSuccessIfAbsent ? " absence " : " existence ") + ": "
                        + result.getLogMessage(),
                result.getException());

    } finally {
        if (head != null)
            head.releaseConnection();
    }
    return result;
}

From source file:com.mikenimer.familydam.web.jobs.FacebookCheckinsJob.java

@Override
protected JobResult queryFacebook(Node facebookData, String username, String userPath, String nextUrl)
        throws RepositoryException, IOException, JSONException {
    String accessToken = facebookData.getProperty("accessToken").getString();
    String expiresIn = facebookData.getProperty("expiresIn").getString();
    String signedRequest = facebookData.getProperty("signedRequest").getString();

    String userId = "me";
    if (facebookData.hasProperty("userId")) {
        userId = facebookData.getProperty("userId").getString();
    }//from   w  w w  .j  ava  2  s  .co m

    String _url = nextUrl;
    if (nextUrl == null) {
        _url = "https://graph.facebook.com/" + userId + "/checkins?access_token=" + accessToken;
    }
    URL url = new URL(_url);

    HttpClient client = new HttpClient();
    GetMethod method = new GetMethod(_url);
    int statusCode = client.executeMethod(method);

    if (statusCode != HttpStatus.SC_OK) {
        return JobResult.FAILED;
    }

    // Read the response body.
    String jsonStr = method.getResponseBodyAsString();
    return saveData(username, jsonStr, FACEBOOKPATH, "checkin");
}

From source file:de.escidoc.core.test.examples.RetrieveCompressedExamplesIT.java

@Test
public void testRetrieveCompressedExampleOrganizationalUnits() throws Exception {

    for (int i = 0; i < EXAMPLE_OU_IDS.length; ++i) {

        String ou = handleXmlResult(ouClient.retrieve(EXAMPLE_OU_IDS[i]));

        String response = null;// www . ja v a  2  s .c  o  m

        String url = "http://localhost:8080" + Constants.ORGANIZATIONAL_UNIT_BASE_URI + "/" + EXAMPLE_OU_IDS[i];

        HttpClient client = new HttpClient();

        try {
            if (PropertiesProvider.getInstance().getProperty("http.proxyHost") != null
                    && PropertiesProvider.getInstance().getProperty("http.proxyPort") != null) {
                ProxyHost proxyHost = new ProxyHost(
                        PropertiesProvider.getInstance().getProperty("http.proxyHost"),
                        Integer.parseInt(PropertiesProvider.getInstance().getProperty("http.proxyPort")));

                client.getHostConfiguration().setProxyHost(proxyHost);
            }
        } catch (final Exception e) {
            throw new RuntimeException("[ClientBase] Error occured loading properties! " + e.getMessage(), e);
        }

        GetMethod getMethod = new GetMethod(url);
        getMethod.setRequestHeader("Accept-Encoding", "gzip");

        InputStream responseBody = null;
        try {

            int statusCode = client.executeMethod(getMethod);

            if (statusCode != HttpStatus.SC_OK) {
                throw new RuntimeException("getMethod failed:" + getMethod.getStatusLine());
            }
            responseBody = new GZIPInputStream(getMethod.getResponseBodyAsStream());

            BufferedReader bufferedReader = new BufferedReader(
                    new InputStreamReader(responseBody, getMethod.getResponseCharSet()));
            StringBuffer result = new StringBuffer();

            char[] buffer = new char[4 * 1024];
            int charsRead;
            while ((charsRead = bufferedReader.read(buffer)) != -1) {
                result.append(buffer, 0, charsRead);
            }

            response = result.toString();
        } catch (Exception e) {
            throw new RuntimeException("Error occured in retrieving compressed example! " + e.getMessage(), e);
        }

        assertXmlEquals("Compressed document not the same like the uncompressed one", ou, response);
    }
}