List of usage examples for org.apache.http.client.utils URIBuilder build
public URI build() throws URISyntaxException
From source file:com.seleniumtests.connectors.selenium.SeleniumRobotGridConnector.java
/** * In case an app is required on the node running the test, upload it to the grid hub * This will then be made available through HTTP GET URL to the node (appium will receive an url instead of a file) * /*from w w w. j av a2s.co m*/ */ @Override public void uploadMobileApp(Capabilities caps) { String appPath = (String) caps.getCapability(MobileCapabilityType.APP); // check whether app is given and app path is a local file if (appPath != null && new File(appPath).isFile()) { try (CloseableHttpClient client = HttpClients.createDefault();) { // zip file List<File> appFiles = new ArrayList<>(); appFiles.add(new File(appPath)); File zipFile = FileUtility.createZipArchiveFromFiles(appFiles); HttpHost serverHost = new HttpHost(hubUrl.getHost(), hubUrl.getPort()); URIBuilder builder = new URIBuilder(); builder.setPath("/grid/admin/FileServlet/"); builder.addParameter("output", "app"); HttpPost httpPost = new HttpPost(builder.build()); httpPost.setHeader(HttpHeaders.CONTENT_TYPE, MediaType.OCTET_STREAM.toString()); FileInputStream fileInputStream = new FileInputStream(zipFile); InputStreamEntity entity = new InputStreamEntity(fileInputStream); httpPost.setEntity(entity); CloseableHttpResponse response = client.execute(serverHost, httpPost); if (response.getStatusLine().getStatusCode() != 200) { throw new SeleniumGridException( "could not upload application file: " + response.getStatusLine().getReasonPhrase()); } else { // set path to the mobile application as an URL on the grid hub ((DesiredCapabilities) caps).setCapability(MobileCapabilityType.APP, IOUtils.toString(response.getEntity().getContent()) + "/" + appFiles.get(0).getName()); } } catch (IOException | URISyntaxException e) { throw new SeleniumGridException("could not upload application file", e); } } }
From source file:ph.com.globe.connect.Payment.java
/** * Build request url./*from w ww .j a va2 s . c o m*/ * * @param url target url * @param appId app id * @param appSecret app secret * @return String * @throws ApiException api exception */ protected String buildUrl(String url, String appId, String appSecret) throws ApiException { // try parsing url try { // initialize url builder URIBuilder builder = new URIBuilder(url); // set app id parameter builder.setParameter("app_id", appId); // set app secret parameter builder.setParameter("app_secret", appSecret); // build the url url = builder.build().toString(); return url; } catch (URISyntaxException e) { // throw exception throw new ApiException(e.getMessage()); } }
From source file:io.fabric8.etcd.impl.dsl.GetDataImpl.java
@Override public HttpUriRequest createRequest(OperationContext context) { try {//from w ww. j a va 2s . co m URIBuilder builder = new URIBuilder(context.getBaseUri()).setPath(Keys.makeKey(key)) .addParameter("recursive", String.valueOf(recursive)) .addParameter("sorted", String.valueOf(sorted)); if (shouldWait) { builder = builder.addParameter("wait", "true"); if (waitIndex > 0) { builder = builder.addParameter("waitIndex", String.valueOf(waitIndex)); } } return new HttpGet(builder.build()); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:com.esri.geoportal.commons.agp.client.AgpClient.java
/** * Adds item.//from ww w. j a va 2s .c o m * @param owner owner * @param folderId folder id (optional) * @param title title * @param description description * @param text text * @param thumbnailUrl thumbnail url * @param itemType item type (must be a URL type) * @param extent extent * @param typeKeywords type keywords * @param tags tags tags * @param token token * @return add item response * @throws URISyntaxException if invalid URL * @throws IOException if operation fails */ public ItemResponse addItem(String owner, String folderId, String title, String description, String text, URL thumbnailUrl, ItemType itemType, Double[] extent, String[] typeKeywords, String[] tags, String token) throws IOException, URISyntaxException { URIBuilder builder = new URIBuilder(addItemUri(owner, StringUtils.trimToNull(folderId))); HttpPost req = new HttpPost(builder.build()); HashMap<String, String> params = new HashMap<>(); params.put("f", "json"); params.put("title", title); params.put("description", description); params.put("type", itemType.getTypeName()); params.put("text", text); if (thumbnailUrl != null) { params.put("thumbnailurl", thumbnailUrl.toExternalForm()); } if (extent != null && extent.length == 4) { params.put("extent", Arrays.asList(extent).stream().map(Object::toString).collect(Collectors.joining(","))); } if (typeKeywords != null) { params.put("typeKeywords", Arrays.asList(typeKeywords).stream().collect(Collectors.joining(","))); } if (tags != null) { params.put("tags", Arrays.asList(tags).stream().collect(Collectors.joining(","))); } params.put("token", token); req.setEntity(createEntity(params)); return execute(req, ItemResponse.class); }
From source file:com.esri.geoportal.commons.agp.client.AgpClient.java
/** * Adds item./* ww w. j a v a2 s. c om*/ * @param owner user name * @param folderId folder id (optional) * @param title title * @param description description * @param url URL * @param thumbnailUrl thumbnail url * @param itemType item type (must be a URL type) * @param extent extent * @param typeKeywords type keywords * @param tags tags tags * @param token token * @return add item response * @throws URISyntaxException if invalid URL * @throws IOException if operation fails */ public ItemResponse addItem(String owner, String folderId, String title, String description, URL url, URL thumbnailUrl, ItemType itemType, Double[] extent, String[] typeKeywords, String[] tags, String token) throws IOException, URISyntaxException { URIBuilder builder = new URIBuilder(addItemUri(owner, StringUtils.trimToNull(folderId))); HttpPost req = new HttpPost(builder.build()); HashMap<String, String> params = new HashMap<>(); params.put("f", "json"); params.put("title", title); params.put("description", description); params.put("type", itemType.getTypeName()); params.put("url", url.toExternalForm()); if (thumbnailUrl != null) { params.put("thumbnailurl", thumbnailUrl.toExternalForm()); } if (extent != null && extent.length == 4) { params.put("extent", Arrays.asList(extent).stream().map(Object::toString).collect(Collectors.joining(","))); } if (typeKeywords != null) { params.put("typeKeywords", Arrays.asList(typeKeywords).stream().collect(Collectors.joining(","))); } if (tags != null) { params.put("tags", Arrays.asList(tags).stream().collect(Collectors.joining(","))); } params.put("token", token); req.setEntity(createEntity(params)); return execute(req, ItemResponse.class); }
From source file:com.geoxp.oss.client.OSSClient.java
public static boolean init(String ossURL, byte[] secret, String sshKeyFingerprint) throws OSSException { SSHAgentClient agent = null;// www. jav a 2s. c om HttpClient httpclient = newHttpClient(); try { agent = new SSHAgentClient(); List<SSHKey> sshkeys = agent.requestIdentities(); // // If no SSH Key fingerprint was provided, try all SSH keys available in the agent // List<String> fingerprints = new ArrayList<String>(); if (null == sshKeyFingerprint) { for (SSHKey key : sshkeys) { fingerprints.add(key.fingerprint); } } else { fingerprints.add(sshKeyFingerprint.toLowerCase().replaceAll("[^0-9a-f]", "")); } int idx = 0; for (String fingerprint : fingerprints) { idx++; // // Ask the SSH agent for the SSH key blob // byte[] keyblob = null; for (SSHKey key : sshkeys) { if (key.fingerprint.equals(fingerprint)) { keyblob = key.blob; break; } } // // Throw an exception if this condition is encountered as it can only happen if // there was a provided fingerprint which is not in the agent. // if (null == keyblob) { throw new OSSException("SSH Key " + sshKeyFingerprint + " was not found by your SSH agent."); } // // Retrieve OSS RSA key // RSAPublicKey pubkey = getOSSRSA(ossURL); // // Build the initialization token // // <TS> <SECRET> <SSH Signing Key Blob> <SSH Signature Blob> // ByteArrayOutputStream token = new ByteArrayOutputStream(); byte[] tsdata = nowBytes(); token.write(CryptoHelper.encodeNetworkString(tsdata)); token.write(CryptoHelper.encodeNetworkString(secret)); token.write(CryptoHelper.encodeNetworkString(keyblob)); byte[] sigblob = agent.sign(keyblob, token.toByteArray()); token.write(CryptoHelper.encodeNetworkString(sigblob)); // // Encrypt the token with a random AES256 key // byte[] aeskey = new byte[32]; CryptoHelper.getSecureRandom().nextBytes(aeskey); byte[] wrappedtoken = CryptoHelper.wrapAES(aeskey, token.toByteArray()); // // Encrypt the random key with OSS' RSA key // byte[] sealedaeskey = CryptoHelper.encryptRSA(pubkey, aeskey); // // Create the token // token.reset(); token.write(CryptoHelper.encodeNetworkString(wrappedtoken)); token.write(CryptoHelper.encodeNetworkString(sealedaeskey)); // // Base64 encode the encryptedtoken // String b64token = new String(Base64.encode(token.toByteArray()), "UTF-8"); // // Send request to OSS // URIBuilder builder = new URIBuilder(ossURL + GuiceServletModule.SERVLET_PATH_INIT); builder.addParameter("token", b64token); URI uri = builder.build(); String qs = uri.getRawQuery(); HttpPost post = new HttpPost( uri.getScheme() + "://" + uri.getHost() + ":" + uri.getPort() + uri.getPath()); post.setHeader("Content-Type", "application/x-www-form-urlencoded"); post.setEntity(new StringEntity(qs)); httpclient = newHttpClient(); HttpResponse response = httpclient.execute(post); HttpEntity resEntity = response.getEntity(); String content = EntityUtils.toString(resEntity, "UTF-8"); post.reset(); if (HttpServletResponse.SC_ACCEPTED == response.getStatusLine().getStatusCode()) { return false; } else if (HttpServletResponse.SC_OK != response.getStatusLine().getStatusCode()) { // Only throw an exception if this is the last SSH key we could try if (idx == fingerprints.size()) { throw new OSSException("None of the provided keys (" + idx + ") could be used to initialize this Open Secret Server. Latest error message was: " + response.getStatusLine().getReasonPhrase()); } else { continue; } } return true; } } catch (OSSException osse) { throw osse; } catch (Exception e) { throw new OSSException(e); } finally { if (null != httpclient) { httpclient.getConnectionManager().shutdown(); } if (null != agent) { agent.close(); } } return false; }
From source file:com.msopentech.odatajclient.engine.uri.ODataURIBuilder.java
/** * Build OData URI.//from w ww.j a va2 s .co m * * @return OData URI. */ public URI build() { final StringBuilder segmentsBuilder = new StringBuilder(); for (Segment seg : segments) { if (segmentsBuilder.length() > 0 && seg.type != SegmentType.KEY) { segmentsBuilder.append('/'); } segmentsBuilder.append(seg.value); } try { final URIBuilder builder = new URIBuilder(segmentsBuilder.toString()); for (Map.Entry<String, String> option : queryOptions.entrySet()) { builder.addParameter("$" + option.getKey(), option.getValue()); } return builder.build().normalize(); } catch (URISyntaxException e) { throw new IllegalArgumentException("Could not build valid URI", e); } }
From source file:org.wso2.carbon.apimgt.hostobjects.oidc.OIDCRelyingPartyObject.java
/** * Building authentication request URL. This URL allows to redirect in to OIDC server and authenticate. * @param cx - Context/*from w ww. j av a 2s .c om*/ * @param thisObj - This Object * @param args - takes nonce and state parameters * @param funObj - Function * @return URL which redirects to OIDC server and allow to authenticate * @throws Exception */ public static String jsFunction_buildAuthRequestUrl(Context cx, Scriptable thisObj, Object[] args, Function funObj) throws Exception { int argLength = args.length; if (argLength != 2 || !(args[0] instanceof String) || !(args[1] instanceof String)) { throw new ScriptException("Invalid argument. Nonce or State not set properly"); } String nonce = (String) args[0]; String state = (String) args[1]; OIDCRelyingPartyObject relyingPartyObject = (OIDCRelyingPartyObject) thisObj; try { log.debug(" Building auth request Url"); URIBuilder uriBuilder = new URIBuilder( relyingPartyObject.getOIDCProperty(OIDCConstants.AUTHORIZATION_ENDPOINT_URI)); uriBuilder.addParameter(OIDCConstants.RESPONSE_TYPE, relyingPartyObject.getOIDCProperty(OIDCConstants.RESPONSE_TYPE)); uriBuilder.addParameter(OIDCConstants.CLIENT_ID, relyingPartyObject.getOIDCProperty(OIDCConstants.CLIENT_ID)); uriBuilder.addParameter(OIDCConstants.SCOPE, relyingPartyObject.getOIDCProperty(OIDCConstants.SCOPE)); uriBuilder.addParameter(OIDCConstants.REDIRECT_URI, relyingPartyObject.getOIDCProperty(OIDCConstants.REDIRECT_URI)); uriBuilder.addParameter(OIDCConstants.NONCE, nonce); uriBuilder.addParameter(OIDCConstants.STATE, state); // Optional parameters: //for (Map.Entry<String, String> option : options.entrySet()) { // uriBuilder.addParameter(option.getKey(), option.getValue()); //} //uriBuilder.addParameter("requestURI", requestURI); return uriBuilder.build().toString(); } catch (URISyntaxException e) { log.error("Build Auth Request Failed", e); throw new Exception("Build Auth Request Failed", e); } }
From source file:com.michaeljones.httpclient.apache.ApacheMethodClient.java
@Override public int DeleteFile(String url, List<Pair<String, String>> queryParams) { try {//from www.j av a 2s . c o m URIBuilder fileUri = new URIBuilder(url); if (queryParams != null) { // Query params are optional. In the case of a redirect the url will contain // all the params. for (Pair<String, String> queryParam : queryParams) { fileUri.addParameter(queryParam.getFirst(), queryParam.getSecond()); } } HttpDelete httpDel = new HttpDelete(fileUri.build()); CloseableHttpResponse response = clientImpl.execute(httpDel); return response.getStatusLine().getStatusCode(); } catch (URISyntaxException | IOException ex) { throw new RuntimeException("Apache method deleteQuery: " + ex.getMessage()); } }