Search in sources :

Example 1 with HttpRequestWriteException

use of com.bluenimble.platform.http.request.HttpRequestWriteException in project serverless by bluenimble.

the class BlueNimbleHttpRequestVisitor method visit.

@Override
public void visit(HttpRequest request, HttpURLConnection connection) throws HttpRequestWriteException {
    // sign request
    List<HttpHeader> headers = request.getHeaders();
    if (headers == null) {
        headers = new ArrayList<HttpHeader>();
        request.setHeaders(headers);
    }
    AccessSecretKeysBasedHttpRequestSigner signer = new AccessSecretKeysBasedHttpRequestSigner("m>h>p>d>k>t", "Bearer", accessKey, secretKey);
    String timestamp = Lang.utc();
    headers.add(new HttpHeaderImpl(ApiHeaders.Timestamp, timestamp));
    signer.getData().put('t', timestamp);
    try {
        signer.sign(request);
    } catch (HttpRequestSignerException e) {
        throw new HttpRequestWriteException(e.getMessage(), e);
    }
}
Also used : HttpHeader(com.bluenimble.platform.http.HttpHeader) AccessSecretKeysBasedHttpRequestSigner(com.bluenimble.platform.http.auth.impls.AccessSecretKeysBasedHttpRequestSigner) HttpRequestSignerException(com.bluenimble.platform.http.auth.HttpRequestSignerException) HttpHeaderImpl(com.bluenimble.platform.http.impls.HttpHeaderImpl) HttpRequestWriteException(com.bluenimble.platform.http.request.HttpRequestWriteException)

Example 2 with HttpRequestWriteException

use of com.bluenimble.platform.http.request.HttpRequestWriteException in project serverless by bluenimble.

the class OAuthHttpRequestVisitor method visit.

@Override
public void visit(HttpRequest request, HttpURLConnection connection) throws HttpRequestWriteException {
    HttpEndpoint endpoint = ((AbstractHttpRequest) request).getEndpoint();
    OAuthConsumer consumer = new DefaultOAuthConsumer(key, secret);
    HttpParameters encodedParams = new HttpParameters();
    List<HttpParameter> params = request.getParameters();
    if (params != null && !params.isEmpty()) {
        for (HttpParameter p : params) {
            if (p.getValue() != null) {
                encodedParams.put(p.getName(), OAuth.percentEncode(String.valueOf(p.getValue())));
            }
        }
    }
    encodedParams.put("realm", endpoint.getScheme() + "://" + endpoint.getHost() + endpoint.getPath());
    consumer.setAdditionalParameters(encodedParams);
    try {
        consumer.sign(connection);
    } catch (Exception e) {
        throw new HttpRequestWriteException(e.getMessage(), e);
    }
}
Also used : HttpParameters(oauth.signpost.http.HttpParameters) AbstractHttpRequest(com.bluenimble.platform.http.request.impls.AbstractHttpRequest) DefaultOAuthConsumer(oauth.signpost.basic.DefaultOAuthConsumer) DefaultOAuthConsumer(oauth.signpost.basic.DefaultOAuthConsumer) OAuthConsumer(oauth.signpost.OAuthConsumer) HttpEndpoint(com.bluenimble.platform.http.HttpEndpoint) HttpParameter(com.bluenimble.platform.http.HttpParameter) HttpRequestWriteException(com.bluenimble.platform.http.request.HttpRequestWriteException) HttpRequestWriteException(com.bluenimble.platform.http.request.HttpRequestWriteException)

Example 3 with HttpRequestWriteException

use of com.bluenimble.platform.http.request.HttpRequestWriteException in project serverless by bluenimble.

the class BodyAwareRequest method write.

@Override
public void write(HttpURLConnection hc) throws HttpRequestWriteException {
    hc.setUseCaches(isCachingEnabled());
    String boundary = setContentType();
    if (visitor != null) {
        visitor.visit(this, hc);
    }
    addHeaders(hc);
    Writer writer = null;
    try {
        OutputStream os = null;
        if (isDebugMode()) {
            os = System.out;
        } else {
            os = hc.getOutputStream();
        }
        OutputStreamWriter osw = (charset == null ? new OutputStreamWriter(os) : new OutputStreamWriter(os, charset));
        writer = new PrintWriter(osw, true);
        if (hasParameters() && getBodyPartsCount() == 0) {
            writer.append(super.dumpParameters()).flush();
            return;
        }
        if (getBodyPartsCount() > 0) {
            if (hasParameters()) {
                String cs = charset != null ? charset : HttpUtils.DEFAULT_ENCODING;
                for (HttpParameter p : getParameters()) {
                    writer.append("--" + boundary).append(HttpMessageBody.CRLF);
                    writer.append("Content-Disposition: form-data; name=\"" + p.getName() + "\"").append(HttpMessageBody.CRLF);
                    writer.append("Content-Type: text/plain; charset=" + cs).append(HttpMessageBody.CRLF);
                    writer.append(HttpMessageBody.CRLF);
                    writer.append(String.valueOf(p.getValue())).append(HttpMessageBody.CRLF).flush();
                }
            }
            body.dump(os, charset, boundary);
            return;
        }
    } catch (Throwable th) {
        throw new HttpRequestWriteException(th);
    } finally {
        if (writer != null) {
            try {
                writer.close();
            } catch (IOException e) {
            // IGNORE
            }
        }
    }
}
Also used : OutputStream(java.io.OutputStream) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) HttpParameter(com.bluenimble.platform.http.HttpParameter) PrintWriter(java.io.PrintWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) PrintWriter(java.io.PrintWriter) HttpRequestWriteException(com.bluenimble.platform.http.request.HttpRequestWriteException)

Aggregations

HttpRequestWriteException (com.bluenimble.platform.http.request.HttpRequestWriteException)3 HttpParameter (com.bluenimble.platform.http.HttpParameter)2 HttpEndpoint (com.bluenimble.platform.http.HttpEndpoint)1 HttpHeader (com.bluenimble.platform.http.HttpHeader)1 HttpRequestSignerException (com.bluenimble.platform.http.auth.HttpRequestSignerException)1 AccessSecretKeysBasedHttpRequestSigner (com.bluenimble.platform.http.auth.impls.AccessSecretKeysBasedHttpRequestSigner)1 HttpHeaderImpl (com.bluenimble.platform.http.impls.HttpHeaderImpl)1 AbstractHttpRequest (com.bluenimble.platform.http.request.impls.AbstractHttpRequest)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 PrintWriter (java.io.PrintWriter)1 Writer (java.io.Writer)1 OAuthConsumer (oauth.signpost.OAuthConsumer)1 DefaultOAuthConsumer (oauth.signpost.basic.DefaultOAuthConsumer)1 HttpParameters (oauth.signpost.http.HttpParameters)1