Search in sources :

Example 11 with BasicHeader

use of org.apache.http.message.BasicHeader in project intellij-community by JetBrains.

the class EduStepicAuthorizedClient method initializeClient.

@Nullable
private static CloseableHttpClient initializeClient(@NotNull final StepicUser stepicUser) {
    final List<BasicHeader> headers = new ArrayList<>();
    final String accessToken = stepicUser.getAccessToken();
    if (accessToken != null && !accessToken.isEmpty()) {
        headers.add(new BasicHeader("Authorization", "Bearer " + accessToken));
        headers.add(new BasicHeader("Content-type", EduStepicNames.CONTENT_TYPE_APP_JSON));
        return getBuilder().setDefaultHeaders(headers).build();
    }
    return null;
}
Also used : ArrayList(java.util.ArrayList) BasicHeader(org.apache.http.message.BasicHeader) Nullable(org.jetbrains.annotations.Nullable)

Example 12 with BasicHeader

use of org.apache.http.message.BasicHeader in project Activiti by Activiti.

the class BaseSpringRestTestCase method internalExecuteRequest.

protected CloseableHttpResponse internalExecuteRequest(HttpUriRequest request, int expectedStatusCode, boolean addJsonContentType) {
    CloseableHttpResponse response = null;
    try {
        if (addJsonContentType && request.getFirstHeader(HttpHeaders.CONTENT_TYPE) == null) {
            // Revert to default content-type 
            request.addHeader(new BasicHeader(HttpHeaders.CONTENT_TYPE, "application/json"));
        }
        response = client.execute(request);
        Assert.assertNotNull(response.getStatusLine());
        Assert.assertEquals(expectedStatusCode, response.getStatusLine().getStatusCode());
        httpResponses.add(response);
        return response;
    } catch (ClientProtocolException e) {
        Assert.fail(e.getMessage());
    } catch (IOException e) {
        Assert.fail(e.getMessage());
    }
    return null;
}
Also used : CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) IOException(java.io.IOException) BasicHeader(org.apache.http.message.BasicHeader) ClientProtocolException(org.apache.http.client.ClientProtocolException)

Example 13 with BasicHeader

use of org.apache.http.message.BasicHeader in project Activiti by Activiti.

the class DeploymentResourceResourceTest method testGetDeploymentResourceUnexistingResource.

/**
    * Test getting an unexisting resource for an existing deployment.
    * GET repository/deployments/{deploymentId}/resources/{resourceId}
    */
