/** * 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. * * $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 java.util.Properties; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.commons.httpclient.Header; 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 Properties 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); Properties response_properties = new Properties(); if (logger_.isDebugEnabled()) { Header[] response_headers = post_method.getResponseHeaders(); logger_.debug("#" + response_headers.length + " headers in response:"); for (int i = 0; i < response_headers.length; i++) { logger_.debug(" response_header[" + i + "]: name = " + response_headers[i].getName() + ", value = " + response_headers[i].getValue()); } } Header server_header = post_method.getResponseHeader("Server"); logger_.debug("server_header: name = " + server_header.getName() + ", value = " + server_header.getValue()); parseBKUVersion(server_header.getValue(), response_properties); 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(); response_properties.setProperty("response_string", response_string); return response_properties; } // TODO hotfix public static void parseBKUVersion(String header_value, Properties properties) { // dummy bku header starts with Apache Coyote... //Pattern pattern = Pattern.compile("^citizen-card-environment/(\\d+\\.\\d+) (.+)/(\\d+\\.\\d+\\.\\d+)$"); //Pattern pattern = Pattern.compile("^.*citizen-card-environment/(\\d+\\.\\d+) (.+)/(\\d+\\.\\d+\\.\\d+)$"); Pattern pattern = Pattern.compile("^.*citizen-card-environment/(\\d+\\.\\d+) (.+)/(\\d+\\.\\d+\\.\\d+)(.*)$"); Matcher m = pattern.matcher(header_value); m.matches(); logger_.debug("group count = " + m.groupCount()); for (int i = 0; i <= m.groupCount(); i++) { logger_.debug(" group[" + i + "] = " + m.group(i)); } final String cceVersion = m.group(1); final String productName = m.group(2); final String productVersion = m.group(3); logger_.debug("cceVersion = " + cceVersion); logger_.debug("productName = " + productName); logger_.debug("productVersion = " + productVersion); properties.setProperty("cceVersion", cceVersion); properties.setProperty("productName", productName); properties.setProperty("productVersion", productVersion); } }