Example usage for org.apache.http.client.utils URIUtils createURI

List of usage examples for org.apache.http.client.utils URIUtils createURI

Introduction

In this page you can find the example usage for org.apache.http.client.utils URIUtils createURI.

Prototype

@Deprecated
public static URI createURI(final String scheme, final String host, final int port, final String path,
        final String query, final String fragment) throws URISyntaxException 

Source Link

Document

Constructs a URI using all the parameters.

Usage

From source file:com.healthcit.cacure.dao.CouchDBDao.java

/**
 * This version of the "getFormsByModuleId" method is necessary
 * to prevent Java heap size errors for large databases. 
 * Rather than returning all the documents in the database,
 * it allows documents to be returned in batches of size "batchSize", with "startingDocumentId" as the
 * DocId of the first document in the list.
 * @param moduleId//  w ww.  j  av a2s.  c o m
 * @param batchSize
 * @param startingDocId
 * @return
 * @throws IOException
 * @throws URISyntaxException
 */
public JSONArray getDocsByModule(String moduleId, Integer batchSize, boolean includeDocs, String startingDocId)
        throws IOException, URISyntaxException {
    String viewURL = includeDocs ? constructViewURL("GetDocsByModule") : constructViewURL("GetDocIdsByModule");

    String key = URLEncoder.encode("\"" + moduleId + "\"", "UTF-8");

    String parameters = "key=" + key + (batchSize != null ? "&limit=" + batchSize : "")
            + (startingDocId != null ? ("&startkey_docid=" + startingDocId) : "");

    URI uri = URIUtils.createURI("http", host, port, viewURL, parameters, null);

    HttpGet httpGet = new HttpGet(uri);

    String response = doHttp(httpGet);

    JSONObject json = JSONObject.fromObject(response);

    JSONArray objects = json.getJSONArray("rows");

    return objects;
}

From source file:com.healthcit.cacure.dao.CouchDBDao.java

public String getFormXMLByOwnerIdAndFormId(JSONArray key) throws IOException, URISyntaxException {
    String keyString = key.toString();
    String encodedURL = URLEncoder.encode(keyString, "UTF-8");
    String viewURL = constractListURL("formToXml", "GetDocByOwnerAndForm");
    URI uri = URIUtils.createURI("http", host, port, viewURL, "key=" + encodedURL, null);
    HttpGet httpGet = new HttpGet(uri);
    String response = doHttp(httpGet);
    return response;
}

From source file:gtu.youtube.JavaYoutubeVideoUrlHandler.java

private URI getUri(String path, List<NameValuePair> qparams) throws URISyntaxException {
    URI uri = URIUtils.createURI(scheme, host, -1, "/" + path,
            URLEncodedUtils.format(qparams, DEFAULT_ENCODING), null);
    return uri;/*from ww w  .java 2s . com*/
}

From source file:com.archivas.clienttools.arcutils.impl.adapter.Hcp3AuthNamespaceAdapter.java

@Override
public void setMetadata(final String targetNode, final String path, final FileMetadata metadata)
        throws StorageAdapterException {

    String queryString = generateQueryParameters(metadata, false);

    HttpHost httpHost = new HttpHost(targetNode, profile.getPort(), profile.getProtocol());
    URI uri;//from ww w  . j  ava  2s  .  c o  m
    try {
        String resolvedPath = getProfile().resolvePath(path);
        uri = URIUtils.createURI(profile.getProtocol(), targetNode, profile.getPort(), resolvedPath,
                queryString, null);
    } catch (URISyntaxException e) {
        LOG.log(Level.WARNING, "Unexpected error generating post URI for : " + path);
        throw new StorageAdapterLiteralException("Error writing metadata to the server", e);
    }

    String activity = "setting metadata";
    HttpPost request = new HttpPost(uri);

    try {
        HcapAdapterCookie cookie = new HcapAdapterCookie(request, httpHost);
        synchronized (savingCookieLock) {
            if (savedCookie != null) {
                throw new RuntimeException(
                        "This adapter already has a current connection to host -- cannot create two at once.");
            }
            savedCookie = cookie;
        }
        executeMethod(cookie);

        this.handleHttpResponse(cookie.getResponse(), activity, path);
    } catch (IOException e) {
        this.handleIOExceptionFromRequest(e, activity, path);
    } finally {
        close();
    }
}

