1 /* 2 * Copyright 2005 the original author or authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package org.springframework.ws.server.endpoint.mapping; 18 19 import org.springframework.context.support.ApplicationObjectSupport; 20 import org.springframework.core.Ordered; 21 import org.springframework.ws.context.MessageContext; 22 import org.springframework.ws.server.EndpointInterceptor; 23 import org.springframework.ws.server.EndpointInvocationChain; 24 import org.springframework.ws.server.EndpointMapping; 25 26 /** 27 * Abstract base class for EndpointMapping implementations. Supports a default endpoint, and endpoint interceptors. 28 * 29 * @author Arjen Poutsma 30 * @see #getEndpointInternal(org.springframework.ws.context.MessageContext) 31 * @see org.springframework.ws.server.EndpointInterceptor 32 * @since 1.0.0 33 */ 34 public abstract class AbstractEndpointMapping extends ApplicationObjectSupport implements EndpointMapping, Ordered { 35 36 private int order = Integer.MAX_VALUE; // default: same as non-Ordered 37 38 private Object defaultEndpoint; 39 40 private EndpointInterceptor[] interceptors; 41 42 /** 43 * Returns the the endpoint interceptors to apply to all endpoints mapped by this endpoint mapping. 44 * 45 * @return array of endpoint interceptors, or <code>null</code> if none 46 */ 47 public EndpointInterceptor[] getInterceptors() { 48 return interceptors; 49 } 50 51 /** 52 * Sets the endpoint interceptors to apply to all endpoints mapped by this endpoint mapping. 53 * 54 * @param interceptors array of endpoint interceptors, or <code>null</code> if none 55 */ 56 public final void setInterceptors(EndpointInterceptor[] interceptors) { 57 this.interceptors = interceptors; 58 } 59 60 public final int getOrder() { 61 return order; 62 } 63 64 /** 65 * Specify the order value for this mapping. 66 * <p/> 67 * Default value is {@link Integer#MAX_VALUE}, meaning that it's non-ordered. 68 * 69 * @see org.springframework.core.Ordered#getOrder() 70 */ 71 public final void setOrder(int order) { 72 this.order = order; 73 } 74 75 /** 76 * Look up an endpoint for the given message context, falling back to the default endpoint if no specific one is 77 * found. 78 * 79 * @return the looked up endpoint instance, or the default endpoint 80 * @see #getEndpointInternal(org.springframework.ws.context.MessageContext) 81 */ 82 public final EndpointInvocationChain getEndpoint(MessageContext messageContext) throws Exception { 83 Object endpoint = getEndpointInternal(messageContext); 84 if (endpoint == null) { 85 endpoint = defaultEndpoint; 86 } 87 if (endpoint == null) { 88 return null; 89 } 90 if (endpoint instanceof String) { 91 String endpointName = (String) endpoint; 92 endpoint = resolveStringEndpoint(endpointName); 93 if (endpoint == null) { 94 return null; 95 } 96 } 97 return createEndpointInvocationChain(messageContext, endpoint, interceptors); 98 } 99 100 /** 101 * Creates a new <code>EndpointInvocationChain</code> based on the given message context, endpoint, and 102 * interceptors. Default implementation creates a simple <code>EndpointInvocationChain</code> based on the set 103 * interceptors. 104 * 105 * @param endpoint the endpoint 106 * @param interceptors the endpoint interceptors 107 * @return the created invocation chain 108 * @see #setInterceptors(org.springframework.ws.server.EndpointInterceptor[]) 109 */ 110 protected EndpointInvocationChain createEndpointInvocationChain(MessageContext messageContext, 111 Object endpoint, 112 EndpointInterceptor[] interceptors) { 113 return new EndpointInvocationChain(endpoint, interceptors); 114 } 115 116 /** 117 * Returns the default endpoint for this endpoint mapping. 118 * 119 * @return the default endpoint mapping, or null if none 120 */ 121 protected final Object getDefaultEndpoint() { 122 return defaultEndpoint; 123 } 124 125 /** 126 * Sets the default endpoint for this endpoint mapping. This endpoint will be returned if no specific mapping was 127 * found. 128 * <p/> 129 * Default is <code>null</code>, indicating no default endpoint. 130 * 131 * @param defaultEndpoint the default endpoint, or null if none 132 */ 133 public final void setDefaultEndpoint(Object defaultEndpoint) { 134 this.defaultEndpoint = defaultEndpoint; 135 } 136 137 /** 138 * Resolves an endpoint string. If the given string can is a bean name, it is resolved using the application 139 * context. 140 * 141 * @param endpointName the endpoint name 142 * @return the resolved enpoint, or <code>null</code> if the name could not be resolved 143 */ 144 protected Object resolveStringEndpoint(String endpointName) { 145 if (getApplicationContext().containsBean(endpointName)) { 146 return getApplicationContext().getBean(endpointName); 147 } 148 else { 149 return null; 150 } 151 } 152 153 /** 154 * Lookup an endpoint for the given request, returning <code>null</code> if no specific one is found. This template 155 * method is called by getEndpoint, a <code>null</code> return value will lead to the default handler, if one is 156 * set. 157 * <p/> 158 * The returned endpoint can be a string, in which case it is resolved as a bean name. Also, it can take the form 159 * <code>beanName#method</code>, in which case the method is resolved. 160 * 161 * @return the looked up endpoint instance, or null 162 * @throws Exception if there is an error 163 */ 164 protected abstract Object getEndpointInternal(MessageContext messageContext) throws Exception; 165 }