Example usage for org.apache.http.client.utils URIBuilder setScheme

List of usage examples for org.apache.http.client.utils URIBuilder setScheme

Introduction

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

Prototype

public URIBuilder setScheme(final String scheme) 

Source Link

Document

Sets URI scheme.

Usage

From source file:azkaban.utils.RestfulApiClient.java

/** helper function to build a valid URI.
 *  @param host   host name./*  www . j a v a  2 s.c o m*/
 *  @param port   host port.
 *  @param path   extra path after host.
 *  @param isHttp indicates if whether Http or HTTPS should be used.
 *  @param params extra query parameters.
 *  @return the URI built from the inputs.
 *  @throws IOException
 * */
public static URI buildUri(String host, int port, String path, boolean isHttp, Pair<String, String>... params)
        throws IOException {
    URIBuilder builder = new URIBuilder();
    builder.setScheme(isHttp ? "http" : "https").setHost(host).setPort(port);

    if (null != path && path.length() > 0) {
        builder.setPath(path);
    }

    if (params != null) {
        for (Pair<String, String> pair : params) {
            builder.setParameter(pair.getFirst(), pair.getSecond());
        }
    }

    URI uri = null;
    try {
        uri = builder.build();
    } catch (URISyntaxException e) {
        throw new IOException(e);
    }

    return uri;
}

From source file:com.github.autermann.wps.streaming.ProcessConfiguration.java

private static URI createSocketURI() {
    try {//from w  ww  . ja  va2  s  . co  m
        String wpsEndpoint = CapabilitiesConfiguration.ENDPOINT_URL;
        URIBuilder builder = new URIBuilder(wpsEndpoint);
        switch (builder.getScheme()) {
        case HTTP_SCHEME:
            builder.setScheme(WS_SCHEME);
            if (builder.getPort() == DEFAULT_HTTP_PORT) {
                builder.setPort(INVALID_PORT);
            }
            break;
        case HTTPS_SCHEME:
            builder.setScheme(WSS_SCHEME);
            if (builder.getPort() == DEFAULT_HTTPS_PORT) {
                builder.setPort(INVALID_PORT);
            }
            break;
        }
        String webappPath = normalizePath(WebProcessingService.WEBAPP_PATH);
        String servletPath = normalizePath(StreamingSocketEndpoint.PATH);
        if (webappPath != null) {
            builder.setPath(webappPath + servletPath);
        } else {
            builder.setPath(servletPath);
        }
        return builder.build();
    } catch (URISyntaxException ex) {
        return URI.create("ws://localhost:8080/streaming");
    }
}

From source file:org.ireas.mediawiki.MediaWikiUtils.java

/**
 * Constructs an {@code URI} object from {@code scheme}, {@code host},
 * {@code port} and {@code apiPath}. The constructed URI will have the
 * structure {@code "<scheme>://<host>:<port>/<apiPath>"}.
 *
 * @param scheme/*from  ww w . j av a 2 s.co m*/
 *            the scheme of the URI, e. g. {@code "https"}
 * @param host
 *            the host of the URI, e. g. {@code "example.org"}
 * @param port
 *            the port of the URI, e. g. {@code 443}. The port must be
 *            non-negative.
 * @param path
 *            the path of the URI, e. g. {@code "/index.html"}
 * @return an {@code URI} constructed from the given parts
 * @throws MediaWikiException
 *             if the given parts do not form a valid URI
 * @throws NullPointerException
 *             if {@code scheme}, {@code host} or {@code path} is
 *             {@code null}
 * @throws IllegalArgumentException
 *             if {@code port} is negative
 */
public static URI buildUri(final String scheme, final String host, final int port, final String path)
        throws MediaWikiException {
    Preconditions.checkNotNull(scheme);
    Preconditions.checkNotNull(host);
    Preconditions.checkNotNull(path);
    Preconditions.checkArgument(port >= 0);

    URIBuilder uriBuilder = new URIBuilder();
    uriBuilder.setScheme(scheme);
    uriBuilder.setHost(host);
    uriBuilder.setPort(port);
    uriBuilder.setPath(path);
    try {
        return uriBuilder.build();
    } catch (URISyntaxException exception) {
        throw new MediaWikiException(exception);
    }
}

From source file:org.finra.msl.client.MockAPI.java

/**
 * Method to register intercept XHR. Once you register, whenever
 * server receives a request matching the registered requestPath, it will
 * intercept and store for later retrieval
 * /* w w w . j  a  v  a  2  s.  c  o  m*/
 * @param server
 *            => url of web-server.js running on node
 * @param port
 *            => port number of web-server.js running on node
 * @param requestPath
 *            => path which you want to mock a fake response with
 * 
 * @return String response
 **/