From source file:edu.scripps.fl.pubchem.web.session.PCWebSession.java

protected InputStream getBioActivityAssaySummaryAsStream(List<Long> cids) throws Exception {
    List<NameValuePair> params = addParameters(new ArrayList<NameValuePair>(), "cid",
            StringUtils.join(cids, ","), "q", "cids", "exptype", "bioactivitycsv");
    URI uri = URIUtils.createURI("http", SITE, 80, "/assay/assay.cgi", URLEncodedUtils.format(params, "UTF-8"),
            null);/*from  ww  w .  ja  v  a  2 s  .  com*/
    Document doc = new WaitOnRequestId(uri).asDocument();
    return getFtpLinkAsStream(doc);
}

From source file:com.basho.riak.client.util.ClientHelper.java

/**
 * Perform and HTTP request and return the resulting response using the
 * internal HttpClient./*  w  w w .j  av  a 2s  .  c  om*/
 * 
 * @param bucket
 *            Bucket of the object receiving the request.
 * @param key
 *            Key of the object receiving the request or null if the request
 *            is for a bucket.
 * @param httpMethod
 *            The HTTP request to perform; must not be null.
 * @param meta
 *            Extra HTTP headers to attach to the request. Query parameters
 *            are ignored; they should have already been used to construct
 *            <code>httpMethod</code> and query parameters.
 * @param streamResponse
 *            If true, the connection will NOT be released. Use
 *            HttpResponse.getHttpMethod().getResponseBodyAsStream() to get
 *            the response stream; HttpResponse.getBody() will return null.
 * 
 * @return The HTTP response returned by Riak from executing
 *         <code>httpMethod</code>.
 * 
 * @throws RiakIORuntimeException
 *             If an error occurs during communication with the Riak server
 *             (i.e. HttpClient threw an IOException)
 */
HttpResponse executeMethod(String bucket, String key, HttpRequestBase httpMethod, RequestMeta meta,
        boolean streamResponse) {

    if (meta != null) {
        Map<String, String> headers = meta.getHeaders();
        for (String header : headers.keySet()) {
            httpMethod.addHeader(header, headers.get(header));
        }

        Map<String, String> queryParams = meta.getQueryParamMap();
        if (!queryParams.isEmpty()) {
            URI originalURI = httpMethod.getURI();
            List<NameValuePair> currentQuery = URLEncodedUtils.parse(originalURI, CharsetUtils.UTF_8.name());
            List<NameValuePair> newQuery = new LinkedList<NameValuePair>(currentQuery);

            for (Map.Entry<String, String> qp : queryParams.entrySet()) {
                newQuery.add(new BasicNameValuePair(qp.getKey(), qp.getValue()));
            }

            // For this, HC4.1 authors, I hate you
            URI newURI;
            try {
                newURI = URIUtils.createURI(originalURI.getScheme(), originalURI.getHost(),
                        originalURI.getPort(), originalURI.getPath(), URLEncodedUtils.format(newQuery, "UTF-8"),
                        null);
            } catch (URISyntaxException e) {
                throw new RiakIORuntimeException(e);
            }
            httpMethod.setURI(newURI);
        }
    }
    HttpEntity entity;
    try {
        org.apache.http.HttpResponse response = httpClient.execute(httpMethod);

        int status = 0;
        if (response.getStatusLine() != null) {
            status = response.getStatusLine().getStatusCode();
        }

        Map<String, String> headers = ClientUtils.asHeaderMap(response.getAllHeaders());
        byte[] body = null;
        InputStream stream = null;
        entity = response.getEntity();

        if (streamResponse) {
            stream = entity.getContent();
        } else {
            if (null != entity) {
                body = EntityUtils.toByteArray(entity);
            }
        }

        if (!streamResponse) {
            EntityUtils.consume(entity);
        }

        return new DefaultHttpResponse(bucket, key, status, headers, body, stream, response, httpMethod);
    } catch (IOException e) {
        httpMethod.abort();
        return toss(new RiakIORuntimeException(e));
    }
}

