Search in sources :

Example 1 with HttpEndpoint

use of com.bluenimble.platform.http.HttpEndpoint 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 2 with HttpEndpoint

use of com.bluenimble.platform.http.HttpEndpoint in project serverless by bluenimble.

the class HttpUtils method createEndpoint.

public static HttpEndpoint createEndpoint(final URI uri) {
    if (uri == null) {
        return null;
    }
    HttpEndpoint target = null;
    if (uri.isAbsolute()) {
        // may be overridden later
        int port = uri.getPort();
        String host = uri.getHost();
        if (host == null) {
            // normal parse failed;
            // authority does not seem to care about the valid charset
            // for host names
            host = uri.getAuthority();
            if (host != null) {
                // Strip off any leading user credentials
                int at = host.indexOf('@');
                if (at >= 0) {
                    if (host.length() > at + 1) {
                        host = host.substring(at + 1);
                    } else {
                        // @ on its own
                        host = null;
                    }
                }
                // Extract the port suffix, if present
                if (host != null) {
                    int colon = host.indexOf(':');
                    if (colon >= 0) {
                        if (colon + 1 < host.length()) {
                            port = Integer.parseInt(host.substring(colon + 1));
                        }
                        host = host.substring(0, colon);
                    }
                }
            }
        }
        if (host != null) {
            target = new HttpEndpoint(uri.getScheme(), host, port, uri.getPath(), uri.getQuery());
        }
    }
    return target;
}
Also used : HttpEndpoint(com.bluenimble.platform.http.HttpEndpoint) HttpEndpoint(com.bluenimble.platform.http.HttpEndpoint)

Aggregations

HttpEndpoint (com.bluenimble.platform.http.HttpEndpoint)2 HttpParameter (com.bluenimble.platform.http.HttpParameter)1 HttpRequestWriteException (com.bluenimble.platform.http.request.HttpRequestWriteException)1 AbstractHttpRequest (com.bluenimble.platform.http.request.impls.AbstractHttpRequest)1 OAuthConsumer (oauth.signpost.OAuthConsumer)1 DefaultOAuthConsumer (oauth.signpost.basic.DefaultOAuthConsumer)1 HttpParameters (oauth.signpost.http.HttpParameters)1