use of org.apache.http.entity.mime.content.ByteArrayBody in project scheduling by ow2-proactive.
the class LoginWithCredentialsCommand method login.
@Override
protected String login(ApplicationContext currentContext) throws CLIException {
File credentials = new File(pathname);
if (!credentials.exists()) {
throw new CLIException(REASON_INVALID_ARGUMENTS, String.format("File does not exist: %s", credentials.getAbsolutePath()));
}
if (warn) {
writeLine(currentContext, "Using the default credentials file: %s", credentials.getAbsolutePath());
}
HttpPost request = new HttpPost(currentContext.getResourceUrl("login"));
MultipartEntity entity = new MultipartEntity();
entity.addPart("credential", new ByteArrayBody(FileUtility.byteArray(credentials), APPLICATION_OCTET_STREAM.getMimeType()));
request.setEntity(entity);
HttpResponseWrapper response = execute(request, currentContext);
if (statusCode(OK) == statusCode(response)) {
return StringUtility.responseAsString(response).trim();
} else {
handleError("An error occurred while logging: ", response, currentContext);
throw new CLIException(REASON_OTHER, "An error occurred while logging.");
}
}
use of org.apache.http.entity.mime.content.ByteArrayBody in project selenium_java by sergueik.
the class RestClient method request.
private JSON request(HttpEntityEnclosingRequestBase req, Issue.NewAttachment... attachments) throws RestException, IOException {
if (attachments != null) {
req.setHeader("X-Atlassian-Token", "nocheck");
MultipartEntity ent = new MultipartEntity();
for (Issue.NewAttachment attachment : attachments) {
String filename = attachment.getFilename();
Object content = attachment.getContent();
if (content instanceof byte[]) {
ent.addPart("file", new ByteArrayBody((byte[]) content, filename));
} else if (content instanceof InputStream) {
ent.addPart("file", new InputStreamBody((InputStream) content, filename));
} else if (content instanceof File) {
ent.addPart("file", new FileBody((File) content, filename));
} else if (content == null) {
throw new IllegalArgumentException("Missing content for the file " + filename);
} else {
throw new IllegalArgumentException("Expected file type byte[], java.io.InputStream or java.io.File but provided " + content.getClass().getName() + " for the file " + filename);
}
}
req.setEntity(ent);
}
return request(req);
}
use of org.apache.http.entity.mime.content.ByteArrayBody in project new-cloud by xie-summer.
the class HttpUtils method uploadFile.
/**
* @param url
* @param params
* @param uploadMap
* @param fileNameMap
* @param encode
* @return
*/
public static HttpResult uploadFile(String url, Map<String, String> params, Map<String, byte[]> uploadMap, Map<String, String> fileNameMap, String encode, int timeout) {
/**
* DefaultHttpClient client = new DefaultHttpClient();
* client.getParams().setIntParameter("http.socket.timeout",
* LONG_TIMEOUT); client.getParams().setBooleanParameter(
* "http.protocol.expect-continue", false);
* client.getParams().setIntParameter("http.connection.timeout",
* CONNECTION_TIMEOUT);
*/
CookieStore cookieStore = getCookieStore(null);
CloseableHttpClient client = getHttpClient(CONNECTION_TIMEOUT, timeout, cookieStore);
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create().setCharset(Charset.forName(encode));
HttpPost request = new HttpPost(url);
// MultipartEntity reqEntity = new MultipartEntity();
for (String input : uploadMap.keySet()) {
ByteArrayBody isb = new ByteArrayBody(uploadMap.get(input), fileNameMap.get(input));
multipartEntityBuilder.addPart(input, isb);
}
try {
if (params != null && !params.isEmpty()) {
for (String key : params.keySet()) {
multipartEntityBuilder.addTextBody(key, params.get(key), ContentType.create("text/plain", Charset.forName(encode)));
}
}
request.setEntity(multipartEntityBuilder.build());
List<Cookie> reqcookie = cookieStore.getCookies();
CloseableHttpResponse response = client.execute(request);
try {
String result = "";
HttpEntity entity = getEntity(response);
if (entity != null) {
result = EntityUtils.toString(entity, encode);
}
if (isSuccess(response)) {
HttpResult ret = HttpResult.getSuccessReturn(result);
addHeader(ret, response);
List<Cookie> cookies = cookieStore.getCookies();
addCookie(ret, cookies, reqcookie);
return ret;
} else {
int statusCode = response.getStatusLine().getStatusCode();
String msg = "httpStatus:" + statusCode + response.getStatusLine().getReasonPhrase() + ", Header: ";
Header[] headers = response.getAllHeaders();
for (Header header : headers) {
msg += header.getName() + ":" + header.getValue();
}
request.abort();
DB_LOGGER.error("ERROR HttpUtils:" + msg + request.getURI());
return HttpResult.getFailure("httpStatus:" + response.getStatusLine().getStatusCode(), statusCode, result);
}
} finally {
response.close();
}
} catch (HttpHostConnectException e) {
request.abort();
DB_LOGGER.error(request.getURI() + ":" + LoggerUtils.getExceptionTrace(e, 30));
return HttpResult.getFailure(request.getURI() + " exception:" + e.getClass().getCanonicalName(), HTTP_STATUSCODE_HTTP_HOST_CONNECT_EXCEPTION);
} catch (ConnectTimeoutException e) {
request.abort();
DB_LOGGER.error(request.getURI() + ":" + LoggerUtils.getExceptionTrace(e, 30));
return HttpResult.getFailure(request.getURI() + " exception:" + e.getClass().getCanonicalName(), HTTP_STATUSCODE_CONNECT_TIMEOUT_EXCEPTION);
} catch (SocketTimeoutException e) {
request.abort();
DB_LOGGER.error(request.getURI() + ":" + LoggerUtils.getExceptionTrace(e, 30));
return HttpResult.getFailure(request.getURI() + " exception:" + e.getClass().getCanonicalName(), HTTP_STATUSCODE_SOCKET_TIMEOUT_EXCEPTION);
} catch (Exception e) {
request.abort();
DB_LOGGER.error(request.getURI() + ":" + LoggerUtils.getExceptionTrace(e, 100));
return HttpResult.getFailure(request.getURI() + " exception:" + e.getClass().getCanonicalName(), EXCEPTION_HTTP_STATUSCODE);
}
}
Aggregations