/** * */ package at.knowcenter.wag.egov.egiz.sig.connectors.bku; import java.io.IOException; import java.util.Properties; import org.apache.commons.httpclient.Header; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.methods.multipart.ByteArrayPartSource; import org.apache.commons.httpclient.methods.multipart.FilePart; import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity; import org.apache.commons.httpclient.methods.multipart.Part; import org.apache.commons.httpclient.methods.multipart.StringPart; import org.apache.commons.httpclient.params.HttpMethodParams; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import at.knowcenter.wag.egov.egiz.sig.SignatureData; /** * Helper class with methods that use the Apache Https Client to send HTTP * requests. * * @author wprinz */ public final class BKUPostConnection { /** * The response Properties key that identifies the response string. */ public static final String RESPONSE_STRING_KEY = "response_string"; //$NON-NLS-1$ /** * The response Properties key that identifies the BKU Server header. */ public static final String BKU_SERVER_HEADER_KEY = "BKU-Server-Header"; //$NON-NLS-1$ /** * The log. */ private static Log log = LogFactory.getLog(BKUPostConnection.class); /** * Sends a multipart/form-data HTTP Post request to the given URL. * * @param url The url the request is directed to. * @param request The request XML, which will be the UTF-8 text/xml first part of the message. * @param data The binary second part of the message. * @return Returns the response properties which, among others, contain the response String. * @throws HttpException * @throws IOException */ public static Properties doPostRequestMultipart(String url, String request, SignatureData data) throws HttpException, IOException { log.debug("doPostRequestMultipart:"); //$NON-NLS-1$ StringPart xmlpart = new StringPart("XmlRequest", request, "UTF-8"); //$NON-NLS-1$//$NON-NLS-2$ // TODO this is a BUG in BKU that doesn't allow the Content-Type header xmlpart.setContentType(null); xmlpart.setTransferEncoding(null); // BKU 2.7.4 can't handle the Content-Type Header for the XML // xmlpart.setContentType("text/xml"); // xmlpart.setTransferEncoding(null); String filename = data.getMimeType().equals("application/pdf") ? "myfile.pdf" : "myfile.txt"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ ByteArrayPartSource baps = new ByteArrayPartSource(filename, data.getData()); FilePart filepart = new FilePart("fileupload", baps); //$NON-NLS-1$ filepart.setContentType(data.getMimeType()); // this is optional // filepart.setCharSet(data.getCharacterEncoding()); Part[] parts = { xmlpart, filepart }; HttpMethodParams method_params = new HttpMethodParams(); method_params.setContentCharset("UTF-8"); //$NON-NLS-1$ PostMethod post_method = new PostMethod(url); post_method.setParams(method_params); MultipartRequestEntity mprqe = new MultipartRequestEntity(parts, post_method.getParams()); post_method.setRequestEntity(mprqe); HttpClient http_client = new HttpClient(); int method_response = http_client.executeMethod(post_method); log.debug("method_response = " + method_response); //$NON-NLS-1$ Properties response_properties = new Properties(); if (log.isDebugEnabled()) { Header[] response_headers = post_method.getResponseHeaders(); for (int i = 0; i < response_headers.length; i++) { log.debug(" response_header[" + i + "]: name = " + response_headers[i].getName() + ", value = " + response_headers[i].getValue()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ } } Header server_header = post_method.getResponseHeader("Server"); //$NON-NLS-1$ response_properties.setProperty(BKU_SERVER_HEADER_KEY, server_header.getValue()); log.debug(post_method.getResponseCharSet()); if (!post_method.getResponseCharSet().equals("UTF-8")) //$NON-NLS-1$ { log.warn("BKU response charset is not UTF-8!"); //$NON-NLS-1$ } String response_string = post_method.getResponseBodyAsString(); response_properties.setProperty(RESPONSE_STRING_KEY, response_string); log.debug("doPostRequestMultipart finished."); //$NON-NLS-1$ return response_properties; } }