/** * Copyright (c) 2006 by Know-Center, Graz, Austria * * This software is the confidential and proprietary information of Know-Center, * Graz, Austria. You shall not disclose such Confidential Information and shall * use it only in accordance with the terms of the license agreement you entered * into with Know-Center. * * KNOW-CENTER MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR * NON-INFRINGEMENT. KNOW-CENTER SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY * LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS * DERIVATIVES. * * $Id: BKUPostConnection.java,v 1.3 2006/10/11 07:56:10 wprinz Exp $ */ package at.knowcenter.wag.egov.egiz.sig.connectors; import java.io.IOException; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.NameValuePair; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.params.HttpMethodParams; import org.apache.log4j.Logger; import at.knowcenter.wag.egov.egiz.cfg.ConfigLogger; /** * @author wprinz */ public abstract class BKUPostConnection { /** * The logger definition. */ private static final Logger logger_ = ConfigLogger.getLogger(BKUPostConnection.class); /** * This method connects the BKU server getting the request and the url. The * request is an XML Message send and recieve by the HttpClient module. The * Response message of the BKU server is is send back to the calling method. * * @param url * the URL which the BKU server is running * @param request * the request string (XML) to send. * @return the response string (XML) of the BKU server * @throws IOException * @throws HttpException * ErrorCode:320 */ public static String doPostRequest(String url, String request) throws HttpException, IOException { PostMethod post_method = new PostMethod(url); // It is very important to specify the charset of the content (the request) // as UTF-8 this way. // The HttpClient will then perform the URL encoding assuming that the // request is UTF-8 as the BKU expects. // If the MethodParams are omitted, the HttpClient will assume that the // request is ISO-8859-1 and thereby the BKU cannot properly decode it. HttpMethodParams method_params = new HttpMethodParams(); method_params.setContentCharset("UTF-8"); post_method.setParams(method_params); // This is just a hint: do not set the content-type this way or the BKU will // assume it as text/XML, but the HttpClient sends it as URL-encoded. // The HttpClient will automatically generate the proper Content-Type: // application/x-www-form-urlencoded // post.addRequestHeader(new Header("Content-Type", // "text/xml;charset=UTF-8")); NameValuePair[] data = { new NameValuePair("XMLRequest", request) }; post_method.setRequestBody(data); HttpClient http_client = new HttpClient(); int method_response = http_client.executeMethod(post_method); logger_.debug("method_response = " + method_response); byte[] response_body = post_method.getResponseBody(); String response_string = new String(response_body, "UTF-8"); // Alternatively this could be used. // The HttpClient is assumed to use the Content-Type provided by the // response. // String response_string = post.getResponseBodyAsString(); return response_string; } }