Search in sources :

Example 16 with HttpContext

use of org.apache.http.protocol.HttpContext in project tmdm-studio-se by Talend.

the class HttpClientUtil method uploadImageFile.

public static String uploadImageFile(String URL, String localFilename, String filename, String imageCatalog, String username, String password, HashMap<String, String> picturePathMap) throws XtentisException {
    HttpUriRequest request = createUploadRequest(URL, username, localFilename, filename, imageCatalog, picturePathMap);
    DefaultHttpClient client = wrapAuthClient(URL, username, password);
    HttpContext preemptiveContext = getPreemptiveContext(URL);
    authenticate(username, password, request, preemptiveContext);
    // $NON-NLS-1$//$NON-NLS-2$
    String errMessage = Messages.Util_25 + "%s" + Messages.Util_26 + "%s";
    String content = getTextContent(client, request, preemptiveContext, errMessage);
    if (null == content) {
        // $NON-NLS-1$
        throw new XtentisException("no response content");
    }
    if (content.contains("upload")) {
        // $NON-NLS-1$
        // $NON-NLS-1$//$NON-NLS-2$
        String returnValue = content.substring(content.indexOf("upload"), content.indexOf("}") - 1);
        if (picturePathMap != null) {
            File file = new File(localFilename);
            String fileName1 = file.getName();
            picturePathMap.put(fileName1, returnValue);
        }
        return returnValue;
    } else {
        // $NON-NLS-1$
        return "";
    }
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) HttpContext(org.apache.http.protocol.HttpContext) File(java.io.File) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient)

Example 17 with HttpContext

use of org.apache.http.protocol.HttpContext in project iaf by ibissource.

the class HttpResponseMock method answer.

@Override
public HttpResponse answer(InvocationOnMock invocation) throws Throwable {
    HttpHost host = (HttpHost) invocation.getArguments()[0];
    HttpRequestBase request = (HttpRequestBase) invocation.getArguments()[1];
    HttpContext context = (HttpContext) invocation.getArguments()[2];
    InputStream response = null;
    if (request instanceof HttpGet)
        response = doGet(host, (HttpGet) request, context);
    else if (request instanceof HttpPost)
        response = doPost(host, (HttpPost) request, context);
    else if (request instanceof HttpPut)
        response = doPut(host, (HttpPut) request, context);
    else if (request instanceof HttpPatch)
        response = doPatch(host, (HttpPatch) request, context);
    else if (request instanceof HttpDelete)
        response = doDelete(host, (HttpDelete) request, context);
    else
        throw new Exception("mock method not implemented");
    return buildResponse(response);
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) HttpDelete(org.apache.http.client.methods.HttpDelete) HttpHost(org.apache.http.HttpHost) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) HttpGet(org.apache.http.client.methods.HttpGet) HttpContext(org.apache.http.protocol.HttpContext) HttpPut(org.apache.http.client.methods.HttpPut) HttpPatch(org.apache.http.client.methods.HttpPatch) ParseException(org.apache.http.ParseException) IOException(java.io.IOException)

Example 18 with HttpContext

use of org.apache.http.protocol.HttpContext in project keycloak by keycloak.

the class CookieTest method testCookieValue.

private void testCookieValue(String cookieName) throws Exception {
    final String accountClientId = realmsResouce().realm("test").clients().findByClientId("account").get(0).getId();
    final String clientSecret = realmsResouce().realm("test").clients().get(accountClientId).getSecret().getValue();
    AuthorizationEndpointResponse codeResponse = oauth.clientId("account").redirectUri(accountPage.buildUri().toString()).doLogin("test-user@localhost", "password");
    OAuthClient.AccessTokenResponse accTokenResp = oauth.doAccessTokenRequest(codeResponse.getCode(), clientSecret);
    String accessToken = accTokenResp.getAccessToken();
    accountPage.navigateTo();
    accountPage.assertCurrent();
    try (CloseableHttpClient hc = OAuthClient.newCloseableHttpClient()) {
        BasicCookieStore cookieStore = new BasicCookieStore();
        BasicClientCookie cookie = new BasicClientCookie(cookieName, accessToken);
        cookie.setDomain("localhost");
        cookie.setPath("/");
        cookieStore.addCookie(cookie);
        HttpContext localContext = new BasicHttpContext();
        localContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);
        HttpGet get = new HttpGet(oauth.clientId("account").redirectUri(accountPage.buildUri().toString()).getLoginFormUrl());
        try (CloseableHttpResponse resp = hc.execute(get, localContext)) {
            final String pageContent = EntityUtils.toString(resp.getEntity());
            // Ensure that we did not get to the account page ...
            assertThat(pageContent, not(containsString("First name")));
            assertThat(pageContent, not(containsString("Last name")));
            // ... but were redirected to login page
            assertThat(pageContent, containsString("Sign In"));
            assertThat(pageContent, containsString("Forgot Password?"));
        }
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) AuthorizationEndpointResponse(org.keycloak.testsuite.util.OAuthClient.AuthorizationEndpointResponse) OAuthClient(org.keycloak.testsuite.util.OAuthClient) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) HttpGet(org.apache.http.client.methods.HttpGet) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) HttpContext(org.apache.http.protocol.HttpContext) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) BasicClientCookie(org.apache.http.impl.cookie.BasicClientCookie)

