Search in sources :

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

Example 57 with StringRequestEntity

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

Example 58 with StringRequestEntity

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();
    }
}
Also used : JSONTokener(org.json.JSONTokener) StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) Header(org.apache.commons.httpclient.Header) InputStreamReader(java.io.InputStreamReader) JSONObject(org.json.JSONObject) PostMethod(org.apache.commons.httpclient.methods.PostMethod) LuceneQueryParserException(org.alfresco.repo.search.impl.lucene.LuceneQueryParserException) BufferedReader(java.io.BufferedReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) URI(org.apache.commons.httpclient.URI)

Example 59 with StringRequestEntity

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;
}
Also used : StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) JSONObject(net.sf.json.JSONObject) PostMethod(org.apache.commons.httpclient.methods.PostMethod) HttpClient(org.apache.commons.httpclient.HttpClient) HttpResponseException(org.apache.http.client.HttpResponseException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) ClientProtocolException(org.apache.http.client.ClientProtocolException)

Example 60 with StringRequestEntity

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

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