public static String setInterceptXHR(String server, int port, String requestPath) throws Exception {
    URIBuilder builder = new URIBuilder();
    builder.setScheme("http").setHost(server).setPort(port).setPath("/mock/interceptxhr");

    JSONObject object = new JSONObject();
    object.put("requestPath", requestPath);

    HttpPost post = new HttpPost(builder.toString());
    post.setHeader("Content-Type", "application/json");
    post.setEntity(new StringEntity(object.toString()));

    HttpClient client = new DefaultHttpClient();
    HttpResponse resp = client.execute(post);

    if (resp.getStatusLine().getStatusCode() != 200) {
        throw new Exception("POST failed. Error code: " + resp.getStatusLine().getStatusCode());
    }
    return EntityUtils.toString(resp.getEntity());
}

From source file:org.finra.msl.client.MockAPI.java

/**
 * Method to set up parameters that will be ignored in the URL.
 * //from   w  ww  . ja va2  s.  c  o  m
 * @param server
 *            => url of web-server.js running on node
 * @param port
 *            => port number of web-server.js running on node
 * @param params => parameters that will be ignored in the app URL, type is string. 
 *                  For example,if we set ignore paramB, URL http://aa.bb.com/result?paramA=123&paramB=456 will be treated as http://aa.bb.com/result?paramA=123
 * 
 * @return String response
 **/
public static String setParamIgnored(String server, int port, String params) throws Exception {

    URIBuilder builder = new URIBuilder();
    builder.setScheme("http").setHost(server).setPort(port).setPath("/setIgnoreFlag");

    JSONObject object = new JSONObject();
    object.put("requestPath", params);

    HttpPost get = new HttpPost(builder.toString());
    get.setHeader("Content-Type", "application/json");
    get.setEntity(new StringEntity(object.toString()));

    HttpClient client = new DefaultHttpClient();
    HttpResponse resp = client.execute(get);

    if (resp.getStatusLine().getStatusCode() != 200) {
        throw new Exception("GET failed. Error code: " + resp.getStatusLine().getStatusCode());
    }

    return EntityUtils.toString(resp.getEntity());
}

From source file:org.finra.msl.client.MockAPI.java

/**
 * This allows for the removal of all registered mocks once they are no longer in
 * use.//from w  ww  .  j ava2 s  .co m
 * 
 * 
 * @param server
 *            => url of web-server.js running on node
 * @param port
 *            => port number of web-server.js running on node
 * @return
 * @throws Exception
 */
public static String unRegisterMock(String server, int port) throws Exception {

    URIBuilder builder = new URIBuilder();

    builder.setScheme("http").setHost(server).setPort(port).setPath("/unregisterMock");

    JSONObject object = new JSONObject();
    object.put("requestPath", "");

    HttpPost get = new HttpPost(builder.toString());
    get.setHeader("Content-Type", "application/json");
    get.setEntity(new StringEntity(object.toString()));

    HttpClient client = new DefaultHttpClient();
    HttpResponse resp = client.execute(get);

    if (resp.getStatusLine().getStatusCode() != 200) {
        throw new Exception("GET failed. Error code: " + resp.getStatusLine().getStatusCode());
    }

    return EntityUtils.toString(resp.getEntity());

}

From source file:org.finra.msl.client.MockAPI.java

/**
 * This allows for the removal of a registered mock once it is no longer in
 * use./*  w  w  w.  j  a v  a 2s.  c om*/
 * 
 * 
 * @param server
 *            => url of web-server.js running on node
 * @param port
 *            => port number of web-server.js running on node
 * @param requestPath
 *            => path which you want to mock a fake response with * @return
 * @throws Exception
 */
public static String unRegisterMock(String server, int port, String requestPath) throws Exception {

    URIBuilder builder = new URIBuilder();

    builder.setScheme("http").setHost(server).setPort(port).setPath("/unregisterMock");

    JSONObject object = new JSONObject();
    object.put("requestPath", requestPath);

    HttpPost get = new HttpPost(builder.toString());
    get.setHeader("Content-Type", "application/json");
    get.setEntity(new StringEntity(object.toString()));

    HttpClient client = new DefaultHttpClient();
    HttpResponse resp = client.execute(get);

    if (resp.getStatusLine().getStatusCode() != 200) {
        throw new Exception("GET failed. Error code: " + resp.getStatusLine().getStatusCode());
    }

    return EntityUtils.toString(resp.getEntity());

}

From source file:org.finra.msl.client.MockAPI.java

/**
 * Register a template that is passed to the web-server where the it is
 * stored using the ID as the key. This is used later when setting up a mock
 * response./*from  w w  w  .  j  a v  a  2 s.c om*/
 * 
 * @param server
 *            => url of web-server.js running on node
 * @param port
 *            => port number of web-server.js running on node
 * @param template
 *            => the template that is to be registered for later use
 * @param id
 *            => key used to indicate which template is to be used when
 *            mocking a response
 * @return => returns the response
 * @throws Exception
 */
