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;
}
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);
}
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;
}
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);
}
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;
}
Aggregations