use of org.apache.http.entity.ContentType in project opennms by OpenNMS.
the class HttpUrlConnection method getInputStream.
/* (non-Javadoc)
* @see java.net.URLConnection#getInputStream()
*/
@Override
public InputStream getInputStream() throws IOException {
try {
if (m_clientWrapper == null) {
connect();
}
// Build URL
int port = m_url.getPort() > 0 ? m_url.getPort() : m_url.getDefaultPort();
URIBuilder ub = new URIBuilder();
ub.setPort(port);
ub.setScheme(m_url.getProtocol());
ub.setHost(m_url.getHost());
ub.setPath(m_url.getPath());
if (m_url.getQuery() != null && !m_url.getQuery().trim().isEmpty()) {
final List<NameValuePair> params = URLEncodedUtils.parse(m_url.getQuery(), StandardCharsets.UTF_8);
if (!params.isEmpty()) {
ub.addParameters(params);
}
}
// Build Request
HttpRequestBase request = null;
if (m_request != null && m_request.getMethod().equalsIgnoreCase("post")) {
final Content cnt = m_request.getContent();
HttpPost post = new HttpPost(ub.build());
ContentType contentType = ContentType.create(cnt.getType());
LOG.info("Processing POST request for {}", contentType);
if (contentType.getMimeType().equals(ContentType.APPLICATION_FORM_URLENCODED.getMimeType())) {
FormFields fields = JaxbUtils.unmarshal(FormFields.class, cnt.getData());
post.setEntity(fields.getEntity());
} else {
StringEntity entity = new StringEntity(cnt.getData(), contentType);
post.setEntity(entity);
}
request = post;
} else {
request = new HttpGet(ub.build());
}
if (m_request != null) {
// Add Custom Headers
for (final Header header : m_request.getHeaders()) {
request.addHeader(header.getName(), header.getValue());
}
}
// Get Response
CloseableHttpResponse response = m_clientWrapper.execute(request);
return response.getEntity().getContent();
} catch (Exception e) {
throw new IOException("Can't retrieve " + m_url.getPath() + " from " + m_url.getHost() + " because " + e.getMessage(), e);
}
}
use of org.apache.http.entity.ContentType in project intellij-plugins by StepicOrg.
the class HttpTransportClient method post.
@NotNull
@Override
public ClientResponse post(@NotNull StepikApiClient stepikApiClient, @NotNull String url, @Nullable String body, @Nullable Map<String, String> headers) {
HttpPost request = new HttpPost(url);
if (headers == null) {
headers = new HashMap<>();
}
headers.forEach(request::setHeader);
if (body != null) {
ContentType contentType;
contentType = ContentType.create(headers.getOrDefault(CONTENT_TYPE_HEADER, CONTENT_TYPE), Consts.UTF_8);
request.setEntity(new StringEntity(body, contentType));
}
return call(stepikApiClient, request);
}
use of org.apache.http.entity.ContentType in project tdi-studio-se by Talend.
the class HttpUtil method handHttpResponse.
public static void handHttpResponse(org.apache.http.HttpResponse response) throws java.io.IOException {
if (response == null) {
return;
}
if (response.getStatusLine().getStatusCode() != 200) {
String message = "";
String returnedValue = EntityUtils.toString(response.getEntity());
ContentType contentType = ContentType.get(response.getEntity());
if (contentType != null && ContentType.APPLICATION_JSON.getMimeType().equals(contentType.getMimeType())) {
JSONObject object = JSONObject.fromObject(returnedValue);
message = getValueFromJson(object, "message");
String cause = getValueFromJson(object, "cause");
if (!cause.isEmpty()) {
if (!message.isEmpty()) {
message = message + "\n" + cause;
} else {
message = cause;
}
}
if (message.isEmpty()) {
message = response.getStatusLine().getReasonPhrase();
}
} else {
if (returnedValue.isEmpty()) {
message = response.getStatusLine().getReasonPhrase();
}
}
throw new RuntimeException(message);
}
}
use of org.apache.http.entity.ContentType in project jackrabbit by apache.
the class Utils method addPart.
/**
*
* @param paramName
* @param value
* @param resolver
* @throws RepositoryException
*/
static void addPart(String paramName, QValue value, NamePathResolver resolver, List<FormBodyPart> parts, List<QValue> binaries) throws RepositoryException {
FormBodyPartBuilder builder = FormBodyPartBuilder.create().setName(paramName);
ContentType ctype = ContentType.create(JcrValueType.contentTypeFromType(value.getType()), DEFAULT_CHARSET);
FormBodyPart part;
switch(value.getType()) {
case PropertyType.BINARY:
binaries.add(value);
part = builder.setBody(new InputStreamBody(value.getStream(), ctype)).build();
break;
case PropertyType.NAME:
part = builder.setBody(new StringBody(resolver.getJCRName(value.getName()), ctype)).build();
break;
case PropertyType.PATH:
part = builder.setBody(new StringBody(resolver.getJCRPath(value.getPath()), ctype)).build();
break;
default:
part = builder.setBody(new StringBody(value.getString(), ctype)).build();
}
parts.add(part);
}
use of org.apache.http.entity.ContentType in project jackrabbit by apache.
the class RepositoryServiceImpl method getPropertyInfo.
@Override
public PropertyInfo getPropertyInfo(SessionInfo sessionInfo, PropertyId propertyId) throws RepositoryException {
HttpGet request = null;
try {
String uri = getItemUri(propertyId, sessionInfo);
request = new HttpGet(uri);
HttpResponse response = executeRequest(sessionInfo, request);
int status = response.getStatusLine().getStatusCode();
if (status != DavServletResponse.SC_OK) {
throw ExceptionConverter.generate(new DavException(status, response.getStatusLine().getReasonPhrase()));
}
Path path = uriResolver.getQPath(uri, sessionInfo);
HttpEntity entity = response.getEntity();
ContentType ct = ContentType.get(entity);
boolean isMultiValued;
QValue[] values;
int type;
NamePathResolver resolver = getNamePathResolver(sessionInfo);
if (ct != null && ct.getMimeType().startsWith("jcr-value")) {
type = JcrValueType.typeFromContentType(ct.getMimeType());
QValue v;
if (type == PropertyType.BINARY) {
v = getQValueFactory().create(entity.getContent());
} else {
Reader reader = new InputStreamReader(entity.getContent(), ct.getCharset());
StringBuffer sb = new StringBuffer();
int c;
while ((c = reader.read()) > -1) {
sb.append((char) c);
}
Value jcrValue = valueFactory.createValue(sb.toString(), type);
if (jcrValue instanceof QValueValue) {
v = ((QValueValue) jcrValue).getQValue();
} else {
v = ValueFormat.getQValue(jcrValue, resolver, getQValueFactory());
}
}
values = new QValue[] { v };
isMultiValued = false;
} else if (ct != null && ct.getMimeType().equals("text/xml")) {
// jcr:values property spooled
values = getValues(entity.getContent(), resolver, propertyId);
type = (values.length > 0) ? values[0].getType() : loadType(uri, getClient(sessionInfo), propertyId, sessionInfo, resolver);
isMultiValued = true;
} else {
throw new ItemNotFoundException("Unable to retrieve the property with id " + saveGetIdString(propertyId, resolver));
}
return new PropertyInfoImpl(propertyId, path, type, isMultiValued, values);
} catch (IOException e) {
throw new RepositoryException(e);
} catch (DavException e) {
throw ExceptionConverter.generate(e);
} catch (NameException e) {
throw new RepositoryException(e);
} finally {
if (request != null) {
request.releaseConnection();
}
}
}
Aggregations