use of org.apache.commons.httpclient.methods.StringRequestEntity in project alfresco-remote-api by Alfresco.
the class PublicApiHttpClient method put.
public HttpResponse put(final Class<?> c, final RequestContext rq, final Object entityId, final Object relationshipEntityId, final String body) throws IOException {
RestApiEndpoint endpoint = new RestApiEndpoint(c, rq.getNetworkId(), entityId, relationshipEntityId, null);
String url = endpoint.getUrl();
PutMethod req = new PutMethod(url);
if (body != null) {
StringRequestEntity requestEntity = new StringRequestEntity(body, "application/json", "UTF-8");
req.setRequestEntity(requestEntity);
}
return submitRequest(req, rq);
}
use of org.apache.commons.httpclient.methods.StringRequestEntity in project alfresco-remote-api by Alfresco.
the class PublicApiHttpClient method post.
public HttpResponse post(final RequestContext rq, final String urlSuffix, String body) throws IOException {
RestApiEndpoint endpoint = new RestApiEndpoint(rq.getNetworkId(), urlSuffix, null);
String url = endpoint.getUrl();
PostMethod req = new PostMethod(url.toString());
if (body != null) {
StringRequestEntity requestEntity = new StringRequestEntity(body, "application/json", "UTF-8");
req.setRequestEntity(requestEntity);
}
return submitRequest(req, rq);
}
use of org.apache.commons.httpclient.methods.StringRequestEntity in project alfresco-repository by Alfresco.
the class AbstractSolrQueryHTTPClient method postQuery.
protected JSONObject postQuery(HttpClient httpClient, String url, JSONObject body) throws UnsupportedEncodingException, IOException, HttpException, URIException, JSONException {
PostMethod post = new PostMethod(url);
if (body.toString().length() > DEFAULT_SAVEPOST_BUFFER) {
post.getParams().setBooleanParameter(HttpMethodParams.USE_EXPECT_CONTINUE, true);
}
StringRequestEntity requestEntity = new StringRequestEntity(body.toString(), "application/json", "UTF-8");
post.setRequestEntity(requestEntity);
try {
httpClient.executeMethod(post);
if (post.getStatusCode() == HttpStatus.SC_MOVED_PERMANENTLY || post.getStatusCode() == HttpStatus.SC_MOVED_TEMPORARILY) {
Header locationHeader = post.getResponseHeader("location");
if (locationHeader != null) {
String redirectLocation = locationHeader.getValue();
post.setURI(new URI(redirectLocation, true));
httpClient.executeMethod(post);
}
}
if (post.getStatusCode() != HttpServletResponse.SC_OK) {
throw new LuceneQueryParserException("Request failed " + post.getStatusCode() + " " + url.toString());
}
Reader reader = new BufferedReader(new InputStreamReader(post.getResponseBodyAsStream(), post.getResponseCharSet()));
// TODO - replace with streaming-based solution e.g. SimpleJSON ContentHandler
JSONObject json = new JSONObject(new JSONTokener(reader));
return json;
} finally {
post.releaseConnection();
}
}
use of org.apache.commons.httpclient.methods.StringRequestEntity in project phabricator-jenkins-plugin by uber.
the class UberallsClient method recordCoverage.
public boolean recordCoverage(String sha, CodeCoverageMetrics codeCoverageMetrics) {
if (codeCoverageMetrics != null) {
JSONObject params = new JSONObject();
params.put("sha", sha);
params.put("branch", branch);
params.put("repository", repository);
params.put(PACKAGE_COVERAGE_KEY, codeCoverageMetrics.getPackageCoveragePercent());
params.put(FILES_COVERAGE_KEY, codeCoverageMetrics.getFilesCoveragePercent());
params.put(CLASSES_COVERAGE_KEY, codeCoverageMetrics.getClassesCoveragePercent());
params.put(METHOD_COVERAGE_KEY, codeCoverageMetrics.getMethodCoveragePercent());
params.put(LINE_COVERAGE_KEY, codeCoverageMetrics.getLineCoveragePercent());
params.put(CONDITIONAL_COVERAGE_KEY, codeCoverageMetrics.getConditionalCoveragePercent());
params.put(LINES_COVERED_KEY, codeCoverageMetrics.getLinesCovered());
params.put(LINES_TESTED_KEY, codeCoverageMetrics.getLinesTested());
try {
HttpClient client = getClient();
PostMethod request = new PostMethod(getBuilder().build().toString());
request.addRequestHeader("Content-Type", "application/json");
StringRequestEntity requestEntity = new StringRequestEntity(params.toString(), ContentType.APPLICATION_JSON.toString(), "UTF-8");
request.setRequestEntity(requestEntity);
int statusCode = client.executeMethod(request);
if (statusCode != HttpStatus.SC_OK) {
logger.info(TAG, "Call failed: " + request.getStatusLine());
return false;
}
return true;
} catch (URISyntaxException e) {
e.printStackTrace(logger.getStream());
} catch (HttpResponseException e) {
// e.g. 404, pass
logger.info(TAG, "HTTP Response error recording metrics: " + e);
} catch (ClientProtocolException e) {
e.printStackTrace(logger.getStream());
} catch (IOException e) {
e.printStackTrace(logger.getStream());
}
}
return false;
}
use of org.apache.commons.httpclient.methods.StringRequestEntity in project zeppelin by apache.
the class ZeppelinHubRealm method authenticateUser.
/**
* Send to ZeppelinHub a login request based on the request body which is a JSON that contains 2
* fields "login" and "password".
*
* @param requestBody JSON string of ZeppelinHub payload.
* @return Account object with login, name (if set in ZeppelinHub), and mail.
* @throws AuthenticationException if fail to login.
*/
protected User authenticateUser(String requestBody) {
PutMethod put = new PutMethod(Joiner.on("/").join(zeppelinhubUrl, USER_LOGIN_API_ENDPOINT));
String responseBody = StringUtils.EMPTY;
String userSession = StringUtils.EMPTY;
try {
put.setRequestEntity(new StringRequestEntity(requestBody, JSON_CONTENT_TYPE, UTF_8_ENCODING));
int statusCode = httpClient.executeMethod(put);
if (statusCode != HttpStatus.SC_OK) {
LOG.error("Cannot login user, HTTP status code is {} instead on 200 (OK)", statusCode);
put.releaseConnection();
throw new AuthenticationException("Couldnt login to ZeppelinHub. " + "Login or password incorrect");
}
responseBody = put.getResponseBodyAsString();
userSession = put.getResponseHeader(USER_SESSION_HEADER).getValue();
put.releaseConnection();
} catch (IOException e) {
LOG.error("Cannot login user", e);
throw new AuthenticationException(e.getMessage());
}
User account = null;
try {
account = gson.fromJson(responseBody, User.class);
} catch (JsonParseException e) {
LOG.error("Cannot deserialize ZeppelinHub response to User instance", e);
throw new AuthenticationException("Cannot login to ZeppelinHub");
}
// Add ZeppelinHub user_session token this singleton map, this will help ZeppelinHubRepo
// to get specific information about the current user.
UserSessionContainer.instance.setSession(account.login, userSession);
/* TODO(khalid): add proper roles and add listener */
HashSet<String> userAndRoles = new HashSet<String>();
userAndRoles.add(account.login);
ZeppelinServer.notebookWsServer.broadcastReloadedNoteList(new org.apache.zeppelin.user.AuthenticationInfo(account.login), userAndRoles);
return account;
}
Aggregations