From source file:com.healthcit.cacure.dao.CouchDBDao.java

public String getFormXMLByFormId(String keyString, List<String> owners, String context)
        throws IOException, URISyntaxException {
    JSONObject jsonBody = new JSONObject();
    JSONArray keys = new JSONArray();

    for (String ownerId : owners) {
        JSONArray aKey = new JSONArray();
        aKey.add(keyString);//from   ww w  . ja  va2  s .  c  o m
        aKey.add(ownerId);
        keys.add(aKey);
    }
    jsonBody.put("keys", keys);
    String viewURL = constractListURL("formToXmlAllOwners", "GetDocByFormAndOwner", context);
    URI uri = URIUtils.createURI("http", host, port, viewURL, null, null);
    String response = runPostQuery(uri, jsonBody.toString());
    return response;
}

From source file:com.healthcit.cacure.dao.CouchDBDao.java

public JSONObject getFormJSONByFormId(String keyString, List<String> owners, String context)
        throws IOException, URISyntaxException {
    JSONObject jsonBody = new JSONObject();
    JSONArray keys = new JSONArray();

    for (String ownerId : owners) {
        JSONArray aKey = new JSONArray();
        aKey.add(keyString);//  w  ww .  j  a v  a2s  .c om
        aKey.add(ownerId);
        keys.add(aKey);
    }
    jsonBody.put("keys", keys);
    String viewURL = constructViewURL("GetDocByFormAndOwner", context);
    URI uri = URIUtils.createURI("http", host, port, viewURL, null, null);
    String response = runPostQuery(uri, jsonBody.toString());
    JSONObject json = JSONObject.fromObject(response);
    JSONArray objects = json.getJSONArray("rows");
    JSONObject form = null;
    if (objects != null && objects.size() > 0) {
        JSONObject row = objects.getJSONObject(0);
        form = row.getJSONObject("value");
    }
    return form;

}

From source file:org.mahasen.util.PutUtil.java

/**
 * @param nodeIp//from  w  w  w  .  j  a v a2 s .com
 * @param part
 * @param partName
 * @param mahasenResourceToUpdate
 * @param parentFileId
 * @throws MahasenConfigurationException
 * @throws URISyntaxException
 */
private void sendReplicateRequest(String nodeIp, File part, String partName,
        MahasenResource mahasenResourceToUpdate, Id parentFileId)
        throws MahasenConfigurationException, URISyntaxException {

    setTrustStore();

    URI uri = null;

    ArrayList<NameValuePair> qparams = new ArrayList<NameValuePair>();
    qparams.add(new BasicNameValuePair("splittedfilename", part.getName()));
    uri = URIUtils.createURI("https", nodeIp + ":" + MahasenConstants.SERVER_PORT, -1,
            "/mahasen/upload_request_ajaxprocessor.jsp", URLEncodedUtils.format(qparams, "UTF-8"), null);

    System.out.println("Target Address for replicating : " + uri);
    PutUtil.incrementNoOfReplicas(partName);
    MahasenReplicateWorker replicateWorker = new MahasenReplicateWorker(uri, partName, part,
            mahasenResourceToUpdate, nodeIp, MahasenNodeManager.getInstance(), parentFileId);
    Thread replicateThread = new Thread(replicateWorker);

    replicateThread.start();

}