Search in sources :

Example 16 with BasicHeader

use of org.apache.http.message.BasicHeader in project gerrit by GerritCodeReview.

the class CorsIT method check.

private RestResponse check(String url, boolean accept, String origin) throws Exception {
    Header hdr = new BasicHeader(ORIGIN, origin);
    RestResponse r = adminRestSession.getWithHeader(url, hdr);
    r.assertOK();
    checkCors(r, accept, origin);
    return r;
}
Also used : Header(org.apache.http.Header) BasicHeader(org.apache.http.message.BasicHeader) RestResponse(com.google.gerrit.acceptance.RestResponse) BasicHeader(org.apache.http.message.BasicHeader)

Example 17 with BasicHeader

use of org.apache.http.message.BasicHeader in project gerrit by GerritCodeReview.

the class ChangeRebuilderIT method rebuildEntitiesCreatedByImpersonation.

@Test
public void rebuildEntitiesCreatedByImpersonation() throws Exception {
    PushOneCommit.Result r = createChange();
    Change.Id id = r.getPatchSetId().getParentKey();
    PatchSet.Id psId = new PatchSet.Id(id, 1);
    String prefix = "/changes/" + id + "/revisions/current/";
    // For each of the entities that have a real user field, create one entity
    // without impersonation and one with.
    CommentInput ci = new CommentInput();
    ci.path = Patch.COMMIT_MSG;
    ci.side = Side.REVISION;
    ci.line = 1;
    ci.message = "comment without impersonation";
    ReviewInput ri = new ReviewInput();
    ri.label("Code-Review", -1);
    ri.message = "message without impersonation";
    ri.drafts = DraftHandling.KEEP;
    ri.comments = ImmutableMap.of(ci.path, ImmutableList.of(ci));
    userRestSession.post(prefix + "review", ri).assertOK();
    DraftInput di = new DraftInput();
    di.path = Patch.COMMIT_MSG;
    di.side = Side.REVISION;
    di.line = 1;
    di.message = "draft without impersonation";
    userRestSession.put(prefix + "drafts", di).assertCreated();
    allowRunAs();
    try {
        Header runAs = new BasicHeader("X-Gerrit-RunAs", user.id.toString());
        ci.message = "comment with impersonation";
        ri.message = "message with impersonation";
        ri.label("Code-Review", 1);
        adminRestSession.postWithHeader(prefix + "review", ri, runAs).assertOK();
        di.message = "draft with impersonation";
        adminRestSession.putWithHeader(prefix + "drafts", runAs, di).assertCreated();
    } finally {
        removeRunAs();
    }
    List<ChangeMessage> msgs = Ordering.natural().onResultOf(ChangeMessage::getWrittenOn).sortedCopy(db.changeMessages().byChange(id));
    assertThat(msgs).hasSize(3);
    assertThat(msgs.get(1).getMessage()).endsWith("message without impersonation");
    assertThat(msgs.get(1).getAuthor()).isEqualTo(user.id);
    assertThat(msgs.get(1).getRealAuthor()).isEqualTo(user.id);
    assertThat(msgs.get(2).getMessage()).endsWith("message with impersonation");
    assertThat(msgs.get(2).getAuthor()).isEqualTo(user.id);
    assertThat(msgs.get(2).getRealAuthor()).isEqualTo(admin.id);
    List<PatchSetApproval> psas = db.patchSetApprovals().byChange(id).toList();
    assertThat(psas).hasSize(1);
    assertThat(psas.get(0).getLabel()).isEqualTo("Code-Review");
    assertThat(psas.get(0).getValue()).isEqualTo(1);
    assertThat(psas.get(0).getAccountId()).isEqualTo(user.id);
    assertThat(psas.get(0).getRealAccountId()).isEqualTo(admin.id);
    Ordering<PatchLineComment> commentOrder = Ordering.natural().onResultOf(PatchLineComment::getWrittenOn);
    List<PatchLineComment> drafts = commentOrder.sortedCopy(db.patchComments().draftByPatchSetAuthor(psId, user.id));
    assertThat(drafts).hasSize(2);
    assertThat(drafts.get(0).getMessage()).isEqualTo("draft without impersonation");
    assertThat(drafts.get(0).getAuthor()).isEqualTo(user.id);
    assertThat(drafts.get(0).getRealAuthor()).isEqualTo(user.id);
    assertThat(drafts.get(1).getMessage()).isEqualTo("draft with impersonation");
    assertThat(drafts.get(1).getAuthor()).isEqualTo(user.id);
    assertThat(drafts.get(1).getRealAuthor()).isEqualTo(admin.id);
    List<PatchLineComment> pub = commentOrder.sortedCopy(db.patchComments().publishedByPatchSet(psId));
    assertThat(pub).hasSize(2);
    assertThat(pub.get(0).getMessage()).isEqualTo("comment without impersonation");
    assertThat(pub.get(0).getAuthor()).isEqualTo(user.id);
    assertThat(pub.get(0).getRealAuthor()).isEqualTo(user.id);
    assertThat(pub.get(1).getMessage()).isEqualTo("comment with impersonation");
    assertThat(pub.get(1).getAuthor()).isEqualTo(user.id);
    assertThat(pub.get(1).getRealAuthor()).isEqualTo(admin.id);
}
Also used : PatchSet(com.google.gerrit.reviewdb.client.PatchSet) Change(com.google.gerrit.reviewdb.client.Change) ReviewInput(com.google.gerrit.extensions.api.changes.ReviewInput) PatchSetApproval(com.google.gerrit.reviewdb.client.PatchSetApproval) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) PatchLineComment(com.google.gerrit.reviewdb.client.PatchLineComment) Header(org.apache.http.Header) BasicHeader(org.apache.http.message.BasicHeader) CommentInput(com.google.gerrit.extensions.api.changes.ReviewInput.CommentInput) ChangeMessage(com.google.gerrit.reviewdb.client.ChangeMessage) DraftInput(com.google.gerrit.extensions.api.changes.DraftInput) ObjectId(org.eclipse.jgit.lib.ObjectId) BasicHeader(org.apache.http.message.BasicHeader) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Example 18 with BasicHeader

