package at.gv.egovernment.moa.id.proxy; import java.io.IOException; import java.net.HttpURLConnection; import com.ibm.webdav.HTTPHeaders; import com.ibm.webdav.protocol.URLStreamHandlerFactory; import java.util.StringTokenizer; import com.ibm.webdav.protocol.http.WebDAVURLConnection; import java.net.URL; import java.net.URLStreamHandler; import java.util.Iterator; import java.util.Map; import javax.net.ssl.SSLSocketFactory; import javax.servlet.http.HttpServletRequest; import at.gv.egovernment.moa.id.config.ConfigurationException; import at.gv.egovernment.moa.id.config.proxy.ProxyConfigurationProvider; import at.gv.egovernment.moa.id.util.MOAIDMessageProvider; import at.gv.egovernment.moa.logging.Logger; import at.gv.egovernment.moa.util.BoolUtils; import com.sun.net.ssl.HostnameVerifier; import com.sun.net.ssl.HttpsURLConnection; /** * Defaultimplementierung von ConnectionBuilder. * @author Paul Ivancsics * @version $Id$ */ public class ElakConnectionBuilder implements ConnectionBuilder { /** a boolean to disable the HostnameVerification (default = false)*/ private static boolean cbDisableHostnameVerification = false; /** a boolean to indicat if webdav protocol handler was already set */ private static boolean webdavPHSet = false; /** * The system property name used to register a protocol handler. */ public final static String PROTOCOL_HANDLER_PROPERTY_NAME = "java.protocol.handler.pkgs"; /** * The package providing the ldap protocol handler. */ public final static String WEBDAV_PROTOCOL_HANDLER = "com.ibm.webdav.protocol"; /** * The pipe character used to sepearte different protocol handlers. */ public final static char PIPE_CHAR = '|'; /** * Constructor for ElakConnectionBuilder. * @throws ConfigurationException on any config error */ public ElakConnectionBuilder() throws ConfigurationException { cbDisableHostnameVerification = BoolUtils.valueOf( ProxyConfigurationProvider.getInstance().getGenericConfigurationParameter( "ProxyComponent.DisableHostnameVerification")); //TODO MOA-ID BRZ undocumented feature if (cbDisableHostnameVerification) Logger.warn("ProxyComponent.DisableHostnameVerification: " + cbDisableHostnameVerification); } /** * @see at.gv.egovernment.moa.id.proxy.ConnectionBuilder#buildConnection */ public HttpURLConnection buildConnection( HttpServletRequest req, String publicURLPrefix, String realURLPrefix, SSLSocketFactory sslSocketFactory, Map parameters) throws IOException { String requestedURL = req.getRequestURL().toString(); // check whether requested URL starts with publicURLPrefix if (! requestedURL.startsWith(publicURLPrefix)) throw new IOException(MOAIDMessageProvider.getInstance().getMessage( "proxy.01", new Object[] {requestedURL, publicURLPrefix})); // in case of GET request, append query string to requested URL; // otherwise, HttpURLConnection would perform a POST request //FIXME right parameters /* if ("get".equalsIgnoreCase(req.getMethod()) && ! parameters.isEmpty()) { requestedURL = appendQueryString(requestedURL, parameters); } */ //TODO RSCH check functionality if (null != req.getQueryString() && 0 != req.getQueryString().length() ) { String query = req.getQueryString(); requestedURL = requestedURL + "?" + query; for (Iterator iter = parameters.keySet().iterator(); iter.hasNext();) { String parameterKey = (String) iter.next(); if(query.indexOf(parameterKey) >= 0) iter.remove(); } } // build real URL in online application String realURLString = realURLPrefix + requestedURL.substring(publicURLPrefix.length()); Logger.info("Registering WebDAV protocol handler"); String protocolHandlers = System.getProperty(ElakConnectionBuilder.PROTOCOL_HANDLER_PROPERTY_NAME); if (protocolHandlers == null) { protocolHandlers = ElakConnectionBuilder.WEBDAV_PROTOCOL_HANDLER; System.setProperty(ElakConnectionBuilder.PROTOCOL_HANDLER_PROPERTY_NAME, protocolHandlers); } else { // check, if WEBDAV protocol handler is already configured boolean isConfigured = false; StringTokenizer tokenizer = new StringTokenizer(protocolHandlers, "| "); while (tokenizer.hasMoreTokens()) { String protocolHandler = tokenizer.nextToken(); if (protocolHandler.equals(ElakConnectionBuilder.WEBDAV_PROTOCOL_HANDLER)) { isConfigured = true; break; } } // if it has not been configured yet, configure it if (!isConfigured) { protocolHandlers = ElakConnectionBuilder.WEBDAV_PROTOCOL_HANDLER + ElakConnectionBuilder.PIPE_CHAR + protocolHandlers; System.setProperty(ElakConnectionBuilder.PROTOCOL_HANDLER_PROPERTY_NAME, protocolHandlers); } } Logger.info("Registered protocol handlers: " + protocolHandlers); Class webdavSH = null; try { webdavSH = Class.forName(ElakConnectionBuilder.WEBDAV_PROTOCOL_HANDLER + ".http.Handler"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } URLStreamHandler urlStreamHandler = null; try { urlStreamHandler = (URLStreamHandler) webdavSH.newInstance(); } catch (InstantiationException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (IllegalAccessException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } //URL testURL = new URL("http", realURLString.substring("http://localhost:82".length()), 82, "", urlStreamHandler); //WebDAVURLConnection webDavTest = (WebDAVURLConnection) testURL.openConnection(); URL testURL = new URL(realURLString); Logger.debug("TEST URL ist von der Klasse: " + testURL.getClass().getName()); //URL url = new URL(realURLString); URL testURL2 = new URL(realURLString); URL url = new URL("http", "localhost", 82, realURLString.substring("http://localhost:82".length()), urlStreamHandler); Logger.debug("OA Request: " + req.getMethod() + " " + url.toString()); WebDAVURLConnection webDavConn = (WebDAVURLConnection) url.openConnection(); HttpURLConnection conn = (HttpURLConnection)webDavConn; webDavConn.setRequestMethod(req.getMethod()); webDavConn.setDoInput(true); webDavConn.setDoOutput(true); //conn.setUseCaches(false); webDavConn.setAllowUserInteraction(true); webDavConn.setInstanceFollowRedirects(false); if (conn instanceof HttpsURLConnection && sslSocketFactory != null) { HttpsURLConnection httpsConn = (HttpsURLConnection) conn; httpsConn.setSSLSocketFactory(sslSocketFactory); if (cbDisableHostnameVerification) httpsConn.setHostnameVerifier(new HostnameNonVerifier()); } return conn; } /** * @param requestedURL * @param parameters * @return */ private String appendQueryString(String requestedURL, Map parameters) { String newURL = requestedURL; for (Iterator iter = parameters.keySet().iterator(); iter.hasNext();) { String paramName = (String)iter.next(); String paramValue = (String)parameters.get(paramName); String paramString = paramName + "=" + paramValue; if (newURL.indexOf("?") < 0) newURL = newURL + "?" + paramString; else newURL = newURL + "&" + paramString; } return newURL; } /** * @author Stefan Knirsch * @version $Id$ * A private class to change the standard HostName verifier to disable the * Hostname Verification Check */ private class HostnameNonVerifier implements HostnameVerifier { /** * @see com.sun.net.ssl.HostnameVerifier#verify(String, String) */ public boolean verify(String arg0, String arg1) { return true; } } }