public static String registerTemplate(String server, int port, String template, String id) throws Exception {
    HttpPost post = null;
    JSONObject object = new JSONObject();

    URIBuilder builder = new URIBuilder();

    builder.setScheme("http").setHost(server).setPort(port).setPath("/mock/template");

    post = new HttpPost(builder.toString());
    object.put("template", template);
    object.put("id", id);

    post.setHeader("Content-Type", "application/json");
    post.setEntity(new StringEntity(object.toString()));

    HttpClient client = new DefaultHttpClient();
    HttpResponse resp = client.execute(post);

    if (resp.getStatusLine().getStatusCode() != 200) {
        throw new Exception("POST failed. Error code: " + resp.getStatusLine().getStatusCode());
    }
    return EntityUtils.toString(resp.getEntity());

}

From source file:org.finra.msl.client.MockAPI.java

/**
 * Method to register mock response. Once you register, whenever
 * server receives a request matching the registered requestPath, it will
 * respond with a fake response using the provided JSONObject's information.
 * //from w  ww . jav a 2  s.c o  m
 * @param server
 *            => url of web-server.js running on node
 * @param port
 *            => port number of web-server.js running on node
 * @param configurations
 *            => the JSONObject that contains all of the options (content
 *            type, requestPath, etc)
 * @return
 * @throws Exception
 */
@SuppressWarnings("unchecked")
public static String setMockRespond(String server, int port, Map<String, Object> configurations)
        throws Exception {

    URIBuilder builder = new URIBuilder();
    builder.setScheme("http").setHost(server).setPort(port).setPath("/mock/fakerespond");

    if (configurations.keySet().contains("keyValues")) {
        if (configurations.get("keyValues") != null && configurations.get("keyValues") instanceof Map) {
            configurations.put("keyValues",
                    new JSONObject((Map<String, Object>) configurations.get("keyValues")));
        }
    }

    if (configurations.keySet().contains("header")) {
        if (configurations.get("header") != null && configurations.get("header") instanceof Map) {
            configurations.put("header", new JSONObject((Map<String, Object>) configurations.get("header")));
        }
    }

    JSONObject object = new JSONObject(configurations);
    HttpPost post = new HttpPost(builder.toString());

    post.setHeader("Content-Type", "application/json");
    post.setEntity(new StringEntity(object.toString()));

    HttpClient client = new DefaultHttpClient();
    HttpResponse resp = client.execute(post);

    if (resp.getStatusLine().getStatusCode() != 200) {
        throw new Exception("POST failed. Error code: " + resp.getStatusLine().getStatusCode());
    }
    return EntityUtils.toString(resp.getEntity());
}

From source file:org.finra.msl.client.MockAPI.java

/**
 * Method to retrieve intercepted XHRs. Use in conjunction with
 * setInterceptXHR()//from  ww w  .  ja va  2s .  com
 * 
 * @param server
 *            => url of web-server.js running on node
 * @param port
 *            => port number of web-server.js running on node
 * @param requestPath
 *            => path which you want to mock a fake response with
 * 
 * @return list of intercepted XHR objects
 */
public static XHR[] getInterceptedXHR(String server, int port, String requestPath) throws Exception {
    URIBuilder builder = new URIBuilder();

    builder.setScheme("http").setHost(server).setPort(port).setPath("/mock/getinterceptedxhr");

    JSONObject object = new JSONObject();
    object.put("requestPath", requestPath);

    HttpPost post = new HttpPost(builder.toString());
    post.setHeader("Content-Type", "application/json");
    post.setEntity(new StringEntity(object.toString()));

    HttpClient client = new DefaultHttpClient();
    HttpResponse resp = client.execute(post);

    if (resp.getStatusLine().getStatusCode() != 200) {
        throw new Exception("GET failed. Error code: " + resp.getStatusLine().getStatusCode());
    }

    // Parse JSON
    JSONObject jsonObj = (JSONObject) JSONValue.parse(EntityUtils.toString(resp.getEntity()));
    XHR[] interceptedXHRs = new XHR[jsonObj.keySet().size()];
    for (String key : jsonObj.keySet()) {
        int index = Integer.parseInt(key.split("_")[1]);
        JSONObject xhrDataObj = (JSONObject) jsonObj.get(key);
        JSONObject xhrObj = (JSONObject) xhrDataObj.get("xhr");
        Object postObj = xhrDataObj.get("post");

        // Get URL and method
        String urlStr = xhrObj.get("url").toString();
        String method = xhrObj.get("method").toString();

        // Post body
        String body = "";
        if (postObj != null) {
            body = postObj.toString();
        }

        XHR xhr = new XHR(urlStr, method, body);
        interceptedXHRs[index - 1] = xhr;
    }

    return interceptedXHRs;
}