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

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

Introduction

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

Prototype

public void setQueryString(String queryString) 

Source Link

Usage

From source file:eu.learnpad.core.impl.or.XwikiCoreFacadeRestResource.java

@Override
public InputStream getModel(String modelSetId, ModelSetType type) throws LpRestException {
    // Now send the package's path to the importer for XWiki
    HttpClient httpClient = this.getClient();
    String uri = String.format("%s/learnpad/or/corefacade/getmodel/%s", DefaultRestResource.REST_URI,
            modelSetId);/*from  w ww .  j  a  v  a  2s  . c  om*/
    GetMethod getMethod = new GetMethod(uri);
    getMethod.addRequestHeader("Accept", "application/xml");

    NameValuePair[] queryString = new NameValuePair[1];
    queryString[0] = new NameValuePair("type", type.toString());
    getMethod.setQueryString(queryString);

    InputStream model = null;
    try {
        httpClient.executeMethod(getMethod);
        model = getMethod.getResponseBodyAsStream();
    } catch (IOException e) {
        throw new LpRestExceptionXWikiImpl(e.getMessage(), e.getCause());
    }
    return model;
}

From source file:eu.learnpad.core.impl.or.XwikiCoreFacadeRestResource.java

@Override
public InputStream getExternalKPIs(String modelSetId, String kpisid, KPIsFormat type) throws LpRestException {
    // Now send the package's path to the importer for XWiki
    HttpClient httpClient = this.getClient();
    String uri = String.format("%s/learnpad/or/corefacade/getkpis/%s/%s", DefaultRestResource.REST_URI,
            modelSetId, kpisid);/*from w w  w  . j  av a2 s .  com*/
    GetMethod getMethod = new GetMethod(uri);
    getMethod.addRequestHeader("Accept", "application/xml");

    NameValuePair[] queryString = new NameValuePair[1];
    queryString[0] = new NameValuePair("type", type.toString());
    getMethod.setQueryString(queryString);

    InputStream kpisStream = null;
    try {
        httpClient.executeMethod(getMethod);
        kpisStream = getMethod.getResponseBodyAsStream();
    } catch (IOException e) {
        throw new LpRestExceptionXWikiImpl(e.getMessage(), e.getCause());
    }
    return kpisStream;
}

From source file:com.owncloud.android.lib.resources.shares.GetRemoteSharesForFileOperation.java

@Override
protected RemoteOperationResult run(OwnCloudClient client) {
    RemoteOperationResult result = null;
    int status = -1;

    GetMethod get = null;

    try {//w w w  .j a va 2 s. co  m
        // Get Method
        get = new GetMethod(client.getBaseUri() + ShareUtils.SHARING_API_PATH);

        // Add Parameters to Get Method
        get.setQueryString(new NameValuePair[] { new NameValuePair(PARAM_PATH, mRemoteFilePath),
                new NameValuePair(PARAM_RESHARES, String.valueOf(mReshares)),
                new NameValuePair(PARAM_SUBFILES, String.valueOf(mSubfiles)) });

        get.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE);

        status = client.executeMethod(get);

        if (isSuccess(status)) {
            String response = get.getResponseBodyAsString();

            result = new RemoteOperationResult(ResultCode.OK);

            // Parse xml response --> obtain the response in ShareFiles ArrayList
            // convert String into InputStream
            InputStream is = new ByteArrayInputStream(response.getBytes());
            ShareXMLParser xmlParser = new ShareXMLParser();
            mShares = xmlParser.parseXMLResponse(is);
            if (mShares != null) {
                Log_OC.d(TAG, "Got " + mShares.size() + " shares");
                result = new RemoteOperationResult(ResultCode.OK);
                ArrayList<Object> sharesObjects = new ArrayList<Object>();
                for (OCShare share : mShares) {
                    // Build the link 
                    if (share.getToken().length() > 0) {
                        share.setShareLink(
                                client.getBaseUri() + ShareUtils.SHARING_LINK_TOKEN + share.getToken());
                    }
                    sharesObjects.add(share);
                }
                result.setData(sharesObjects);
            }

        } else {
            result = new RemoteOperationResult(false, status, get.getResponseHeaders());
        }

    } catch (Exception e) {
        result = new RemoteOperationResult(e);
        Log_OC.e(TAG, "Exception while getting shares", e);

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

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);/* w  w  w. j  a v a  2 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.clarkparsia.sbol.editor.sparql.StardogEndpoint.java

@Override
public void export(RDFHandler handler, String namedGraph)
        throws HttpException, IOException, QueryEvaluationException {
    boolean complete = false;
    try {/*  w  ww .  j ava  2s  .  com*/
        GetMethod response = new GetMethod(url);
        response.setQueryString(new NameValuePair[] { graphParam(namedGraph) });
        execute(response);
        try {
            RDFParser parser = getParser(response);
            parser.setRDFHandler(handler);
            parser.parse(response.getResponseBodyAsStream(), url);
            complete = true;
        } catch (HttpException e) {
            throw new QueryEvaluationException(e);
        } catch (RDFParseException e) {
            throw new QueryEvaluationException(e);
        } catch (RDFHandlerException e) {
            throw new QueryEvaluationException(e);
        } finally {
            if (!complete)
                response.abort();
        }
    } catch (IOException e) {
        throw new QueryEvaluationException(e);
    }
}

