use of org.apache.commons.httpclient.methods.StringRequestEntity in project SSM by Intel-bigdata.
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");
}
onLoginSuccess(account.login, userSession);
return account;
}
use of org.apache.commons.httpclient.methods.StringRequestEntity in project android by nextcloud.
the class InputStreamBinder method buildMethod.
private HttpMethodBase buildMethod(NextcloudRequest request, Uri baseUri, InputStream requestBodyInputStream) throws IOException {
String requestUrl = baseUri + request.getUrl();
HttpMethodBase method;
switch(request.getMethod()) {
case "GET":
method = new GetMethod(requestUrl);
break;
case "POST":
method = new PostMethod(requestUrl);
if (requestBodyInputStream != null) {
RequestEntity requestEntity = new InputStreamRequestEntity(requestBodyInputStream);
((PostMethod) method).setRequestEntity(requestEntity);
} else if (request.getRequestBody() != null) {
StringRequestEntity requestEntity = new StringRequestEntity(request.getRequestBody(), CONTENT_TYPE_APPLICATION_JSON, CHARSET_UTF8);
((PostMethod) method).setRequestEntity(requestEntity);
}
break;
case "PATCH":
method = new PatchMethod(requestUrl);
if (requestBodyInputStream != null) {
RequestEntity requestEntity = new InputStreamRequestEntity(requestBodyInputStream);
((PatchMethod) method).setRequestEntity(requestEntity);
} else if (request.getRequestBody() != null) {
StringRequestEntity requestEntity = new StringRequestEntity(request.getRequestBody(), CONTENT_TYPE_APPLICATION_JSON, CHARSET_UTF8);
((PatchMethod) method).setRequestEntity(requestEntity);
}
break;
case "PUT":
method = new PutMethod(requestUrl);
if (requestBodyInputStream != null) {
RequestEntity requestEntity = new InputStreamRequestEntity(requestBodyInputStream);
((PutMethod) method).setRequestEntity(requestEntity);
} else if (request.getRequestBody() != null) {
StringRequestEntity requestEntity = new StringRequestEntity(request.getRequestBody(), CONTENT_TYPE_APPLICATION_JSON, CHARSET_UTF8);
((PutMethod) method).setRequestEntity(requestEntity);
}
break;
case "DELETE":
method = new DeleteMethod(requestUrl);
break;
case "PROPFIND":
method = new NCPropFindMethod(requestUrl, DavConstants.PROPFIND_ALL_PROP, DavConstants.DEPTH_1);
if (request.getRequestBody() != null) {
// text/xml; charset=UTF-8 is taken from XmlRequestEntity... Should be application/xml
StringRequestEntity requestEntity = new StringRequestEntity(request.getRequestBody(), "text/xml; charset=UTF-8", CHARSET_UTF8);
((PropFindMethod) method).setRequestEntity(requestEntity);
}
break;
case "MKCOL":
method = new MkColMethod(requestUrl);
break;
case "HEAD":
method = new HeadMethod(requestUrl);
break;
default:
throw new UnsupportedOperationException(EXCEPTION_UNSUPPORTED_METHOD);
}
return method;
}
use of org.apache.commons.httpclient.methods.StringRequestEntity in project alfresco-remote-api by Alfresco.
the class HttpResponse method toString.
public String toString() {
StringBuilder sb = new StringBuilder();
String requestType = null;
RequestEntity requestEntity = null;
if (method instanceof GetMethod) {
requestType = "GET";
} else if (method instanceof PutMethod) {
requestType = "PUT";
requestEntity = ((PutMethod) method).getRequestEntity();
} else if (method instanceof PostMethod) {
requestType = "POST";
requestEntity = ((PostMethod) method).getRequestEntity();
} else if (method instanceof DeleteMethod) {
requestType = "DELETE";
}
try {
sb.append(requestType).append(" request ").append(method.getURI()).append("\n");
} catch (URIException e) {
}
if (requestEntity != null) {
sb.append("\nRequest body: ");
if (requestEntity instanceof StringRequestEntity) {
sb.append(((StringRequestEntity) requestEntity).getContent());
} else if (requestEntity instanceof ByteArrayRequestEntity) {
sb.append(" << ").append(((ByteArrayRequestEntity) requestEntity).getContent().length).append(" bytes >>");
}
sb.append("\n");
}
sb.append("user ").append(user).append("\n");
sb.append("returned ").append(method.getStatusCode()).append(" and took ").append(time).append("ms").append("\n");
String contentType = null;
Header hdr = method.getResponseHeader("Content-Type");
if (hdr != null) {
contentType = hdr.getValue();
}
sb.append("Response content type: ").append(contentType).append("\n");
if (contentType != null) {
sb.append("\nResponse body: ");
if (contentType.startsWith("text/plain") || contentType.startsWith("application/json")) {
sb.append(getResponse());
sb.append("\n");
} else if (getResponseAsBytes() != null) {
sb.append(" << ").append(getResponseAsBytes().length).append(" bytes >>");
sb.append("\n");
}
}
return sb.toString();
}
use of org.apache.commons.httpclient.methods.StringRequestEntity in project alfresco-remote-api by Alfresco.
the class PublicApiHttpClient method put.
public HttpResponse put(final RequestContext rq, Binding cmisBinding, String version, String cmisOperation, final String body) throws IOException {
RestApiEndpoint endpoint = new RestApiEndpoint(rq.getNetworkId(), cmisBinding, version, cmisOperation, null);
String url = endpoint.getUrl();
PutMethod req = new PutMethod(url.toString());
if (body != null) {
StringRequestEntity requestEntity = null;
if (cmisBinding.equals(Binding.atom)) {
requestEntity = new StringRequestEntity(body, "text/xml", "UTF-8");
} else if (cmisBinding.equals(Binding.browser)) {
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 put.
public HttpResponse put(final RequestContext rq, final String scope, final int version, final String entityCollectionName, final Object entityId, final String relationCollectionName, final Object relationshipEntityId, final String body, Map<String, String> params) throws IOException {
RestApiEndpoint endpoint = new RestApiEndpoint(rq.getNetworkId(), scope, version, entityCollectionName, entityId, relationCollectionName, relationshipEntityId, params);
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);
}
Aggregations