List of usage examples for org.apache.http.client.utils URIBuilder setScheme
public URIBuilder setScheme(final String scheme)
From source file:org.opennms.netmgt.provision.detector.web.client.WebClient.java
@Override public void connect(InetAddress address, int port, int timeout) throws IOException, Exception { final URIBuilder ub = new URIBuilder(); ub.setScheme(m_schema); ub.setHost(InetAddressUtils.str(address)); ub.setPort(port);// ww w . j a v a 2 s . c om ub.setPath(m_path); if (m_queryString != null && m_queryString.trim().length() > 0) { final List<NameValuePair> params = URLEncodedUtils.parse(m_queryString, Charset.forName("UTF-8")); if (!params.isEmpty()) { ub.setParameters(params); } } m_httpMethod = new HttpGet(ub.build()); m_httpMethod.setProtocolVersion(m_version); m_httpClientWrapper = HttpClientWrapper.create(); if (m_overrideSSL) { try { m_httpClientWrapper.trustSelfSigned("https"); } catch (final Exception e) { LOG.warn("Failed to create relaxed SSL client.", e); } } if (m_userAgent != null && !m_userAgent.trim().isEmpty()) { m_httpClientWrapper.setUserAgent(m_userAgent); } if (timeout > 0) { m_httpClientWrapper.setConnectionTimeout(timeout); m_httpClientWrapper.setSocketTimeout(timeout); } if (m_virtualHost != null && !m_virtualHost.trim().isEmpty()) { m_httpClientWrapper.setVirtualHost(m_virtualHost); } if (m_userName != null && !m_userName.trim().isEmpty()) { m_httpClientWrapper.addBasicCredentials(m_userName, m_password); } if (m_authPreemptive) { m_httpClientWrapper.usePreemptiveAuth(); } }
From source file:org.opennms.protocols.http.HttpUrlConnection.java
@Override public InputStream getInputStream() throws IOException { try {/*from w w w.j a v a2 s .com*/ if (m_clientWrapper == null) { connect(); } // Build URL int port = m_url.getPort() > 0 ? m_url.getPort() : m_url.getDefaultPort(); URIBuilder ub = new URIBuilder(); ub.setPort(port); ub.setScheme(m_url.getProtocol()); ub.setHost(m_url.getHost()); ub.setPath(m_url.getPath()); if (m_url.getQuery() != null && !m_url.getQuery().trim().isEmpty()) { final List<NameValuePair> params = URLEncodedUtils.parse(m_url.getQuery(), Charset.forName("UTF-8")); if (!params.isEmpty()) { ub.addParameters(params); } } // Build Request HttpRequestBase request = null; if (m_request != null && m_request.getMethod().equalsIgnoreCase("post")) { final Content cnt = m_request.getContent(); HttpPost post = new HttpPost(ub.build()); ContentType contentType = ContentType.create(cnt.getType()); LOG.info("Processing POST request for {}", contentType); if (contentType.getMimeType().equals(ContentType.APPLICATION_FORM_URLENCODED.getMimeType())) { FormFields fields = JaxbUtils.unmarshal(FormFields.class, cnt.getData()); post.setEntity(fields.getEntity()); } else { StringEntity entity = new StringEntity(cnt.getData(), contentType); post.setEntity(entity); } request = post; } else { request = new HttpGet(ub.build()); } if (m_request != null) { // Add Custom Headers for (final Header header : m_request.getHeaders()) { request.addHeader(header.getName(), header.getValue()); } } // Get Response CloseableHttpResponse response = m_clientWrapper.execute(request); return response.getEntity().getContent(); } catch (Exception e) { throw new IOException( "Can't retrieve " + m_url.getPath() + " from " + m_url.getHost() + " because " + e.getMessage(), e); } }
From source file:org.wso2.carbon.analytics.api.internal.client.AnalyticsAPIHttpClient.java
public void authenticate(String username, String password) throws AnalyticsServiceException { URIBuilder builder = new URIBuilder(); builder.setScheme(this.protocol).setHost(hostname).setPort(port) .setPath(AnalyticsAPIConstants.MANAGEMENT_SERVICE_URI) .setParameter(AnalyticsAPIConstants.OPERATION, AnalyticsAPIConstants.LOGIN_OPERATION); try {//from www .j a va 2s . c om HttpGet getMethod = new HttpGet(builder.build().toString()); getMethod.addHeader(AnalyticsAPIConstants.AUTHORIZATION_HEADER, AnalyticsAPIConstants.BASIC_AUTH_HEADER + Base64.encode((username + AnalyticsAPIConstants.SEPARATOR + password).getBytes())); HttpResponse httpResponse = httpClient.execute(getMethod); if (httpResponse.getStatusLine().getStatusCode() != HttpServletResponse.SC_OK) { String response = httpResponse.getStatusLine().toString(); EntityUtils.consume(httpResponse.getEntity()); if (httpResponse.getStatusLine().getStatusCode() != HttpServletResponse.SC_NOT_FOUND) { throw new AnalyticsServiceAuthenticationException("Authentication failed for user : " + username + " ." + "Response received from remote instance : " + response); } else { throw new AnalyticsServiceRemoteException( "Unable to reach the endpoint : " + builder.build().toString() + ". " + response); } } String response = getResponseString(httpResponse); if (response.startsWith(AnalyticsAPIConstants.SESSION_ID)) { String[] reponseElements = response.split(AnalyticsAPIConstants.SEPARATOR); if (reponseElements.length == 2) { this.sessionId = reponseElements[1]; } else { throw new AnalyticsServiceAuthenticationException( "Invalid response returned, cannot find " + "sessionId. Response:" + response); } } else { throw new AnalyticsServiceAuthenticationException( "Invalid response returned, no session id found!" + response); } } catch (URISyntaxException e) { throw new AnalyticsServiceAuthenticationException( "Malformed URL provided for authentication. " + e.getMessage(), e); } catch (IOException e) { throw new AnalyticsServiceRemoteException( "Error while connecting to the remote service. " + e.getMessage(), e); } }
From source file:org.wso2.carbon.analytics.api.internal.client.AnalyticsAPIHttpClient.java
public void createTable(int tenantId, String username, String recordStoreName, String tableName, boolean securityEnabled) throws AnalyticsServiceException { URIBuilder builder = new URIBuilder(); builder.setScheme(protocol).setHost(hostname).setPort(port) .setPath(AnalyticsAPIConstants.TABLE_PROCESSOR_SERVICE_URI); try {//w w w . j a v a 2 s .com HttpPost postMethod = new HttpPost(builder.build().toString()); postMethod.addHeader(AnalyticsAPIConstants.SESSION_ID, sessionId); List<NameValuePair> params = new ArrayList<>(); params.add(new BasicNameValuePair(AnalyticsAPIConstants.OPERATION, AnalyticsAPIConstants.CREATE_TABLE_OPERATION)); if (!securityEnabled) { params.add(new BasicNameValuePair(AnalyticsAPIConstants.TENANT_ID_PARAM, String.valueOf(tenantId))); } else { params.add(new BasicNameValuePair(AnalyticsAPIConstants.USERNAME_PARAM, username)); } if (recordStoreName != null) { params.add(new BasicNameValuePair(AnalyticsAPIConstants.RECORD_STORE_NAME_PARAM, recordStoreName)); } params.add(new BasicNameValuePair(AnalyticsAPIConstants.ENABLE_SECURITY_PARAM, String.valueOf(securityEnabled))); params.add(new BasicNameValuePair(AnalyticsAPIConstants.TABLE_NAME_PARAM, tableName)); postMethod.setEntity(new UrlEncodedFormEntity(params)); HttpResponse httpResponse = httpClient.execute(postMethod); if (httpResponse.getStatusLine().getStatusCode() != HttpServletResponse.SC_OK) { String response = getResponseString(httpResponse); throw new AnalyticsServiceException("Unable to create the table - " + tableName + " for tenant id : " + tenantId + ". " + response); } else { EntityUtils.consume(httpResponse.getEntity()); } } catch (URISyntaxException e) { throw new AnalyticsServiceAuthenticationException("Malformed URL provided. " + e.getMessage(), e); } catch (IOException e) { throw new AnalyticsServiceAuthenticationException( "Error while connecting to the remote service. " + e.getMessage(), e); } }
From source file:org.wso2.carbon.analytics.api.internal.client.AnalyticsAPIHttpClient.java
public void setTableSchema(int tenantId, String username, String tableName, AnalyticsSchema schema, boolean securityEnabled) throws AnalyticsServiceException { URIBuilder builder = new URIBuilder(); builder.setScheme(protocol).setHost(hostname).setPort(port) .setPath(AnalyticsAPIConstants.SCHEMA_PROCESSOR_SERVICE_URI) .addParameter(AnalyticsAPIConstants.OPERATION, AnalyticsAPIConstants.SET_SCHEMA_OPERATION) .addParameter(AnalyticsAPIConstants.ENABLE_SECURITY_PARAM, String.valueOf(securityEnabled)) .addParameter(AnalyticsAPIConstants.TENANT_ID_PARAM, String.valueOf(tenantId)) .addParameter(AnalyticsAPIConstants.TABLE_NAME_PARAM, tableName); if (!securityEnabled) { builder.addParameter(AnalyticsAPIConstants.TENANT_ID_PARAM, String.valueOf(tenantId)); } else {//from w w w. j a v a2 s . c o m builder.addParameter(AnalyticsAPIConstants.USERNAME_PARAM, username); } try { HttpPost postMethod = new HttpPost(builder.build().toString()); postMethod.addHeader(AnalyticsAPIConstants.SESSION_ID, sessionId); postMethod.setEntity(new ByteArrayEntity(GenericUtils.serializeObject(schema))); HttpResponse httpResponse = httpClient.execute(postMethod); if (httpResponse.getStatusLine().getStatusCode() != HttpServletResponse.SC_OK) { String response = getResponseString(httpResponse); throw new AnalyticsServiceException("Unable to set the schema for the table - " + tableName + ", schema - " + gson.toJson(schema) + " for tenant id : " + tenantId + ". " + response); } else { EntityUtils.consume(httpResponse.getEntity()); } } catch (URISyntaxException e) { throw new AnalyticsServiceAuthenticationException("Malformed URL provided. " + e.getMessage(), e); } catch (IOException e) { throw new AnalyticsServiceAuthenticationException( "Error while connecting to the remote service. " + e.getMessage(), e); } }
From source file:org.wso2.carbon.analytics.api.internal.client.AnalyticsAPIHttpClient.java
public AnalyticsSchema getTableSchema(int tenantId, String username, String tableName, boolean securityEnabled) throws AnalyticsServiceException { URIBuilder builder = new URIBuilder(); builder.setScheme(protocol).setHost(hostname).setPort(port) .setPath(AnalyticsAPIConstants.SCHEMA_PROCESSOR_SERVICE_URI) .addParameter(AnalyticsAPIConstants.OPERATION, AnalyticsAPIConstants.GET_SCHEMA_OPERATION) .addParameter(AnalyticsAPIConstants.TABLE_NAME_PARAM, tableName) .addParameter(AnalyticsAPIConstants.ENABLE_SECURITY_PARAM, String.valueOf(securityEnabled)); if (!securityEnabled) { builder.addParameter(AnalyticsAPIConstants.TENANT_ID_PARAM, String.valueOf(tenantId)); } else {/* w w w. j ava 2s. c o m*/ builder.addParameter(AnalyticsAPIConstants.USERNAME_PARAM, username); } try { HttpGet getMethod = new HttpGet(builder.build().toString()); getMethod.addHeader(AnalyticsAPIConstants.SESSION_ID, sessionId); HttpResponse httpResponse = httpClient.execute(getMethod); if (httpResponse.getStatusLine().getStatusCode() != HttpServletResponse.SC_OK) { String response = getResponseString(httpResponse); throw new AnalyticsServiceException("Unable to get the schema for the table - " + tableName + " for tenant id : " + tenantId + ". " + response); } else { Object analyticsSchemaObject = GenericUtils .deserializeObject(httpResponse.getEntity().getContent()); EntityUtils.consumeQuietly(httpResponse.getEntity()); if (analyticsSchemaObject != null && analyticsSchemaObject instanceof AnalyticsSchema) { return (AnalyticsSchema) analyticsSchemaObject; } else { throw new AnalyticsServiceException( getUnexpectedResponseReturnedErrorMsg("getting the table schema", tableName, "analytics schema object ", analyticsSchemaObject)); } } } catch (URISyntaxException e) { throw new AnalyticsServiceAuthenticationException("Malformed URL provided. " + e.getMessage(), e); } catch (IOException e) { throw new AnalyticsServiceAuthenticationException( "Error while connecting to the remote service. " + e.getMessage(), e); } }
From source file:org.wso2.carbon.analytics.api.internal.client.AnalyticsAPIHttpClient.java
public boolean isTableExists(int tenantId, String username, String tableName, boolean securityEnabled) throws AnalyticsServiceException { URIBuilder builder = new URIBuilder(); builder.setScheme(protocol).setHost(hostname).setPort(port) .setPath(AnalyticsAPIConstants.TABLE_PROCESSOR_SERVICE_URI) .addParameter(AnalyticsAPIConstants.OPERATION, AnalyticsAPIConstants.TABLE_EXISTS_OPERATION) .addParameter(AnalyticsAPIConstants.TABLE_NAME_PARAM, tableName) .addParameter(AnalyticsAPIConstants.ENABLE_SECURITY_PARAM, String.valueOf(securityEnabled)); if (!securityEnabled) { builder.addParameter(AnalyticsAPIConstants.TENANT_ID_PARAM, String.valueOf(tenantId)); } else {//from w w w .j a v a 2 s. c o m builder.addParameter(AnalyticsAPIConstants.USERNAME_PARAM, username); } try { HttpGet getMethod = new HttpGet(builder.build().toString()); getMethod.addHeader(AnalyticsAPIConstants.SESSION_ID, sessionId); HttpResponse httpResponse = httpClient.execute(getMethod); String response = getResponseString(httpResponse); if (httpResponse.getStatusLine().getStatusCode() != HttpServletResponse.SC_OK) { throw new AnalyticsServiceException("Unable to check the existence for the table - " + tableName + " for tenant id : " + tenantId + ". " + response); } else { if (response.startsWith(AnalyticsAPIConstants.TABLE_EXISTS)) { String[] reponseElements = response.split(AnalyticsAPIConstants.SEPARATOR); if (reponseElements.length == 2) { return Boolean.parseBoolean(reponseElements[1]); } else { throw new AnalyticsServiceException("Invalid response returned, cannot find table existence" + " message. Response:" + response); } } else { throw new AnalyticsServiceException( "Invalid response returned, table existence message not found!" + response); } } } catch (URISyntaxException e) { throw new AnalyticsServiceAuthenticationException("Malformed URL provided. " + e.getMessage(), e); } catch (IOException e) { throw new AnalyticsServiceAuthenticationException( "Error while connecting to the remote service. " + e.getMessage(), e); } }
From source file:org.wso2.carbon.analytics.api.internal.client.AnalyticsAPIHttpClient.java
@SuppressWarnings("unchecked") public List<String> listTables(int tenantId, String username, boolean securityEnabled) throws AnalyticsServiceException { URIBuilder builder = new URIBuilder(); builder.setScheme(protocol).setHost(hostname).setPort(port) .setPath(AnalyticsAPIConstants.TABLE_PROCESSOR_SERVICE_URI) .addParameter(AnalyticsAPIConstants.OPERATION, AnalyticsAPIConstants.LIST_TABLES_OPERATION) .addParameter(AnalyticsAPIConstants.ENABLE_SECURITY_PARAM, String.valueOf(securityEnabled)); if (!securityEnabled) { builder.addParameter(AnalyticsAPIConstants.TENANT_ID_PARAM, String.valueOf(tenantId)); } else {// w ww .j a v a2s. c om builder.addParameter(AnalyticsAPIConstants.USERNAME_PARAM, username); } try { HttpGet getMethod = new HttpGet(builder.build().toString()); getMethod.addHeader(AnalyticsAPIConstants.SESSION_ID, sessionId); HttpResponse httpResponse = httpClient.execute(getMethod); if (httpResponse.getStatusLine().getStatusCode() != HttpServletResponse.SC_OK) { String response = getResponseString(httpResponse); throw new AnalyticsServiceException( "Unable to get the list of tables for tenant id : " + tenantId + ". " + response); } else { Object listOfTablesObj = GenericUtils.deserializeObject(httpResponse.getEntity().getContent()); EntityUtils.consumeQuietly(httpResponse.getEntity()); if (listOfTablesObj != null && listOfTablesObj instanceof List) { return (List<String>) listOfTablesObj; } else { throw new AnalyticsServiceException(getUnexpectedResponseReturnedErrorMsg( "getting list of tables", null, "list of tables", listOfTablesObj)); } } } catch (URISyntaxException e) { throw new AnalyticsServiceAuthenticationException("Malformed URL provided. " + e.getMessage(), e); } catch (IOException e) { throw new AnalyticsServiceAuthenticationException( "Error while connecting to the remote service. " + e.getMessage(), e); } }
From source file:org.wso2.carbon.analytics.api.internal.client.AnalyticsAPIHttpClient.java
public void deleteTable(int tenantId, String username, String tableName, boolean securityEnabled) throws AnalyticsServiceException { URIBuilder builder = new URIBuilder(); builder.setScheme(protocol).setHost(hostname).setPort(port) .setPath(AnalyticsAPIConstants.TABLE_PROCESSOR_SERVICE_URI) .addParameter(AnalyticsAPIConstants.OPERATION, AnalyticsAPIConstants.DELETE_TABLE_OPERATION) .addParameter(AnalyticsAPIConstants.TABLE_NAME_PARAM, tableName) .addParameter(AnalyticsAPIConstants.ENABLE_SECURITY_PARAM, String.valueOf(securityEnabled)); if (!securityEnabled) { builder.addParameter(AnalyticsAPIConstants.TENANT_ID_PARAM, String.valueOf(tenantId)); } else {// w w w . j a v a 2 s.co m builder.addParameter(AnalyticsAPIConstants.USERNAME_PARAM, username); } try { HttpDelete deleteMethod = new HttpDelete(builder.build().toString()); deleteMethod.addHeader(AnalyticsAPIConstants.SESSION_ID, sessionId); HttpResponse httpResponse = httpClient.execute(deleteMethod); if (httpResponse.getStatusLine().getStatusCode() != HttpServletResponse.SC_OK) { String response = getResponseString(httpResponse); throw new AnalyticsServiceException("Unable to create the table - " + tableName + " for tenant id : " + tenantId + ". " + response); } else { EntityUtils.consume(httpResponse.getEntity()); } } catch (URISyntaxException e) { throw new AnalyticsServiceAuthenticationException("Malformed URL provided. " + e.getMessage(), e); } catch (IOException e) { throw new AnalyticsServiceAuthenticationException( "Error while connecting to the remote service. " + e.getMessage(), e); } }
From source file:org.wso2.carbon.analytics.api.internal.client.AnalyticsAPIHttpClient.java
public long getRecordCount(int tenantId, String username, String tableName, long timeFrom, long timeTo, boolean securityEnabled) throws AnalyticsServiceException { URIBuilder builder = new URIBuilder(); builder.setScheme(protocol).setHost(hostname).setPort(port) .setPath(AnalyticsAPIConstants.RECORD_PROCESSOR_SERVICE_URI) .addParameter(AnalyticsAPIConstants.OPERATION, AnalyticsAPIConstants.GET_RECORD_COUNT_OPERATION) .addParameter(AnalyticsAPIConstants.TABLE_NAME_PARAM, tableName) .addParameter(AnalyticsAPIConstants.TIME_FROM_PARAM, String.valueOf(timeFrom)) .addParameter(AnalyticsAPIConstants.TIME_TO_PARAM, String.valueOf(timeTo)) .addParameter(AnalyticsAPIConstants.ENABLE_SECURITY_PARAM, String.valueOf(securityEnabled)); if (!securityEnabled) { builder.addParameter(AnalyticsAPIConstants.TENANT_ID_PARAM, String.valueOf(tenantId)); } else {/* w w w. ja v a2 s . c o m*/ builder.addParameter(AnalyticsAPIConstants.USERNAME_PARAM, username); } try { HttpGet getMethod = new HttpGet(builder.build().toString()); getMethod.addHeader(AnalyticsAPIConstants.SESSION_ID, sessionId); HttpResponse httpResponse = httpClient.execute(getMethod); String response = getResponseString(httpResponse); if (httpResponse.getStatusLine().getStatusCode() != HttpServletResponse.SC_OK) { throw new AnalyticsServiceException( "Unable to get the record count for the table - " + tableName + ", time from : " + timeFrom + " , timeTo : " + timeTo + " for tenant id : " + tenantId + ". " + response); } else { if (response.startsWith(AnalyticsAPIConstants.RECORD_COUNT)) { String[] reponseElements = response.split(AnalyticsAPIConstants.SEPARATOR); if (reponseElements.length == 2) { return Long.parseLong(reponseElements[1]); } else { throw new AnalyticsServiceException("Invalid response returned, cannot find record count" + " message. Response:" + response); } } else { throw new AnalyticsServiceException( "Invalid response returned, record count message not found!" + response); } } } catch (URISyntaxException e) { throw new AnalyticsServiceAuthenticationException("Malformed URL provided. " + e.getMessage(), e); } catch (IOException e) { throw new AnalyticsServiceAuthenticationException( "Error while connecting to the remote service. " + e.getMessage(), e); } }