From source file:com.cloudmade.api.CMClient.java

/**
 * Calls a cloudmade API service/* www .  ja v  a 2 s.  c om*/
 * 
 * @param uri
 * @param subdomain
 * @param params
 * @return the response byte array
 */
private byte[] callService(String uri, String subdomain, NameValuePair[] params) {
    String domain = subdomain == null ? host : subdomain + "." + host;
    String url = String.format("http://%s:%s/%s%s", domain, port, apiKey, uri);
    GetMethod method = new GetMethod(url);
    if (params != null) {
        method.setQueryString(params);
    }
    System.out.println(url + "?" + method.getQueryString());
    try {
        httpClient.executeMethod(method);
        if (method.getStatusCode() >= 400) {
            throw new HTTPError("Http error code: " + method.getStatusCode() + " for url " + url);
        }
        this.response = method.getResponseBody();

        return method.getResponseBody();
    } catch (Exception e) {
        throw new HTTPError(e);
    } finally {
        method.releaseConnection();
    }
}

From source file:com.github.maven.plugin.client.impl.GithubClientImpl.java

/**
 * Retrieves the download informations associated to the given repository
 * url.//from w w  w .j  a v  a 2  s  .  c om
 *
 * @param repositoryUrl The repository url.
 *
 * @return A map containing the downloads informations.
 */
private Map<String, Integer> retrieveDownloadsInfos(String repositoryUrl) {
    final Map<String, Integer> downloads = new HashMap<String, Integer>();

    GetMethod githubGet = new GetMethod(toRepositoryDownloadUrl(repositoryUrl));
    githubGet.setQueryString(
            new NameValuePair[] { new NameValuePair("login", login), new NameValuePair("token", token) });

    int response;

    try {

        response = httpClient.executeMethod(githubGet);
    } catch (IOException e) {
        throw new GithubRepositoryNotFoundException(
                "Cannot retrieve github repository " + repositoryUrl + " informations", e);
    }

    if (response == HttpStatus.SC_OK) {

        String githubResponse;

        try {

            githubResponse = githubGet.getResponseBodyAsString();
        } catch (IOException e) {
            throw new GithubRepositoryNotFoundException(
                    "Cannot retrieve github repository " + repositoryUrl + "  informations", e);
        }

        Pattern pattern = Pattern.compile(
                String.format("<a href=\"(/downloads)?%s/?([^\"]+)\"", removeGithubUrlPart(repositoryUrl)));

        Matcher matcher = pattern.matcher(githubResponse);
        while (matcher.find()) {
            String tmp = matcher.group(2);

            if (tmp.contains("downloads")) {
                String id = matcher.group(2).substring(tmp.lastIndexOf('/') + 1, tmp.length());
                Integer downloadId = Integer.parseInt(id);
                if (matcher.find()) {
                    downloads.put(matcher.group(2), downloadId);
                }
            }
        }

    } else if (response == HttpStatus.SC_NOT_FOUND) {
        throw new GithubRepositoryNotFoundException("Cannot found repository " + repositoryUrl);
    } else {
        throw new GithubRepositoryNotFoundException(
                "Cannot retrieve github repository " + repositoryUrl + " informations");
    }

    githubGet.releaseConnection();

    return downloads;
}

From source file:eu.learnpad.core.impl.mv.XwikiBridgeInterfaceRestResource.java

