Search in sources :

Example 51 with StringRequestEntity

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;
}
Also used : StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) AuthenticationException(org.apache.shiro.authc.AuthenticationException) PutMethod(org.apache.commons.httpclient.methods.PutMethod) IOException(java.io.IOException) JsonParseException(com.google.gson.JsonParseException)

Example 52 with StringRequestEntity

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;
}
Also used : InputStreamRequestEntity(org.apache.commons.httpclient.methods.InputStreamRequestEntity) StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) DeleteMethod(org.apache.commons.httpclient.methods.DeleteMethod) PropFindMethod(org.apache.jackrabbit.webdav.client.methods.PropFindMethod) HttpMethodBase(org.apache.commons.httpclient.HttpMethodBase) PostMethod(org.apache.commons.httpclient.methods.PostMethod) MkColMethod(org.apache.jackrabbit.webdav.client.methods.MkColMethod) HeadMethod(org.apache.commons.httpclient.methods.HeadMethod) GetMethod(org.apache.commons.httpclient.methods.GetMethod) PutMethod(org.apache.commons.httpclient.methods.PutMethod) RequestEntity(org.apache.commons.httpclient.methods.RequestEntity) InputStreamRequestEntity(org.apache.commons.httpclient.methods.InputStreamRequestEntity) StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity)

Example 53 with StringRequestEntity

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();
}
Also used : DeleteMethod(org.apache.commons.httpclient.methods.DeleteMethod) StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) URIException(org.apache.commons.httpclient.URIException) Header(org.apache.commons.httpclient.Header) PostMethod(org.apache.commons.httpclient.methods.PostMethod) GetMethod(org.apache.commons.httpclient.methods.GetMethod) PutMethod(org.apache.commons.httpclient.methods.PutMethod) ByteArrayRequestEntity(org.apache.commons.httpclient.methods.ByteArrayRequestEntity) StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) RequestEntity(org.apache.commons.httpclient.methods.RequestEntity) ByteArrayRequestEntity(org.apache.commons.httpclient.methods.ByteArrayRequestEntity)

Example 54 with StringRequestEntity

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);
}
Also used : StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) PutMethod(org.apache.commons.httpclient.methods.PutMethod)

Example 55 with StringRequestEntity

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);
}
Also used : StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) PutMethod(org.apache.commons.httpclient.methods.PutMethod)

Aggregations

StringRequestEntity (org.apache.commons.httpclient.methods.StringRequestEntity)102 PostMethod (org.apache.commons.httpclient.methods.PostMethod)63 HttpClient (org.apache.commons.httpclient.HttpClient)33 Test (org.junit.Test)23 PutMethod (org.apache.commons.httpclient.methods.PutMethod)19 RequestEntity (org.apache.commons.httpclient.methods.RequestEntity)18 IOException (java.io.IOException)15 UnsupportedEncodingException (java.io.UnsupportedEncodingException)13 AuthRequestHandler (org.apache.commons.httpclient.server.AuthRequestHandler)12 HttpRequestHandlerChain (org.apache.commons.httpclient.server.HttpRequestHandlerChain)12 HttpServiceHandler (org.apache.commons.httpclient.server.HttpServiceHandler)12 Header (org.apache.commons.httpclient.Header)9 UsernamePasswordCredentials (org.apache.commons.httpclient.UsernamePasswordCredentials)9 GetMethod (org.apache.commons.httpclient.methods.GetMethod)9 InputStream (java.io.InputStream)8 InputStreamRequestEntity (org.apache.commons.httpclient.methods.InputStreamRequestEntity)7 StringWriter (java.io.StringWriter)6 Map (java.util.Map)6 ByteArrayRequestEntity (org.apache.commons.httpclient.methods.ByteArrayRequestEntity)6 PutMethod (org.apache.jackrabbit.webdav.client.methods.PutMethod)6