Search in sources :

Example 6 with ContentType

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);
    }
}
Also used : NameValuePair(org.apache.http.NameValuePair) HttpPost(org.apache.http.client.methods.HttpPost) HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) ContentType(org.apache.http.entity.ContentType) HttpGet(org.apache.http.client.methods.HttpGet) IOException(java.io.IOException) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException) URIBuilder(org.apache.http.client.utils.URIBuilder) StringEntity(org.apache.http.entity.StringEntity) Header(org.opennms.protocols.xml.config.Header) Content(org.opennms.protocols.xml.config.Content) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse)

Example 7 with ContentType

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);
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) ContentType(org.apache.http.entity.ContentType) NotNull(org.jetbrains.annotations.NotNull)

Example 8 with ContentType

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);
    }
}
Also used : ContentType(org.apache.http.entity.ContentType) JSONObject(net.sf.json.JSONObject)

Example 9 with ContentType

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);
}
Also used : FormBodyPart(org.apache.http.entity.mime.FormBodyPart) FormBodyPartBuilder(org.apache.http.entity.mime.FormBodyPartBuilder) ContentType(org.apache.http.entity.ContentType) StringBody(org.apache.http.entity.mime.content.StringBody) InputStreamBody(org.apache.http.entity.mime.content.InputStreamBody)

Example 10 with ContentType

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();
        }
    }
}
Also used : Path(org.apache.jackrabbit.spi.Path) QValueValue(org.apache.jackrabbit.spi.commons.value.QValueValue) NamePathResolver(org.apache.jackrabbit.spi.commons.conversion.NamePathResolver) QValue(org.apache.jackrabbit.spi.QValue) HttpEntity(org.apache.http.HttpEntity) ContentType(org.apache.http.entity.ContentType) InputStreamReader(java.io.InputStreamReader) DavException(org.apache.jackrabbit.webdav.DavException) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) RepositoryException(javax.jcr.RepositoryException) IOException(java.io.IOException) NameException(org.apache.jackrabbit.spi.commons.conversion.NameException) IllegalNameException(org.apache.jackrabbit.spi.commons.conversion.IllegalNameException) QValue(org.apache.jackrabbit.spi.QValue) Value(javax.jcr.Value) QValueValue(org.apache.jackrabbit.spi.commons.value.QValueValue) ItemNotFoundException(javax.jcr.ItemNotFoundException)

Aggregations

ContentType (org.apache.http.entity.ContentType)23 IOException (java.io.IOException)9 StringEntity (org.apache.http.entity.StringEntity)9 HttpEntity (org.apache.http.HttpEntity)8 Charset (java.nio.charset.Charset)6 HttpResponse (org.apache.http.HttpResponse)5 HttpPost (org.apache.http.client.methods.HttpPost)5 InputStreamReader (java.io.InputStreamReader)4 HttpGet (org.apache.http.client.methods.HttpGet)4 ByteArrayEntity (org.apache.http.entity.ByteArrayEntity)4 InputStream (java.io.InputStream)3 Reader (java.io.Reader)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Header (org.apache.http.Header)3 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)3 XContentType (org.elasticsearch.common.xcontent.XContentType)3 SuccessUserResponse (com.bwssystems.HABridge.api.SuccessUserResponse)2 UserCreateRequest (com.bwssystems.HABridge.api.UserCreateRequest)2