001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.binding.http;
019:
020: import java.lang.reflect.Method;
021: import java.util.ArrayList;
022: import java.util.List;
023:
024: import javax.xml.namespace.QName;
025:
026: import org.apache.cxf.binding.AbstractBindingFactory;
027: import org.apache.cxf.binding.Binding;
028: import org.apache.cxf.binding.http.interceptor.ContentTypeOutInterceptor;
029: import org.apache.cxf.binding.http.interceptor.DatabindingInSetupInterceptor;
030: import org.apache.cxf.binding.http.interceptor.DatabindingOutSetupInterceptor;
031: import org.apache.cxf.binding.http.strategy.ConventionStrategy;
032: import org.apache.cxf.binding.http.strategy.JRAStrategy;
033: import org.apache.cxf.binding.http.strategy.ResourceStrategy;
034: import org.apache.cxf.binding.xml.XMLBinding;
035: import org.apache.cxf.binding.xml.interceptor.XMLFaultInInterceptor;
036: import org.apache.cxf.binding.xml.interceptor.XMLFaultOutInterceptor;
037: import org.apache.cxf.frontend.MethodDispatcher;
038: import org.apache.cxf.interceptor.AttachmentInInterceptor;
039: import org.apache.cxf.interceptor.AttachmentOutInterceptor;
040: import org.apache.cxf.interceptor.StaxOutInterceptor;
041: import org.apache.cxf.service.Service;
042: import org.apache.cxf.service.factory.ReflectionServiceFactoryBean;
043: import org.apache.cxf.service.factory.ServiceConstructionException;
044: import org.apache.cxf.service.model.BindingInfo;
045: import org.apache.cxf.service.model.BindingOperationInfo;
046: import org.apache.cxf.service.model.OperationInfo;
047: import org.apache.cxf.service.model.ServiceInfo;
048:
049: public class HttpBindingFactory extends AbstractBindingFactory {
050:
051: public static final String HTTP_BINDING_ID = "http://apache.org/cxf/binding/http";
052: private List<ResourceStrategy> strategies = new ArrayList<ResourceStrategy>();
053:
054: public HttpBindingFactory() {
055: strategies.add(new JRAStrategy());
056: strategies.add(new ConventionStrategy());
057: }
058:
059: public Binding createBinding(BindingInfo bi) {
060: XMLBinding binding = new XMLBinding(bi);
061:
062: binding.getInInterceptors().add(new AttachmentInInterceptor());
063: binding.getInInterceptors().add(
064: new DatabindingInSetupInterceptor());
065:
066: binding.getOutInterceptors()
067: .add(new AttachmentOutInterceptor());
068: binding.getOutInterceptors().add(
069: new ContentTypeOutInterceptor());
070:
071: binding.getOutInterceptors().add(
072: new DatabindingOutSetupInterceptor());
073:
074: binding.getInFaultInterceptors().add(
075: new XMLFaultInInterceptor());
076:
077: binding.getOutFaultInterceptors().add(
078: new ContentTypeOutInterceptor());
079: binding.getOutFaultInterceptors().add(new StaxOutInterceptor());
080: binding.getOutFaultInterceptors().add(
081: new XMLFaultOutInterceptor());
082:
083: return binding;
084: }
085:
086: public BindingInfo createBindingInfo(Service service,
087: String namespace, Object obj) {
088: URIMapper mapper = new URIMapper();
089:
090: ServiceInfo si = service.getServiceInfos().get(0);
091: BindingInfo info = new BindingInfo(si,
092: HttpBindingFactory.HTTP_BINDING_ID);
093: info.setName(new QName(si.getName().getNamespaceURI(), si
094: .getName().getLocalPart()
095: + "HttpBinding"));
096:
097: service.put(URIMapper.class.getName(), mapper);
098: MethodDispatcher md = (MethodDispatcher) service
099: .get(MethodDispatcher.class.getName());
100:
101: for (OperationInfo o : si.getInterface().getOperations()) {
102: BindingOperationInfo bop = info.buildOperation(o.getName(),
103: o.getInputName(), o.getOutputName());
104:
105: info.addOperation(bop);
106:
107: Method m = md.getMethod(bop);
108:
109: try {
110: Class c = (Class) service
111: .get(ReflectionServiceFactoryBean.ENDPOINT_CLASS);
112: if (c != null) {
113: m = c.getMethod(m.getName(), m.getParameterTypes());
114: }
115: } catch (SecurityException e) {
116: throw new ServiceConstructionException(e);
117: } catch (NoSuchMethodException e) {
118: throw new ServiceConstructionException(e);
119: }
120:
121: // attempt to map the method to a resource using different strategies
122: for (ResourceStrategy s : strategies) {
123: // Try different ones until we find one that succeeds
124: if (s.map(bop, m, mapper)) {
125: break;
126: }
127: }
128: }
129:
130: return info;
131: }
132:
133: public List<ResourceStrategy> getStrategies() {
134: return strategies;
135: }
136:
137: public void setStrategies(List<ResourceStrategy> strategies) {
138: this.strategies = strategies;
139: }
140:
141: }
|