use of org.apache.http.message.BasicHeader in project lucene-solr by apache.

the class BasicAuthPlugin method doAuthenticate.

@Override
public boolean doAuthenticate(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws Exception {
    HttpServletRequest request = (HttpServletRequest) servletRequest;
    HttpServletResponse response = (HttpServletResponse) servletResponse;
    String authHeader = request.getHeader("Authorization");
    if (authHeader != null) {
        BasicAuthPlugin.authHeader.set(new BasicHeader("Authorization", authHeader));
        StringTokenizer st = new StringTokenizer(authHeader);
        if (st.hasMoreTokens()) {
            String basic = st.nextToken();
            if (basic.equalsIgnoreCase("Basic")) {
                try {
                    String credentials = new String(Base64.decodeBase64(st.nextToken()), "UTF-8");
                    int p = credentials.indexOf(":");
                    if (p != -1) {
                        final String username = credentials.substring(0, p).trim();
                        String pwd = credentials.substring(p + 1).trim();
                        if (!authenticate(username, pwd)) {
                            log.debug("Bad auth credentials supplied in Authorization header");
                            authenticationFailure(response, "Bad credentials");
                        } else {
                            HttpServletRequestWrapper wrapper = new HttpServletRequestWrapper(request) {

                                @Override
                                public Principal getUserPrincipal() {
                                    return new BasicUserPrincipal(username);
                                }
                            };
                            filterChain.doFilter(wrapper, response);
                            return true;
                        }
                    } else {
                        authenticationFailure(response, "Invalid authentication token");
                    }
                } catch (UnsupportedEncodingException e) {
                    throw new Error("Couldn't retrieve authentication", e);
                }
            }
        }
    } else {
        if (blockUnknown) {
            authenticationFailure(response, "require authentication");
        } else {
            request.setAttribute(AuthenticationPlugin.class.getName(), authenticationProvider.getPromptHeaders());
            filterChain.doFilter(request, response);
            return true;
        }
    }
    return false;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) StringTokenizer(java.util.StringTokenizer) BasicUserPrincipal(org.apache.http.auth.BasicUserPrincipal) HttpServletRequestWrapper(javax.servlet.http.HttpServletRequestWrapper) HttpServletResponse(javax.servlet.http.HttpServletResponse) UnsupportedEncodingException(java.io.UnsupportedEncodingException) BasicHeader(org.apache.http.message.BasicHeader)

Example 19 with BasicHeader

use of org.apache.http.message.BasicHeader in project platform_external_apache-http by android.

the class AbstractHttpEntity method setContentType.

/**
     * Specifies the Content-Type header, as a string.
     * The default implementation calls
     * {@link #setContentType(Header) setContentType(Header)}.
     *
     * @param ctString     the new Content-Type header, or
     *                     <code>null</code> to unset
     */
public void setContentType(final String ctString) {
    Header h = null;
    if (ctString != null) {
        h = new BasicHeader(HTTP.CONTENT_TYPE, ctString);
    }
    setContentType(h);
}
Also used : BasicHeader(org.apache.http.message.BasicHeader) Header(org.apache.http.Header) BasicHeader(org.apache.http.message.BasicHeader)

Example 20 with BasicHeader

use of org.apache.http.message.BasicHeader in project lucene-solr by apache.

the class TestConfigSetsAPI method postDataAndGetResponse.

public static Map postDataAndGetResponse(CloudSolrClient cloudClient, String uri, ByteBuffer bytarr, String username, String password) throws IOException {
    HttpPost httpPost = null;
    HttpEntity entity;
    String response = null;
    Map m = null;
    try {
        httpPost = new HttpPost(uri);
        if (username != null) {
            String userPass = username + ":" + password;
            String encoded = Base64.byteArrayToBase64(userPass.getBytes(UTF_8));
            BasicHeader header = new BasicHeader("Authorization", "Basic " + encoded);
            httpPost.setHeader(header);
        }
        httpPost.setHeader("Content-Type", "application/octet-stream");
        httpPost.setEntity(new ByteArrayEntity(bytarr.array(), bytarr.arrayOffset(), bytarr.limit()));
        entity = cloudClient.getLbClient().getHttpClient().execute(httpPost).getEntity();
        try {
            response = EntityUtils.toString(entity, StandardCharsets.UTF_8);
            m = (Map) ObjectBuilder.getVal(new JSONParser(new StringReader(response)));
        } catch (JSONParser.ParseException e) {
            fail(e.getMessage());
        }
    } finally {
        httpPost.releaseConnection();
    }
    return m;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpEntity(org.apache.http.HttpEntity) ByteArrayEntity(org.apache.http.entity.ByteArrayEntity) StringReader(java.io.StringReader) JSONParser(org.noggit.JSONParser) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) 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