Search in sources :

Example 21 with ClientProtocolException

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);
    }
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpPost(org.apache.http.client.methods.HttpPost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) HttpEntity(org.apache.http.HttpEntity) GeneralSecurityException(java.security.GeneralSecurityException) HttpGet(org.apache.http.client.methods.HttpGet) ArrayList(java.util.ArrayList) UnsupportedEncodingException(java.io.UnsupportedEncodingException) HttpResponse(org.apache.http.HttpResponse) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) IOException(java.io.IOException) URI(java.net.URI) ClientProtocolException(org.apache.http.client.ClientProtocolException) HttpResponseRange(org.opennms.core.utils.HttpResponseRange) StringEntity(org.apache.http.entity.StringEntity) NorthbounderException(org.opennms.netmgt.alarmd.api.NorthbounderException) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) HttpClientWrapper(org.opennms.core.web.HttpClientWrapper) HttpVersion(org.apache.http.HttpVersion)

Example 22 with ClientProtocolException

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
}
Also used : HashMap(java.util.HashMap) HttpResponse(org.apache.http.HttpResponse) HttpsConfigInfoBean(io.servicecomb.foundation.common.entities.HttpsConfigInfoBean) ClientProtocolException(org.apache.http.client.ClientProtocolException) IOException(java.io.IOException) KeyStoreException(java.security.KeyStoreException) GeneralSecurityException(java.security.GeneralSecurityException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Test(org.junit.Test)

Example 23 with ClientProtocolException

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;
}
Also used : SystemDefaultHttpClient(org.apache.http.impl.client.SystemDefaultHttpClient) HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) BasicHttpParams(org.apache.http.params.BasicHttpParams) HttpParams(org.apache.http.params.HttpParams) HttpClient(org.apache.http.client.HttpClient) SystemDefaultHttpClient(org.apache.http.impl.client.SystemDefaultHttpClient) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) BasicHttpParams(org.apache.http.params.BasicHttpParams) ClientProtocolException(org.apache.http.client.ClientProtocolException)

Example 24 with ClientProtocolException

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);
    }
}
Also used : HttpDelete(org.apache.http.client.methods.HttpDelete) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) IOException(java.io.IOException) ClientProtocolException(org.apache.http.client.ClientProtocolException) WebDriverException(org.openqa.selenium.WebDriverException)

Example 25 with ClientProtocolException

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);
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpPost(org.apache.http.client.methods.HttpPost) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) URL(java.net.URL) ClientProtocolException(org.apache.http.client.ClientProtocolException) WebDriverException(org.openqa.selenium.WebDriverException)

Aggregations

ClientProtocolException (org.apache.http.client.ClientProtocolException)83 IOException (java.io.IOException)75 HttpResponse (org.apache.http.HttpResponse)45 HttpPost (org.apache.http.client.methods.HttpPost)33 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)29 HttpClient (org.apache.http.client.HttpClient)28 HttpGet (org.apache.http.client.methods.HttpGet)24 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)20 HttpEntity (org.apache.http.HttpEntity)19 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)19 ArrayList (java.util.ArrayList)18 UnsupportedEncodingException (java.io.UnsupportedEncodingException)17 InputStreamReader (java.io.InputStreamReader)14 NameValuePair (org.apache.http.NameValuePair)13 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)13 InputStream (java.io.InputStream)12 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)11 BufferedReader (java.io.BufferedReader)9 StringEntity (org.apache.http.entity.StringEntity)9 JSONException (org.json.JSONException)9