List of usage examples for org.apache.http.client.utils URIBuilder setHost
public URIBuilder setHost(final String host)
From source file:com.github.woz_dialog.ros_woz_dialog_project.TTSHTTPClient.java
/** * Builds the REST clients for speech recognition and synthesis. * * @//from ww w. j a v a 2 s . c o m */ private void buildClients() { // Initialize the HTTP clients asrClient = HttpClientBuilder.create().build(); ttsClient = HttpClientBuilder.create().build(); try { URIBuilder builder = new URIBuilder(); builder.setScheme("https"); builder.setHost("dictation.nuancemobility.net"); builder.setPort(443); builder.setPath("/NMDPAsrCmdServlet/dictation"); builder.setParameter("appId", APP_ID); builder.setParameter("appKey", APP_KEY); builder.setParameter("id", "0000"); asrURI = builder.build(); builder.setHost("tts.nuancemobility.net"); builder.setPath("/NMDPTTSCmdServlet/tts"); builder.setParameter("ttsLang", LANGUAGE); builder.setParameter("voice", VOICE); ttsURI = builder.build(); } catch (Exception e) { throw new RuntimeException("cannot build client: " + e); } }
From source file:net.datacrow.onlinesearch.discogs.task.DiscogsMusicSearch.java
@Override protected DcObject getItem(Object key, boolean full) throws Exception { HttpClient httpClient = getHttpClient(); URIBuilder builder = new URIBuilder(); builder.setScheme("http"); builder.setHost("api.discogs.com"); builder.setPath("/release/" + key); URI uri = builder.build();/*from ww w . j a v a2 s.c o m*/ HttpGet httpGet = new HttpGet(uri); HttpOAuthHelper au = new HttpOAuthHelper("application/json"); au.handleRequest(httpGet, _CONSUMER_KEY, _CONSUMER_SECRET); HttpResponse httpResponse = httpClient.execute(httpGet); String response = getReponseText(httpResponse); httpClient.getConnectionManager().shutdown(); MusicAlbum ma = new MusicAlbum(); ma.addExternalReference(DcRepository.ExternalReferences._DISCOGS, (String) key); ma.setValue(DcObject._SYS_SERVICEURL, uri.toString()); JsonParser jsonParser = new JsonParser(); JsonObject jsonObject = (JsonObject) jsonParser.parse(response); JsonObject eRespone = jsonObject.getAsJsonObject("resp"); JsonObject eRelease = eRespone.getAsJsonObject("release"); if (eRelease != null) { ma.setValue(MusicAlbum._A_TITLE, eRelease.get("title").getAsString()); ma.setValue(MusicAlbum._C_YEAR, eRelease.get("year").getAsString()); ma.setValue(MusicAlbum._N_WEBPAGE, eRelease.get("uri").getAsString()); ma.createReference(MusicAlbum._F_COUNTRY, eRelease.get("country").getAsString()); setStorageMedium(ma, eRelease); setRating(ma, eRelease); setGenres(ma, eRelease); setArtists(ma, eRelease); addTracks(ma, eRelease); setImages(ma, eRelease); } Thread.sleep(1000); return ma; }
From source file:opendial.plugins.NuanceSpeech.java
/** * Builds the REST clients for speech recognition and synthesis. * /*from w w w . j a v a 2s .co m*/ * @ */ private void buildClients() { // Initialize the HTTP clients asrClient = HttpClientBuilder.create().build(); ttsClient = HttpClientBuilder.create().build(); try { URIBuilder builder = new URIBuilder(); builder.setScheme("https"); builder.setHost("dictation.nuancemobility.net"); builder.setPort(443); builder.setPath("/NMDPAsrCmdServlet/dictation"); builder.setParameter("appId", system.getSettings().params.getProperty("id")); builder.setParameter("appKey", system.getSettings().params.getProperty("key")); builder.setParameter("id", system.getSettings().params.getProperty("0000")); asrURI = builder.build(); builder.setHost("tts.nuancemobility.net"); builder.setPath("/NMDPTTSCmdServlet/tts"); builder.setParameter("ttsLang", system.getSettings().params.getProperty("lang")); ttsURI = builder.build(); } catch (Exception e) { throw new RuntimeException("cannot build client: " + e); } }
From source file:io.kamax.mxisd.dns.ClientDnsOverwrite.java
public URIBuilder transform(URI initial) { URIBuilder builder = new URIBuilder(initial); Entry mapping = mappings.get(initial.getHost()); if (mapping == null) { throw new InternalServerError("No DNS client override for " + initial.getHost()); }//from ww w.j a va2 s .com try { URL target = new URL(mapping.getValue()); builder.setScheme(target.getProtocol()); builder.setHost(target.getHost()); if (target.getPort() != -1) { builder.setPort(target.getPort()); } return builder; } catch (MalformedURLException e) { log.warn("Skipping DNS overwrite entry {} due to invalid value [{}]: {}", mapping.getName(), mapping.getValue(), e.getMessage()); throw new ConfigurationException( "Invalid DNS overwrite entry in homeserver client: " + mapping.getName(), e.getMessage()); } }
From source file:org.zaizi.manifoldcf.authorities.authorities.alfresco.AlfrescoAuthorityConnector.java
/** * Check connection for sanity.//from ww w .java 2s . c o m */ @Override public String check() throws ManifoldCFException { URIBuilder uri = new URIBuilder(); uri.setScheme(protocol); uri.setHost(server); uri.setPort(Integer.parseInt(port)); uri.setPath(path + USER_AUTHORITIES_URI); try { HttpResponse response = httpClient.execute(new HttpGet(uri.build())); EntityUtils.consume(response.getEntity()); if (response.getStatusLine().getStatusCode() != 200) { return "Connection not working! Check Configuration"; } } catch (Exception e) { return "Connection not working! Check Configuration"; } return super.check(); }
From source file:org.opennms.netmgt.poller.monitors.HttpPostMonitor.java
/** * {@inheritDoc}// ww w . j a v a 2 s .c o m * * Poll the specified address for service availability. * * During the poll an attempt is made to execute the named method (with optional input) connect on the specified port. If * the exec on request is successful, the banner line generated by the * interface is parsed and if the banner text indicates that we are talking * to Provided that the interface's response is valid we set the service * status to SERVICE_AVAILABLE and return. */ public PollStatus poll(MonitoredService svc, Map<String, Object> parameters) { NetworkInterface<InetAddress> iface = svc.getNetInterface(); // Process parameters // Get interface address from NetworkInterface if (iface.getType() != NetworkInterface.TYPE_INET) throw new NetworkInterfaceNotSupportedException( "Unsupported interface type, only TYPE_INET currently supported"); TimeoutTracker tracker = new TimeoutTracker(parameters, DEFAULT_RETRY, DEFAULT_TIMEOUT); // Port int port = ParameterMap.getKeyedInteger(parameters, PARAMETER_PORT, DEFAULT_PORT); //URI String strURI = ParameterMap.getKeyedString(parameters, PARAMETER_URI, DEFAULT_URI); //Username String strUser = ParameterMap.getKeyedString(parameters, PARAMETER_USERNAME, null); //Password String strPasswd = ParameterMap.getKeyedString(parameters, PARAMETER_PASSWORD, null); //BannerMatch String strBannerMatch = ParameterMap.getKeyedString(parameters, PARAMETER_BANNER, null); //Scheme String strScheme = ParameterMap.getKeyedString(parameters, PARAMETER_SCHEME, DEFAULT_SCHEME); //Payload String strPayload = ParameterMap.getKeyedString(parameters, PARAMETER_PAYLOAD, null); //Mimetype String strMimetype = ParameterMap.getKeyedString(parameters, PARAMETER_MIMETYPE, DEFAULT_MIMETYPE); //Charset String strCharset = ParameterMap.getKeyedString(parameters, PARAMETER_CHARSET, DEFAULT_CHARSET); //SSLFilter boolean boolSSLFilter = ParameterMap.getKeyedBoolean(parameters, PARAMETER_SSLFILTER, DEFAULT_SSLFILTER); // Get the address instance. InetAddress ipv4Addr = (InetAddress) iface.getAddress(); final String hostAddress = InetAddressUtils.str(ipv4Addr); LOG.debug("poll: address = " + hostAddress + ", port = " + port + ", " + tracker); // Give it a whirl PollStatus serviceStatus = PollStatus.unavailable(); for (tracker.reset(); tracker.shouldRetry() && !serviceStatus.isAvailable(); tracker.nextAttempt()) { HttpClientWrapper clientWrapper = null; try { tracker.startAttempt(); clientWrapper = HttpClientWrapper.create().setConnectionTimeout(tracker.getSoTimeout()) .setSocketTimeout(tracker.getSoTimeout()).setRetries(DEFAULT_RETRY); if (boolSSLFilter) { clientWrapper.trustSelfSigned(strScheme); } HttpEntity postReq; if (strUser != null && strPasswd != null) { clientWrapper.addBasicCredentials(strUser, strPasswd); } try { postReq = new StringEntity(strPayload, ContentType.create(strMimetype, strCharset)); } catch (final UnsupportedCharsetException e) { serviceStatus = PollStatus .unavailable("Unsupported encoding encountered while constructing POST body " + e); break; } URIBuilder ub = new URIBuilder(); ub.setScheme(strScheme); ub.setHost(hostAddress); ub.setPort(port); ub.setPath(strURI); LOG.debug("HttpPostMonitor: Constructed URL is " + ub.toString()); HttpPost post = new HttpPost(ub.build()); post.setEntity(postReq); CloseableHttpResponse response = clientWrapper.execute(post); LOG.debug("HttpPostMonitor: Status Line is " + response.getStatusLine()); if (response.getStatusLine().getStatusCode() > 399) { LOG.info("HttpPostMonitor: Got response status code " + response.getStatusLine().getStatusCode()); LOG.debug("HttpPostMonitor: Received server response: " + response.getStatusLine()); LOG.debug("HttpPostMonitor: Failing on bad status code"); serviceStatus = PollStatus .unavailable("HTTP(S) Status code " + response.getStatusLine().getStatusCode()); break; } LOG.debug("HttpPostMonitor: Response code is valid"); double responseTime = tracker.elapsedTimeInMillis(); HttpEntity entity = response.getEntity(); InputStream responseStream = entity.getContent(); String Strresponse = IOUtils.toString(responseStream); if (Strresponse == null) continue; LOG.debug("HttpPostMonitor: banner = " + Strresponse); LOG.debug("HttpPostMonitor: responseTime= " + responseTime + "ms"); //Could it be a regex? if (strBannerMatch.charAt(0) == '~') { if (!Strresponse.matches(strBannerMatch.substring(1))) { serviceStatus = PollStatus .unavailable("Banner does not match Regex '" + strBannerMatch + "'"); break; } else { serviceStatus = PollStatus.available(responseTime); } } else { if (Strresponse.indexOf(strBannerMatch) > -1) { serviceStatus = PollStatus.available(responseTime); } else { serviceStatus = PollStatus .unavailable("Did not find expected Text '" + strBannerMatch + "'"); break; } } } catch (final URISyntaxException e) { final String reason = "URISyntaxException for URI: " + strURI + " " + e.getMessage(); LOG.debug(reason, e); serviceStatus = PollStatus.unavailable(reason); break; } catch (final Exception e) { final String reason = "Exception: " + e.getMessage(); LOG.debug(reason, e); serviceStatus = PollStatus.unavailable(reason); break; } finally { IOUtils.closeQuietly(clientWrapper); } } // return the status of the service return serviceStatus; }
From source file:org.zaizi.manifoldcf.authorities.authorities.alfresco.AlfrescoAuthorityConnector.java
/** * Obtain the access tokens for a given user name. * //from w w w . jav a 2s . c o m * @param userName is the user name or identifier. * @return the response tokens (according to the current authority). (Should throws an exception only when a * condition cannot be properly described within the authorization response object.) */ @Override public AuthorizationResponse getAuthorizationResponse(String userName) throws ManifoldCFException { // String[] tokens = new String[] { userName }; // return new AuthorizationResponse(tokens, AuthorizationResponse.RESPONSE_OK); List<String> tokens = new ArrayList<String>(); try { URIBuilder uri = new URIBuilder(); uri.setScheme(protocol); uri.setHost(server); uri.setPort(Integer.parseInt(port)); uri.setPath(path + USER_AUTHORITIES_URI); uri.setParameter(USERNAME_PARAM, userName); HttpGet get = new HttpGet(uri.build()); HttpResponse response = httpClient.execute(get); JSONObject json = new JSONObject(EntityUtils.toString(response.getEntity(), UTF8)); JSONArray auths = (JSONArray) json.get(AUTHORITIES_JSON); if (auths != null) { int len = auths.length(); for (int i = 0; i < len; i++) { tokens.add(auths.get(i).toString()); } } EntityUtils.consume(response.getEntity()); } catch (Exception e) { return new AuthorizationResponse(null, AuthorizationResponse.RESPONSE_UNREACHABLE); } return new AuthorizationResponse(tokens.toArray(new String[] {}), AuthorizationResponse.RESPONSE_OK); }