/** * Copyright 2006 by Know-Center, Graz, Austria * PDF-AS has been contracted by the E-Government Innovation Center EGIZ, a * joint initiative of the Federal Chancellery Austria and Graz University of * Technology. * * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by * the European Commission - subsequent versions of the EUPL (the "Licence"); * You may not use this work except in compliance with the Licence. * You may obtain a copy of the Licence at: * http://www.osor.eu/eupl/ * * Unless required by applicable law or agreed to in writing, software * distributed under the Licence is distributed on an "AS IS" basis, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the Licence for the specific language governing permissions and * limitations under the Licence. * * This product combines work with different licenses. See the "NOTICE" text * file for details on the various modules and licenses. * The "NOTICE" text file is part of the distribution. Any derivative works * that you distribute must include a readable copy of the "NOTICE" text file. */ package at.knowcenter.wag.egov.egiz.sig.connectors.bku; import java.io.IOException; import java.io.InputStream; 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.FilePart; import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity; import org.apache.commons.httpclient.methods.multipart.Part; import org.apache.commons.httpclient.methods.multipart.PartSource; 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.gv.egiz.pdfas.api.commons.Constants; 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 response property that declares the signature layout being applied. */ public static final String BKU_SIGNATURE_LAYOUT_HEADER_KEY = "BKU-Signature-Layout"; //$NON-NLS-1$ /** * The response Properties key that identifies the BKU User-Agent header. */ public static final String BKU_USER_AGENT_HEADER_KEY = "BKU-User-Agent-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, final 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); final String filename = data.getMimeType().equals("application/pdf") ? "myfile.pdf" : "myfile.txt"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ PartSource ps = new PartSource() { public InputStream createInputStream() throws IOException { return data.getDataSource().createInputStream(); } public String getFileName() { return filename; } public long getLength() { return data.getDataSource().getLength(); } }; //ByteArrayPartSource baps = new ByteArrayPartSource(filename, data.getData()); FilePart filepart = new FilePart("fileupload", ps); //$NON-NLS-1$ filepart.setContentType(data.getMimeType()); // not really needed since external referenced data has to be taken "as-is" (binary stream) for // digest calculation, so neither content type nor charset is relevant 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$ if (server_header != null) { response_properties.setProperty(BKU_SERVER_HEADER_KEY, server_header.getValue()); } else { log.warn("BKU response header \"Server\" is empty."); } Header signatureLayoutHeader = post_method.getResponseHeader(Constants.BKU_HEADER_SIGNATURE_LAYOUT); //$NON-NLS-1$ if (signatureLayoutHeader != null) { response_properties.setProperty(BKU_SIGNATURE_LAYOUT_HEADER_KEY, signatureLayoutHeader.getValue()); } String responseCharSet = post_method.getResponseCharSet(); if (!"UTF8".equalsIgnoreCase(responseCharSet) && !"UTF-8".equalsIgnoreCase(responseCharSet)) { 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; } }