use of org.apache.http.client.ClientProtocolException in project opennms by OpenNMS.
the class HttpNorthbounder method forwardAlarms.
/* (non-Javadoc)
* @see org.opennms.netmgt.alarmd.api.support.AbstractNorthbounder#forwardAlarms(java.util.List)
*/
@Override
public void forwardAlarms(List<NorthboundAlarm> alarms) throws NorthbounderException {
LOG.info("Forwarding {} alarms", alarms.size());
//Need a configuration bean for these
int connectionTimeout = 3000;
int socketTimeout = 3000;
Integer retryCount = Integer.valueOf(3);
URI uri = m_config.getURI();
final HttpClientWrapper clientWrapper = HttpClientWrapper.create().setConnectionTimeout(connectionTimeout).setSocketTimeout(socketTimeout).setRetries(retryCount).useBrowserCompatibleCookies();
if (m_config.getVirtualHost() != null && !m_config.getVirtualHost().trim().isEmpty()) {
clientWrapper.setVirtualHost(m_config.getVirtualHost());
}
if (m_config.getUserAgent() != null && !m_config.getUserAgent().trim().isEmpty()) {
clientWrapper.setUserAgent(m_config.getUserAgent());
}
if ("https".equals(uri.getScheme())) {
try {
clientWrapper.useRelaxedSSL("https");
} catch (final GeneralSecurityException e) {
throw new NorthbounderException("Failed to configure HTTP northbounder for relaxed SSL.", e);
}
}
HttpUriRequest method = null;
if (HttpMethod.POST == (m_config.getMethod())) {
HttpPost postMethod = new HttpPost(uri);
//TODO: need to configure these
List<NameValuePair> postParms = new ArrayList<NameValuePair>();
//FIXME:do this for now
NameValuePair p = new BasicNameValuePair("foo", "bar");
postParms.add(p);
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParms, StandardCharsets.UTF_8);
postMethod.setEntity(formEntity);
HttpEntity entity = null;
try {
//I have no idea what I'm doing here ;)
entity = new StringEntity("XML HERE");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
postMethod.setEntity(entity);
method = postMethod;
} else if (HttpMethod.GET == m_config.getMethod()) {
//TODO: need to configure these
//List<NameValuePair> getParms = null;
method = new HttpGet(uri);
}
HttpVersion httpVersion = determineHttpVersion(m_config.getHttpVersion());
clientWrapper.setVersion(httpVersion);
HttpResponse response = null;
try {
response = clientWrapper.execute(method);
int code = response.getStatusLine().getStatusCode();
HttpResponseRange range = new HttpResponseRange("200-399");
if (!range.contains(code)) {
LOG.debug("response code out of range for uri:{}. Expected {} but received {}", uri, range, code);
throw new NorthbounderException("response code out of range for uri:" + uri + ". Expected " + range + " but received " + code);
}
LOG.debug("HTTP Northbounder received response: {}", response.getStatusLine().getReasonPhrase());
} catch (final ClientProtocolException e) {
throw new NorthbounderException(e);
} catch (final IOException e) {
throw new NorthbounderException(e);
} finally {
IOUtils.closeQuietly(clientWrapper);
}
}
use of org.apache.http.client.ClientProtocolException in project java-chassis by ServiceComb.
the class TestHttpsClient method testHttpClient.
@Test
public void testHttpClient() throws ClientProtocolException, IOException {
// test valid Invalid Inputs
Map<String, String> oHeaders = new HashMap<String, String>();
oHeaders.put("X-Auth", "JHGUGJGH");
HttpResponse oResponse = HttpsClient.execute("UNKNOWN METHOD", oHeaders, "//check", "body", null);
Assert.assertEquals(null, oResponse);
oResponse = HttpsClient.execute("", oHeaders, "//check", "body", null);
Assert.assertEquals(null, oResponse);
oResponse = HttpsClient.execute("UNKNOWN METHOD", oHeaders, "", "body", null);
Assert.assertEquals(null, oResponse);
oResponse = HttpsClient.execute("UNKNOWN METHOD", null, "//check", "body", null);
Assert.assertEquals(null, oResponse);
// With default Bean
HttpsConfigInfoBean oBean = new HttpsConfigInfoBean();
oBean.setKeyStorePath("JHGJ");
oBean.setKeyStorePasswd("HJGJH");
oBean.setTrustStorePasswd("JHGJHG");
oBean.setTrustStorePath("JHGJGj");
Assert.assertEquals("JHGJGj", oBean.getTrustStorePath());
Assert.assertEquals("JHGJHG", oBean.getTrustStorePasswd());
oResponse = HttpsClient.execute("UNKNOWN METHOD", oHeaders, "//check", "body", oBean);
Assert.assertEquals(null, oResponse);
HttpsClient.getHttpsClient(oBean);
Assert.assertNotEquals(null, HttpsClient.getHttpsClient(Mockito.mock(HttpsConfigInfoBean.class)));
//Handle Error Scenarios
try {
oResponse = HttpsClient.execute("POST", oHeaders, "//check", "body", oBean);
} catch (Exception e) {
Assert.assertEquals("Target host is null", e.getMessage());
}
try {
oResponse = HttpsClient.execute("GET", oHeaders, "//check", "body", oBean);
} catch (Exception e) {
Assert.assertEquals("Target host is null", e.getMessage());
}
try {
oResponse = HttpsClient.execute("DELETE", oHeaders, "//check", "body", oBean);
} catch (Exception e) {
Assert.assertEquals("Target host is null", e.getMessage());
}
// TODO Check the valid Responses
}
use of org.apache.http.client.ClientProtocolException in project tdi-studio-se by Talend.
the class WsdlTokenManager method getSOAPResponse.
private static String getSOAPResponse(URI issuerUri, String soapEnvelope) {
HttpResponse response = null;
// Create the request that will submit the request to the server
try {
HttpParams params = new BasicHttpParams();
params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 180000);
HttpClient client = new SystemDefaultHttpClient(params);
HttpPost post = new HttpPost(issuerUri);
StringEntity entity = new StringEntity(soapEnvelope);
post.setHeader("Content-Type", "application/soap+xml; charset=UTF-8");
post.setEntity(entity);
response = client.execute(post);
return EntityUtils.toString(response.getEntity());
} catch (ClientProtocolException e) {
logger.error(e.getMessage());
} catch (IOException e) {
logger.error(e.getMessage());
}
return null;
}
use of org.apache.http.client.ClientProtocolException in project selenium-tests by Wikia.
the class Helios method deleteAllTokens.
public static void deleteAllTokens(User user) {
String heliosGetTokenURL = HeliosConfig.getUrl(HeliosConfig.HeliosController.USERS);
HttpDelete httpDelete = new HttpDelete(String.format("%s/%s/tokens", heliosGetTokenURL, user.getUserId()));
httpDelete.setHeader("THE-SCHWARTZ", Configuration.getCredentials().apiToken);
CloseableHttpResponse response = null;
try {
response = getDefaultClient().execute(httpDelete);
PageObjectLogging.log("DELETE HEADERS: ", response.toString(), true);
} catch (ClientProtocolException e) {
PageObjectLogging.log("CLIENT PROTOCOL EXCEPTION", ExceptionUtils.getStackTrace(e), false);
throw new WebDriverException(e);
} catch (IOException e) {
PageObjectLogging.log(IOEXCEPTION_COMMAND, IOEXCEPTION_ERROR_MESSAGE + ExceptionUtils.getStackTrace(e), false);
throw new WebDriverException(e);
}
}
use of org.apache.http.client.ClientProtocolException in project selenium-tests by Wikia.
the class ApiCall method call.
public void call() {
try {
URL url = new URL(getURL());
CloseableHttpClient httpClient = HttpClientBuilder.create().disableAutomaticRetries().build();
HttpPost httpPost = getHtppPost(url);
// set header
if (getUser() != null) {
httpPost.addHeader("X-Wikia-AccessToken", Helios.getAccessToken(getUser()));
}
// set query params
if (getParams() != null) {
httpPost.setEntity(new UrlEncodedFormEntity(getParams(), StandardCharsets.UTF_8));
}
httpClient.execute(httpPost);
PageObjectLogging.log("CONTENT PUSH", "Content posted to: " + getURL(), true);
} catch (ClientProtocolException e) {
PageObjectLogging.log("EXCEPTION", ExceptionUtils.getStackTrace(e), false);
throw new WebDriverException(ERROR_MESSAGE);
} catch (IOException e) {
PageObjectLogging.log("IO EXCEPTION", ExceptionUtils.getStackTrace(e), false);
throw new WebDriverException(ERROR_MESSAGE);
} catch (URISyntaxException e) {
PageObjectLogging.log("URI_SYNTAX EXCEPTION", ExceptionUtils.getStackTrace(e), false);
throw new WebDriverException(ERROR_MESSAGE);
}
}
Aggregations