Subversion Repositories XServices

Rev

Rev 149 | Rev 196 | Go to most recent revision | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

/*
 *   Copyright 2013 Brian Rosenberger (Brutex Network)
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */
package net.brutex.emitter;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.EntityBuilder;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;

/**
 * Construct a HTTP POST and send it.
 *
 * @author Brian Rosenberger, bru(at)brutex.de
 * @since 0.1
 */
public class SimpleHttpEvent {
        
        private final Logger logger = Logger.getLogger(SimpleHttpEvent.class);
        private final String url;
        private final String soapBody;
        private long duration = 0;
        
        /**
         * Instantiates a new simple http event.
         *
         * @param url the url
         * @param soapBody the soap body
         */
        public SimpleHttpEvent(String url, String soapBody) {
                this.url = url;
                this.soapBody = soapBody;
        }
        
        /**
         * Send soap.
         *
         * @param url the url
         * @param soapBody the soap body
         * @throws ClientProtocolException the client protocol exception
         * @throws IOException Signals that an I/O exception has occurred.
         */
        public void sendSoap(boolean isDropResponse) throws ClientProtocolException, IOException {
                long start = System.currentTimeMillis();
                HttpPost post = new HttpPost(url);
        post.addHeader("Accept" , "text/xml");
        post.addHeader("SOAPAction","");
        EntityBuilder entitybuilder = EntityBuilder.create();
        entitybuilder.setContentEncoding("UTF-8");
        entitybuilder.setText(soapBody);
        HttpEntity entity = entitybuilder.build();                         
        post.setEntity(entity);
        
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpResponse r = httpclient.execute(post);
        logger.debug("Sending event to ALF event manager");
        if(! isDropResponse) {
                        HttpEntity e = r.getEntity();
                        StringBuilder sb = new StringBuilder();
                        BufferedReader in = new BufferedReader(new InputStreamReader(e.getContent()));
                        String s;
                        while( (s=in.readLine()) != null) {
                           sb.append(s);
                        }
                        logger.debug("Response: \n " + sb.toString());
        } else {
                logger.debug("Response was dropped.");
        }
        duration = System.currentTimeMillis() - start;
        }
        
        /**
         * Get the response time of the soap call in milliseconds.
         * 
         * @return duration or '0' if not yet executed
         */
        public long getDuration() {
                return duration;
        }
}