Search in sources :

Example 1 with Content

use of org.opennms.protocols.xml.config.Content 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 2 with Content

use of org.opennms.protocols.xml.config.Content in project opennms by OpenNMS.

the class HttpUrlConnectionIT method buildRequest.

/**
     * Builds the request.
     *
     * @param contentType the content type
     * @param contentData the content data
     * @return the request
     */
private Request buildRequest(String contentType, String contentData) {
    Request req = new Request();
    req.setMethod("POST");
    req.addParameter("timeout", "3000");
    req.addParameter("retries", "2");
    req.addHeader("User-Agent", "FireFox 22.0");
    req.setContent(new Content(contentType, contentData));
    return req;
}
Also used : Content(org.opennms.protocols.xml.config.Content) Request(org.opennms.protocols.xml.config.Request)

Example 3 with Content

use of org.opennms.protocols.xml.config.Content in project opennms by OpenNMS.

the class AbstractXmlCollectionHandler method parseRequest.

/**
     * Parses the request.
     *
     * @param unformattedRequest the unformatted request
     * @param agent the agent
     * @param collectionStep the collection step
     * @param parameters the service parameters
     * @return the request
     * @throws IllegalArgumentException the illegal argument exception
     */
@Override
public Request parseRequest(final NodeDao nodeDao, final Request unformattedRequest, final CollectionAgent agent, final Integer collectionStep, final Map<String, String> parameters) throws IllegalArgumentException {
    if (unformattedRequest == null)
        return null;
    final OnmsNode node = nodeDao.get(agent.getNodeId());
    final Request request = new Request();
    request.setMethod(unformattedRequest.getMethod());
    for (Header header : unformattedRequest.getHeaders()) {
        request.addHeader(header.getName(), parseString(header.getName(), header.getValue(), node, agent.getHostAddress(), collectionStep, parameters));
    }
    for (Parameter param : unformattedRequest.getParameters()) {
        request.addParameter(param.getName(), parseString(param.getName(), param.getValue(), node, agent.getHostAddress(), collectionStep, parameters));
    }
    final Content cnt = unformattedRequest.getContent();
    if (cnt != null)
        request.setContent(new Content(cnt.getType(), parseString("Content", cnt.getData(), node, agent.getHostAddress(), collectionStep, parameters)));
    return request;
}
Also used : OnmsNode(org.opennms.netmgt.model.OnmsNode) Header(org.opennms.protocols.xml.config.Header) Content(org.opennms.protocols.xml.config.Content) Request(org.opennms.protocols.xml.config.Request) Parameter(org.opennms.protocols.xml.config.Parameter)

Aggregations

Content (org.opennms.protocols.xml.config.Content)3 Header (org.opennms.protocols.xml.config.Header)2 Request (org.opennms.protocols.xml.config.Request)2 IOException (java.io.IOException)1 GeneralSecurityException (java.security.GeneralSecurityException)1 NameValuePair (org.apache.http.NameValuePair)1 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)1 HttpGet (org.apache.http.client.methods.HttpGet)1 HttpPost (org.apache.http.client.methods.HttpPost)1 HttpRequestBase (org.apache.http.client.methods.HttpRequestBase)1 URIBuilder (org.apache.http.client.utils.URIBuilder)1 ContentType (org.apache.http.entity.ContentType)1 StringEntity (org.apache.http.entity.StringEntity)1 OnmsNode (org.opennms.netmgt.model.OnmsNode)1 Parameter (org.opennms.protocols.xml.config.Parameter)1