Java tutorial
/* * Copyright 2001-2011 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * */ package org.apache.juddi.v3.client.mapping.wsdl; import java.io.IOException; import java.io.InputStream; import java.io.StringReader; import java.net.URI; import java.net.URL; import javax.wsdl.xml.WSDLLocator; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.http.HttpResponse; import org.apache.http.auth.AuthScope; import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.client.ResponseHandler; import org.apache.http.client.methods.HttpGet; import org.apache.http.conn.ClientConnectionManager; import org.apache.http.conn.scheme.Scheme; import org.apache.http.conn.scheme.SchemeRegistry; import org.apache.http.impl.client.BasicResponseHandler; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.impl.conn.BasicClientConnectionManager; import org.apache.juddi.v3.client.mapping.MockSSLSocketFactory; import org.xml.sax.InputSource; /** * Implementation of the interface {@link WSDLLocatorImpl}. * * @author <a href="mailto:kstam@apache.org">Kurt T Stam</a> * @author <a href="mailto:alexoree@apache.org">Alex O'Ree</a> - Modified for supporting http based credentials */ public class WSDLLocatorImpl implements WSDLLocator { private Exception lastException = null; private final Log log = LogFactory.getLog(this.getClass()); private InputStream inputStream = null; private URI baseURI; private boolean ignoreSSLErrors = false; private String latestImportURI; private String username = null, password = null; /** * Constructor taking the URI to the WSDL. This class implements the * {@link WSDLLocator} Interface. * * @param baseURI - URI of the WSDL */ public WSDLLocatorImpl(URI baseURI) { this.baseURI = baseURI; this.ignoreSSLErrors = false; } /** * Constructor taking the URI to the WSDL. This class implements the * {@link WSDLLocator} Interface and includes support for HTTP * Authentication * * @param baseURI * @param username * @param password * @param ignoreSSLErrors */ public WSDLLocatorImpl(URI baseURI, String username, String password, boolean ignoreSSLErrors) { this.baseURI = baseURI; this.username = username; this.password = password; this.ignoreSSLErrors = ignoreSSLErrors; } /** * see {@link #getBaseInputSource getBaseInputSource} WSDLLocator.getBaseInputSource */ public InputSource getBaseInputSource() { return getImportFromUrl(baseURI.toString()); } /** * Internal method to normalize the importUrl. The importLocation can be * relative to the parentLocation. * * @param parentLocation * @param importLocation * @return a url */ protected URL constructImportUrl(String parentLocation, String importLocation) { URL importUrl = null; try { URI importLocationURI = new URI(importLocation); if (importLocationURI.getScheme() != null || parentLocation == null) { importUrl = importLocationURI.toURL(); } else { String parentDir = parentLocation.substring(0, parentLocation.lastIndexOf("/")); URI uri = new URI(parentDir + "/" + importLocation); importUrl = uri.normalize().toURL(); } } catch (Exception e) { log.error(e.getMessage(), e); } if (importUrl != null) { log.debug("importUrl: " + importUrl.toExternalForm()); } else { log.error("importUrl is null!"); } return importUrl; } private InputSource getImportFromUrl(String url) { InputSource inputSource = null; DefaultHttpClient httpclient = null; try { URL url2 = new URL(url); if (!url.toLowerCase().startsWith("http")) { return getImportFromFile(url); } boolean usessl = false; int port = 80; if (url.toLowerCase().startsWith("https://")) { port = 443; usessl = true; } if (url2.getPort() > 0) { port = url2.getPort(); } if (ignoreSSLErrors && usessl) { SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("https", port, new MockSSLSocketFactory())); ClientConnectionManager cm = new BasicClientConnectionManager(schemeRegistry); httpclient = new DefaultHttpClient(cm); } else { httpclient = new DefaultHttpClient(); } if (username != null && username.length() > 0 && password != null && password.length() > 0) { httpclient.getCredentialsProvider().setCredentials(new AuthScope(url2.getHost(), port), new UsernamePasswordCredentials(username, password)); } HttpGet httpGet = new HttpGet(url); try { HttpResponse response1 = httpclient.execute(httpGet); //System.out.println(response1.getStatusLine()); // HttpEntity entity1 = response1.getEntity(); // do something useful with the response body // and ensure it is fully consumed ResponseHandler<String> responseHandler = new BasicResponseHandler(); String handleResponse = responseHandler.handleResponse(response1); StringReader sr = new StringReader(handleResponse); inputSource = new InputSource(sr); } finally { httpGet.releaseConnection(); } // InputStream inputStream = importUrl.openStream(); //inputSource = new InputSource(inputStream); latestImportURI = url; } catch (Exception e) { log.error(e.getMessage()); log.debug(e.getMessage(), e); lastException = e; } finally { if (httpclient != null) { httpclient.getConnectionManager().shutdown(); } } return inputSource; } /** * see also {@link #getImportInputSource WSDLLocator.getImportInputSource} * @param parentLocation * @param importLocation * @return input source */ public InputSource getImportInputSource(String parentLocation, String importLocation) { InputSource inputSource = null; try { URL importUrl = constructImportUrl(parentLocation, importLocation); return getImportFromUrl(importUrl.toExternalForm()); } catch (Exception e) { log.error(e.getMessage(), e); lastException = e; } return inputSource; } /** * see also {@link WSDLLocator#getBaseURI WSDLLocatorImpl.getBaseURI} * @return string */ public String getBaseURI() { String baseURIStr = null; try { baseURIStr = baseURI.toURL().toExternalForm(); } catch (IOException e) { log.error(e.getMessage()); log.debug(e.getMessage(), e); lastException = e; } return baseURIStr; } /** * {@link WSDLLocator#getLatestImportURI getLatestImportURI} * @return string */ public String getLatestImportURI() { return latestImportURI; } /** * {@link WSDLLocator#close close} */ public void close() { if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { log.error(e.getMessage(), e); lastException = e; } } } private InputSource getImportFromFile(String url) { InputSource inputSource = null; try { URL importUrl = new URL(url); inputStream = importUrl.openStream(); inputSource = new InputSource(inputStream); latestImportURI = importUrl.toExternalForm(); } catch (Exception e) { log.error(e.getMessage()); log.debug(e.getMessage(), e); lastException = e; } return inputSource; } /** * Returns the last exception or null if there wasn't any. This was done because the authors of WSDLLocator apparently thought it would always work * @return the last exception or null */ public Exception getLastException() { return lastException; } }