Example 19 with HttpContext

use of org.apache.http.protocol.HttpContext in project keycloak by keycloak.

the class CookieTest method testCookieValueLoggedOut.

@Test
public void testCookieValueLoggedOut() throws Exception {
    final String accountClientId = realmsResouce().realm("test").clients().findByClientId("account").get(0).getId();
    final String clientSecret = realmsResouce().realm("test").clients().get(accountClientId).getSecret().getValue();
    AuthorizationEndpointResponse codeResponse = oauth.clientId("account").redirectUri(accountPage.buildUri().toString()).doLogin("test-user@localhost", "password");
    OAuthClient.AccessTokenResponse accTokenResp = oauth.doAccessTokenRequest(codeResponse.getCode(), clientSecret);
    String accessToken = accTokenResp.getAccessToken();
    accountPage.navigateTo();
    accountPage.assertCurrent();
    accountPage.logOut();
    try (CloseableHttpClient hc = OAuthClient.newCloseableHttpClient()) {
        BasicCookieStore cookieStore = new BasicCookieStore();
        BasicClientCookie cookie = new BasicClientCookie(KEYCLOAK_IDENTITY_COOKIE, accessToken);
        cookie.setDomain("localhost");
        cookie.setPath("/");
        cookieStore.addCookie(cookie);
        HttpContext localContext = new BasicHttpContext();
        localContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);
        HttpGet get = new HttpGet(oauth.clientId("account").redirectUri(accountPage.buildUri().toString()).getLoginFormUrl());
        try (CloseableHttpResponse resp = hc.execute(get, localContext)) {
            final String pageContent = EntityUtils.toString(resp.getEntity());
            // Ensure that we did not get to the account page ...
            assertThat(pageContent, not(containsString("First name")));
            assertThat(pageContent, not(containsString("Last name")));
            // ... but were redirected to login page
            assertThat(pageContent, containsString("Sign In"));
            assertThat(pageContent, containsString("Forgot Password?"));
        }
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) AuthorizationEndpointResponse(org.keycloak.testsuite.util.OAuthClient.AuthorizationEndpointResponse) OAuthClient(org.keycloak.testsuite.util.OAuthClient) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) HttpGet(org.apache.http.client.methods.HttpGet) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) HttpContext(org.apache.http.protocol.HttpContext) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) BasicClientCookie(org.apache.http.impl.cookie.BasicClientCookie) Test(org.junit.Test) AbstractKeycloakTest(org.keycloak.testsuite.AbstractKeycloakTest)

Example 20 with HttpContext

use of org.apache.http.protocol.HttpContext in project coprhd-controller by CoprHD.

the class WinRMTarget method sendMessage.

public String sendMessage(String request) throws WinRMException {
    try {
        HttpPost post = new HttpPost(getUrl().toExternalForm());
        post.setEntity(new StringEntity(request, SOAP));
        HttpHost targetHost = new HttpHost(getHost(), getPort(), getProtocol());
        CloseableHttpClient client = getClient();
        HttpContext context = getContext();
        HttpResponse response = client.execute(targetHost, post, context);
        String text = EntityUtils.toString(response.getEntity());
        if (response.getStatusLine().getStatusCode() != 200) {
            handleError(response, text);
        }
        return text;
    } catch (WinRMException e) {
        throw e;
    } catch (Exception e) {
        throw new WinRMException(e);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpHost(org.apache.http.HttpHost) HttpContext(org.apache.http.protocol.HttpContext) HttpResponse(org.apache.http.HttpResponse) HttpException(org.apache.http.HttpException) MalformedURLException(java.net.MalformedURLException)

Aggregations

HttpContext (org.apache.http.protocol.HttpContext)187 HttpResponse (org.apache.http.HttpResponse)81 IOException (java.io.IOException)74 BasicHttpContext (org.apache.http.protocol.BasicHttpContext)59 HttpRequest (org.apache.http.HttpRequest)57 HttpException (org.apache.http.HttpException)34 HttpGet (org.apache.http.client.methods.HttpGet)31 Test (org.junit.Test)30 HttpHost (org.apache.http.HttpHost)28 HttpPost (org.apache.http.client.methods.HttpPost)28 StringEntity (org.apache.http.entity.StringEntity)23 ArrayList (java.util.ArrayList)22 Header (org.apache.http.Header)22 MessageContext (org.apache.axis2.context.MessageContext)19 NameValuePair (org.apache.http.NameValuePair)18 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)18 ProtocolException (org.apache.http.ProtocolException)17 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)17 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)17 URI (java.net.URI)16