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);
}
}
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);
}
}
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
}
}
}
}
Aggregations