Author: supun Date: Tue Aug 2 08:48:23 2011 New Revision: 1153064 URL: http://svn.apache.org/viewvc?rev=1153064&view=rev Log: adding support for url endpoints
Added: axis/axis2/java/transports/trunk/modules/base/src/main/java/org/apache/axis2/transport/base/endpoint/ axis/axis2/java/transports/trunk/modules/base/src/main/java/org/apache/axis2/transport/base/endpoint/URLEndpoint.java axis/axis2/java/transports/trunk/modules/base/src/main/java/org/apache/axis2/transport/base/endpoint/URLEndpointsConfiguration.java axis/axis2/java/transports/trunk/modules/base/src/main/java/org/apache/axis2/transport/base/endpoint/config/ axis/axis2/java/transports/trunk/modules/base/src/main/java/org/apache/axis2/transport/base/endpoint/config/URLEndpointFactory.java axis/axis2/java/transports/trunk/modules/base/src/main/java/org/apache/axis2/transport/base/endpoint/config/URLEndpointsConfigurationFactory.java Added: axis/axis2/java/transports/trunk/modules/base/src/main/java/org/apache/axis2/transport/base/endpoint/URLEndpoint.java URL: http://svn.apache.org/viewvc/axis/axis2/java/transports/trunk/modules/base/src/main/java/org/apache/axis2/transport/base/endpoint/URLEndpoint.java?rev=1153064&view=auto ============================================================================== --- axis/axis2/java/transports/trunk/modules/base/src/main/java/org/apache/axis2/transport/base/endpoint/URLEndpoint.java (added) +++ axis/axis2/java/transports/trunk/modules/base/src/main/java/org/apache/axis2/transport/base/endpoint/URLEndpoint.java Tue Aug 2 08:48:23 2011 @@ -0,0 +1,91 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.axis2.transport.base.endpoint; + +import org.apache.axis2.builder.Builder; +import org.apache.axis2.builder.SOAPBuilder; +import org.apache.axis2.context.MessageContext; +import org.apache.axis2.description.Parameter; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class URLEndpoint { + private Pattern urlPattern; + + private Map<String, Builder> messageBuilders = new HashMap<String, Builder>(); + + private List<Parameter> parameters = new ArrayList<Parameter>(); + + private Builder defaultBuilder = new SOAPBuilder(); + + public URLEndpoint(Pattern urlPattern) { + this.urlPattern = urlPattern; + } + + public boolean isMatching(String url) { + Matcher matcher = urlPattern.matcher(url); + return matcher.matches(); + } + + public void addBuilder(String name, Builder builder) { + messageBuilders.put(name, builder); + } + + public void addParameter(Parameter parameter) { + parameters.add(parameter); + } + + public Parameter getParameter(String name) { + for (Parameter p : parameters) { + if (p.getName().equals(name)) { + return p; + } + } + return null; + } + + public List<Parameter> getParameters() { + return parameters; + } + + public void setDefaultBuilder(Builder defaultBuilder) { + this.defaultBuilder = defaultBuilder; + } + + public Builder getBuilder(String contentType) { + Builder builder = messageBuilders.get(contentType); + if (builder == null) { + return defaultBuilder; + } + + return builder; + } + + public void setParameters(MessageContext msgCtx) { + for (Parameter p : parameters) { + msgCtx.setProperty(p.getName(), p.getValue()); + } + } +} Added: axis/axis2/java/transports/trunk/modules/base/src/main/java/org/apache/axis2/transport/base/endpoint/URLEndpointsConfiguration.java URL: http://svn.apache.org/viewvc/axis/axis2/java/transports/trunk/modules/base/src/main/java/org/apache/axis2/transport/base/endpoint/URLEndpointsConfiguration.java?rev=1153064&view=auto ============================================================================== --- axis/axis2/java/transports/trunk/modules/base/src/main/java/org/apache/axis2/transport/base/endpoint/URLEndpointsConfiguration.java (added) +++ axis/axis2/java/transports/trunk/modules/base/src/main/java/org/apache/axis2/transport/base/endpoint/URLEndpointsConfiguration.java Tue Aug 2 08:48:23 2011 @@ -0,0 +1,57 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.axis2.transport.base.endpoint; + +import java.util.ArrayList; +import java.util.List; + +public class URLEndpointsConfiguration { + public static final String URL_PATTERN = "urlPattern"; + public static final String MESSAGE_BUILDERS = "messageBuilders"; + public static final String MESSAGE_BUILDER = "messageBuilder"; + public static final String CONTENT_TYPE = "contentType"; + public static final String CLASS = "class"; + public static final String PARAMETER = "parameter"; + public static final String NAME = "name"; + public static final String ENDPOINT = "endpoint"; + private List<URLEndpoint> endpoints = new ArrayList<URLEndpoint>(); + + /** + * Return the endpoint matching the given URL. + * @param url url of the request + * @return the endpoint matching the given url + */ + public URLEndpoint getEndpoint(String url) { + for (URLEndpoint epr : endpoints) { + if (epr.isMatching(url)) { + return epr; + } + } + + return null; + } + + public List<URLEndpoint> getEndpoints() { + return endpoints; + } + + public void addEndpoint(URLEndpoint endpoint) { + endpoints.add(endpoint); + } +} Added: axis/axis2/java/transports/trunk/modules/base/src/main/java/org/apache/axis2/transport/base/endpoint/config/URLEndpointFactory.java URL: http://svn.apache.org/viewvc/axis/axis2/java/transports/trunk/modules/base/src/main/java/org/apache/axis2/transport/base/endpoint/config/URLEndpointFactory.java?rev=1153064&view=auto ============================================================================== --- axis/axis2/java/transports/trunk/modules/base/src/main/java/org/apache/axis2/transport/base/endpoint/config/URLEndpointFactory.java (added) +++ axis/axis2/java/transports/trunk/modules/base/src/main/java/org/apache/axis2/transport/base/endpoint/config/URLEndpointFactory.java Tue Aug 2 08:48:23 2011 @@ -0,0 +1,140 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.axis2.transport.base.endpoint.config; + +import org.apache.axiom.om.OMAttribute; +import org.apache.axiom.om.OMElement; +import org.apache.axis2.AxisFault; +import org.apache.axis2.builder.Builder; +import org.apache.axis2.description.Parameter; +import org.apache.axis2.transport.base.endpoint.URLEndpoint; +import org.apache.axis2.transport.base.endpoint.URLEndpointsConfiguration; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import javax.xml.namespace.QName; +import java.util.Iterator; +import java.util.regex.Pattern; + +public class URLEndpointFactory { + private static final Log log = LogFactory.getLog(URLEndpointFactory.class); + + public URLEndpoint create(OMElement xml) throws AxisFault { + OMAttribute urlPatternAttr = xml.getAttribute(new QName(URLEndpointsConfiguration.URL_PATTERN)); + if (urlPatternAttr == null) { + handleException(URLEndpointsConfiguration.URL_PATTERN + + " attribute is mandory for an URLEndpoint configuration"); + return null; + } + + String pattern = urlPatternAttr.getAttributeValue(); + URLEndpoint endpoint = new URLEndpoint(Pattern.compile(pattern)); + + OMElement messageBuilders = xml.getFirstChildWithName( + new QName(URLEndpointsConfiguration.MESSAGE_BUILDERS)); + + if (messageBuilders != null) { + OMAttribute defaultBuilderAttr = messageBuilders.getAttribute( + new QName("defaultBuilder")); + if (defaultBuilderAttr != null) { + Builder builder = loadBuilder(defaultBuilderAttr.getAttributeValue()); + if (builder != null) { + endpoint.setDefaultBuilder(builder); + } + } + + Iterator it = messageBuilders.getChildrenWithName( + new QName(URLEndpointsConfiguration.MESSAGE_BUILDER)); + while(it.hasNext()) { + OMElement builderElement = (OMElement) it.next(); + + OMAttribute contentTypeAttr = builderElement.getAttribute( + new QName(URLEndpointsConfiguration.CONTENT_TYPE)); + if (contentTypeAttr == null) { + handleException(URLEndpointsConfiguration.CONTENT_TYPE + + " attribute cannot be null for URLEndpoint " + + "with the " + URLEndpointsConfiguration.URL_PATTERN + " : " + pattern); + } + + OMAttribute classAttr = builderElement.getAttribute( + new QName(URLEndpointsConfiguration.CLASS)); + if (classAttr == null) { + handleException(URLEndpointsConfiguration.CLASS + + " attribute cannot be null for URLEndpoint " + + "with the " + URLEndpointsConfiguration.URL_PATTERN + " : " + pattern); + } + + if (classAttr != null && contentTypeAttr != null) { + Builder builder = loadBuilder(classAttr.getAttributeValue()); + if (builder != null) { + endpoint.addBuilder(contentTypeAttr.getAttributeValue(), builder); + } + } + } + } + + Iterator paramItr = xml.getChildrenWithName( + new QName(URLEndpointsConfiguration.PARAMETER)); + while (paramItr.hasNext()) { + OMElement p = (OMElement) paramItr.next(); + OMAttribute paramNameAttr = p.getAttribute(new QName(URLEndpointsConfiguration.NAME)); + if (paramNameAttr == null) { + handleException("Parameter " + URLEndpointsConfiguration.NAME + " cannot be null"); + } else { + endpoint.addParameter(new Parameter(paramNameAttr.getAttributeValue(), p.getText())); + } + } + + return endpoint; + } + + private Builder loadBuilder(String name) throws AxisFault { + try { + if (name != null) { + Class c = Class.forName(name); + Object o = c.newInstance(); + if (o instanceof Builder) { + return (Builder) o; + } else { + handleException("Class : " + name + + " should be a Builder"); + } + } + } catch (ClassNotFoundException e) { + handleException("Error creating builder: " + name, e); + } catch (InstantiationException e) { + handleException("Error initializing builder: " + name, e); + } catch (IllegalAccessException e) { + handleException("Error initializing builder: " + name, e); + } + + return null; + } + + private void handleException(String msg) throws AxisFault { + log.error(msg); + throw new AxisFault(msg); + } + + private void handleException(String msg, Exception e) throws AxisFault { + log.error(msg, e); + throw new AxisFault(msg, e); + } +} Added: axis/axis2/java/transports/trunk/modules/base/src/main/java/org/apache/axis2/transport/base/endpoint/config/URLEndpointsConfigurationFactory.java URL: http://svn.apache.org/viewvc/axis/axis2/java/transports/trunk/modules/base/src/main/java/org/apache/axis2/transport/base/endpoint/config/URLEndpointsConfigurationFactory.java?rev=1153064&view=auto ============================================================================== --- axis/axis2/java/transports/trunk/modules/base/src/main/java/org/apache/axis2/transport/base/endpoint/config/URLEndpointsConfigurationFactory.java (added) +++ axis/axis2/java/transports/trunk/modules/base/src/main/java/org/apache/axis2/transport/base/endpoint/config/URLEndpointsConfigurationFactory.java Tue Aug 2 08:48:23 2011 @@ -0,0 +1,83 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.axis2.transport.base.endpoint.config; + +import org.apache.axiom.om.OMElement; +import org.apache.axiom.om.OMXMLBuilderFactory; +import org.apache.axis2.AxisFault; +import org.apache.axis2.transport.base.endpoint.URLEndpoint; +import org.apache.axis2.transport.base.endpoint.URLEndpointsConfiguration; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import javax.xml.namespace.QName; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.util.Iterator; + +public class URLEndpointsConfigurationFactory { + private static final Log log = LogFactory.getLog(URLEndpointsConfigurationFactory.class); + + public URLEndpointsConfiguration create(OMElement element) throws AxisFault { + Iterator iterator = element.getChildrenWithName(new QName(URLEndpointsConfiguration.ENDPOINT)); + URLEndpointsConfiguration configuration = new URLEndpointsConfiguration(); + URLEndpointFactory fac = new URLEndpointFactory(); + while (iterator.hasNext()) { + OMElement endpoint = (OMElement) iterator.next(); + + URLEndpoint epr = fac.create(endpoint); + configuration.addEndpoint(epr); + } + + return configuration; + } + + public URLEndpointsConfiguration create(String fileName) throws AxisFault { + File synapseConfigLocation = new File(fileName); + + FileInputStream is = null; + try { + is = new FileInputStream(synapseConfigLocation); + } catch (FileNotFoundException e) { + handleException("Error reading file: " + fileName + "for creating the " + + URLEndpointsConfiguration.ENDPOINT + " configurations"); + } + OMElement element = OMXMLBuilderFactory.createOMBuilder(is).getDocumentElement(); + element.build(); + + Iterator iterator = element.getChildrenWithName(new QName(URLEndpointsConfiguration.ENDPOINT)); + URLEndpointsConfiguration configuration = new URLEndpointsConfiguration(); + URLEndpointFactory fac = new URLEndpointFactory(); + while (iterator.hasNext()) { + OMElement endpoint = (OMElement) iterator.next(); + + URLEndpoint epr = fac.create(endpoint); + configuration.addEndpoint(epr); + } + + return configuration; + } + + private void handleException(String msg) throws AxisFault { + log.error(msg); + throw new AxisFault(msg); + } +}