public void testGetDeploymentResourceUnexistingResource() throws Exception {
    try {
        Deployment deployment = repositoryService.createDeployment().name("Deployment 1").addInputStream("test.txt", new ByteArrayInputStream("Test content".getBytes())).deploy();
        HttpGet httpGet = new HttpGet(SERVER_URL_PREFIX + RestUrls.createRelativeResourceUrl(RestUrls.URL_DEPLOYMENT_RESOURCE, deployment.getId(), "unexisting-resource.png"));
        httpGet.addHeader(new BasicHeader(HttpHeaders.ACCEPT, "image/png,application/json"));
        closeResponse(executeRequest(httpGet, HttpStatus.SC_NOT_FOUND));
    } finally {
        // Always cleanup any created deployments, even if the test failed
        List<Deployment> deployments = repositoryService.createDeploymentQuery().list();
        for (Deployment deployment : deployments) {
            repositoryService.deleteDeployment(deployment.getId(), true);
        }
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) HttpGet(org.apache.http.client.methods.HttpGet) Deployment(org.activiti.engine.repository.Deployment) BasicHeader(org.apache.http.message.BasicHeader)

Example 14 with BasicHeader

use of org.apache.http.message.BasicHeader in project Activiti by Activiti.

the class DeploymentResourceResourceTest method testGetDeploymentResourceContent.

/**
    * Test getting a deployment resource content.
    * GET repository/deployments/{deploymentId}/resources/{resourceId}
    */
public void testGetDeploymentResourceContent() throws Exception {
    try {
        Deployment deployment = repositoryService.createDeployment().name("Deployment 1").addInputStream("test.txt", new ByteArrayInputStream("Test content".getBytes())).deploy();
        HttpGet httpGet = new HttpGet(SERVER_URL_PREFIX + RestUrls.createRelativeResourceUrl(RestUrls.URL_DEPLOYMENT_RESOURCE_CONTENT, deployment.getId(), "test.txt"));
        httpGet.addHeader(new BasicHeader(HttpHeaders.ACCEPT, "text/plain"));
        CloseableHttpResponse response = executeRequest(httpGet, HttpStatus.SC_OK);
        String responseAsString = IOUtils.toString(response.getEntity().getContent());
        closeResponse(response);
        assertNotNull(responseAsString);
        assertEquals("Test content", responseAsString);
    } finally {
        // Always cleanup any created deployments, even if the test failed
        List<Deployment> deployments = repositoryService.createDeploymentQuery().list();
        for (Deployment deployment : deployments) {
            repositoryService.deleteDeployment(deployment.getId(), true);
        }
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) Deployment(org.activiti.engine.repository.Deployment) BasicHeader(org.apache.http.message.BasicHeader)

Example 15 with BasicHeader

use of org.apache.http.message.BasicHeader in project wildfly by wildfly.

the class JBossNegotiateScheme method authenticate.

/**
     * Produces Negotiate authorization Header based on token created by processChallenge.
     *
     * @param credentials Never used be the Negotiate scheme but must be provided to satisfy common-httpclient API. Credentials
     *        from JAAS will be used instead.
     * @param request The request being authenticated
     *
     * @throws AuthenticationException if authorization string cannot be generated due to an authentication failure
     *
     * @return an Negotiate authorization Header
     */
@Override
public Header authenticate(final Credentials credentials, final HttpRequest request, final HttpContext context) throws AuthenticationException {
    if (request == null) {
        throw new IllegalArgumentException("HTTP request may not be null");
    }
    if (state == State.TOKEN_GENERATED) {
        // hack for auto redirects
        return new BasicHeader("X-dummy", "Token already generated");
    }
    if (state != State.CHALLENGE_RECEIVED) {
        throw new IllegalStateException("Negotiation authentication process has not been initiated");
    }
    try {
        String key = null;
        if (isProxy()) {
            key = ExecutionContext.HTTP_PROXY_HOST;
        } else {
            key = HttpCoreContext.HTTP_TARGET_HOST;
        }
        HttpHost host = (HttpHost) context.getAttribute(key);
        if (host == null) {
            throw new AuthenticationException("Authentication host is not set " + "in the execution context");
        }
        String authServer;
        if (!this.stripPort && host.getPort() > 0) {
            authServer = host.toHostString();
        } else {
            authServer = host.getHostName();
        }
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("init " + authServer);
        }
        final Oid negotiationOid = new Oid(SPNEGO_OID);
        final GSSManager manager = GSSManager.getInstance();
        final GSSName serverName = manager.createName("HTTP@" + authServer, GSSName.NT_HOSTBASED_SERVICE);
        final GSSContext gssContext = manager.createContext(serverName.canonicalize(negotiationOid), negotiationOid, null, DEFAULT_LIFETIME);
        gssContext.requestMutualAuth(true);
        gssContext.requestCredDeleg(true);
        if (token == null) {
            token = new byte[0];
        }
        token = gssContext.initSecContext(token, 0, token.length);
        if (token == null) {
            state = State.FAILED;
            throw new AuthenticationException("GSS security context initialization failed");
        }
        state = State.TOKEN_GENERATED;
        String tokenstr = new String(base64codec.encode(token));
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Sending response '" + tokenstr + "' back to the auth server");
        }
        CharArrayBuffer buffer = new CharArrayBuffer(32);
        if (isProxy()) {
            buffer.append(AUTH.PROXY_AUTH_RESP);
        } else {
            buffer.append(AUTH.WWW_AUTH_RESP);
        }
        buffer.append(": Negotiate ");
        buffer.append(tokenstr);
        return new BufferedHeader(buffer);
    } catch (GSSException gsse) {
        state = State.FAILED;
        if (gsse.getMajor() == GSSException.DEFECTIVE_CREDENTIAL || gsse.getMajor() == GSSException.CREDENTIALS_EXPIRED)
            throw new InvalidCredentialsException(gsse.getMessage(), gsse);
        if (gsse.getMajor() == GSSException.NO_CRED)
            throw new InvalidCredentialsException(gsse.getMessage(), gsse);
        if (gsse.getMajor() == GSSException.DEFECTIVE_TOKEN || gsse.getMajor() == GSSException.DUPLICATE_TOKEN || gsse.getMajor() == GSSException.OLD_TOKEN)
            throw new AuthenticationException(gsse.getMessage(), gsse);
        // other error
        throw new AuthenticationException(gsse.getMessage());
    }
}
Also used : GSSName(org.ietf.jgss.GSSName) AuthenticationException(org.apache.http.auth.AuthenticationException) BufferedHeader(org.apache.http.message.BufferedHeader) CharArrayBuffer(org.apache.http.util.CharArrayBuffer) Oid(org.ietf.jgss.Oid) GSSException(org.ietf.jgss.GSSException) InvalidCredentialsException(org.apache.http.auth.InvalidCredentialsException) HttpHost(org.apache.http.HttpHost) GSSManager(org.ietf.jgss.GSSManager) GSSContext(org.ietf.jgss.GSSContext) BasicHeader(org.apache.http.message.BasicHeader)

Aggregations

BasicHeader (org.apache.http.message.BasicHeader)233 Header (org.apache.http.Header)120 IOException (java.io.IOException)54 HttpResponse (org.apache.http.HttpResponse)50 Test (org.junit.Test)50 StringEntity (org.apache.http.entity.StringEntity)36 List (java.util.List)25 HashMap (java.util.HashMap)24 URISyntaxException (java.net.URISyntaxException)23 HttpGet (org.apache.http.client.methods.HttpGet)22 StatusLine (org.apache.http.StatusLine)20 HttpPost (org.apache.http.client.methods.HttpPost)20 BasicStatusLine (org.apache.http.message.BasicStatusLine)19 RestResponse (com.google.gerrit.acceptance.RestResponse)18 ArrayList (java.util.ArrayList)18 ProtocolVersion (org.apache.http.ProtocolVersion)18 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)17 File (java.io.File)17 HttpEntity (org.apache.http.HttpEntity)17 BasicHttpResponse (org.apache.http.message.BasicHttpResponse)17