@Override
public VerificationStatus getVerificationStatus(String verificationProcessId) throws LpRestException {
    HttpClient httpClient = this.getHttpClient();
    String uri = String.format("%s/learnpad/mv/bridge/getverificationstatus", this.restPrefix);
    GetMethod getMethod = new GetMethod(uri);
    getMethod.addRequestHeader("Accept", "application/xml");

    NameValuePair[] queryString = new NameValuePair[1];
    queryString[0] = new NameValuePair("verificationprocessid", verificationProcessId);
    getMethod.setQueryString(queryString);

    try {/*from   ww  w .jav  a  2 s  . c  o m*/
        httpClient.executeMethod(getMethod);
    } catch (IOException e) {
        throw new LpRestExceptionXWikiImpl(e.getMessage(), e);
    }

    VerificationStatus verificationStatus = null;
    try {
        JAXBContext jaxbContext = JAXBContext.newInstance(VerificationStatus.class);
        InputStream retIs = getMethod.getResponseBodyAsStream();
        verificationStatus = (VerificationStatus) jaxbContext.createUnmarshaller().unmarshal(retIs);
    } catch (Exception e) {
        throw new LpRestExceptionXWikiImpl(e.getMessage(), e);
    }

    return verificationStatus;
}

From source file:eu.learnpad.core.impl.mv.XwikiBridgeInterfaceRestResource.java

@Override
public VerificationResults getVerificationResult(String verificationProcessId) throws LpRestException {

    HttpClient httpClient = this.getHttpClient();
    String uri = String.format("%s/learnpad/mv/bridge/getverificationresult", this.restPrefix);
    GetMethod getMethod = new GetMethod(uri);
    getMethod.addRequestHeader("Accept", "application/xml");

    NameValuePair[] queryString = new NameValuePair[1];
    queryString[0] = new NameValuePair("verificationprocessid", verificationProcessId);
    getMethod.setQueryString(queryString);

    try {/*from   www. j av a  2s .c  o m*/
        httpClient.executeMethod(getMethod);
    } catch (IOException e) {
        throw new LpRestExceptionXWikiImpl(e.getMessage(), e);
    }
    VerificationResults verificationResults = null;

    try {
        JAXBContext jaxbContext = JAXBContext.newInstance(VerificationResults.class);
        InputStream retIs = getMethod.getResponseBodyAsStream();
        verificationResults = (VerificationResults) jaxbContext.createUnmarshaller().unmarshal(retIs);
    } catch (Exception e) {
        throw new LpRestExceptionXWikiImpl(e.getMessage(), e);
    }

    return verificationResults;
}

From source file:com.owncloud.android.lib.resources.users.GetRemoteUserQuotaOperation.java

@Override
protected RemoteOperationResult run(OwnCloudClient client) {
    RemoteOperationResult result = null;
    int status;//from  ww w. j a  va2 s .c  o m
    GetMethod get = null;

    //Get the user
    try {
        OwnCloudCredentials credentials = client.getCredentials();
        String url = client.getBaseUri() + OCS_ROUTE + credentials.getUsername();

        get = new GetMethod(url);
        get.setQueryString(new NameValuePair[] { new NameValuePair("format", "json") });
        get.addRequestHeader(OCS_API_HEADER, OCS_API_HEADER_VALUE);
        status = client.executeMethod(get);

        if (isSuccess(status)) {
            String response = get.getResponseBodyAsString();

            // Parse the response
            JSONObject respJSON = new JSONObject(response);
            JSONObject respOCS = respJSON.getJSONObject(NODE_OCS);
            JSONObject respData = respOCS.getJSONObject(NODE_DATA);
            JSONObject quota = respData.getJSONObject(NODE_QUOTA);
            final Long quotaFree = quota.getLong(NODE_QUOTA_FREE);
            final Long quotaUsed = quota.getLong(NODE_QUOTA_USED);
            final Long quotaTotal = quota.getLong(NODE_QUOTA_TOTAL);
            final Double quotaRelative = quota.getDouble(NODE_QUOTA_RELATIVE);

            // Result
            result = new RemoteOperationResult(true, status, get.getResponseHeaders());
            //Quota data in data collection
            ArrayList<Object> data = new ArrayList<Object>();
            data.add(new Quota(quotaFree, quotaUsed, quotaTotal, quotaRelative));
            result.setData(data);

        } else {
            result = new RemoteOperationResult(false, status, get.getResponseHeaders());
            String response = get.getResponseBodyAsString();
            Log_OC.e(TAG, "Failed response while getting user quota information ");
            if (response != null) {
                Log_OC.e(TAG, "*** status code: " + status + " ; response message: " + response);
            } else {
                Log_OC.e(TAG, "*** status code: " + status);
            }
        }
    } catch (Exception e) {
        result = new RemoteOperationResult(e);
        Log_OC.e(TAG, "Exception while getting OC user information", e);

    } finally {
        if (get != null) {
            get.releaseConnection();
        }
    }

    return result;
}