001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.servicemix.saxon;
018:
019: import java.io.File;
020: import java.io.IOException;
021: import java.net.URI;
022: import java.net.URL;
023: import java.util.Iterator;
024: import java.util.Map;
025:
026: import javax.jbi.messaging.MessageExchange;
027: import javax.jbi.messaging.NormalizedMessage;
028: import javax.xml.parsers.DocumentBuilder;
029:
030: import org.w3c.dom.Document;
031:
032: import net.sf.saxon.Configuration;
033:
034: import org.apache.servicemix.common.endpoints.ProviderEndpoint;
035: import org.apache.servicemix.expression.Expression;
036: import org.apache.servicemix.jbi.jaxp.SourceTransformer;
037: import org.springframework.core.io.DefaultResourceLoader;
038: import org.springframework.core.io.FileSystemResource;
039: import org.springframework.core.io.Resource;
040: import org.springframework.core.io.UrlResource;
041:
042: public abstract class SaxonEndpoint extends ProviderEndpoint {
043:
044: public static final String RESULT_BYTES = "bytes";
045: public static final String RESULT_STRING = "string";
046: public static final String RESULT_DOM = "dom";
047:
048: private Configuration configuration;
049: private boolean copyProperties = true;
050: private boolean copyAttachments = true;
051: private boolean copySubject = true;
052: private String result = RESULT_DOM;
053: private Resource resource;
054: private Expression expression;
055: private Resource wsdlResource;
056: private SourceTransformer sourceTransformer = new SourceTransformer();
057: private Map parameters;
058:
059: /**
060: * @param sourceTransformer the sourceTransformer to set
061: */
062: protected void setSourceTransformer(
063: SourceTransformer sourceTransformer) {
064: this .sourceTransformer = sourceTransformer;
065: }
066:
067: /**
068: * @return the sourceTransformer
069: */
070: public SourceTransformer getSourceTransformer() {
071: return sourceTransformer;
072: }
073:
074: /**
075: * @return the wsdlResource
076: */
077: public Resource getWsdlResource() {
078: return wsdlResource;
079: }
080:
081: /**
082: * @param wsdlResource the wsdlResource to set
083: */
084: public void setWsdlResource(Resource wsdlResource) {
085: this .wsdlResource = wsdlResource;
086: }
087:
088: /**
089: * @return the expression
090: */
091: public Expression getExpression() {
092: return expression;
093: }
094:
095: /**
096: * @param expression the expression to set
097: */
098: public void setExpression(Expression expression) {
099: this .expression = expression;
100: }
101:
102: /**
103: * @return the resource
104: */
105: public Resource getResource() {
106: return resource;
107: }
108:
109: /**
110: * @param resource the resource to set
111: */
112: public void setResource(Resource resource) {
113: this .resource = resource;
114: }
115:
116: /**
117: * @return the result
118: */
119: public String getResult() {
120: return result;
121: }
122:
123: /**
124: * @param result the result to set
125: */
126: public void setResult(String result) {
127: this .result = result;
128: }
129:
130: /**
131: * @return the configuration
132: */
133: public Configuration getConfiguration() {
134: return configuration;
135: }
136:
137: /**
138: * @param configuration the configuration to set
139: */
140: public void setConfiguration(Configuration configuration) {
141: this .configuration = configuration;
142: }
143:
144: /**
145: * @return the copyAttachments
146: */
147: public boolean isCopyAttachments() {
148: return copyAttachments;
149: }
150:
151: /**
152: * @param copyAttachments the copyAttachments to set
153: */
154: public void setCopyAttachments(boolean copyAttachments) {
155: this .copyAttachments = copyAttachments;
156: }
157:
158: /**
159: * @return the copyProperties
160: */
161: public boolean isCopyProperties() {
162: return copyProperties;
163: }
164:
165: /**
166: * @param copyProperties the copyProperties to set
167: */
168: public void setCopyProperties(boolean copyProperties) {
169: this .copyProperties = copyProperties;
170: }
171:
172: /**
173: * @return the copySubject
174: */
175: public boolean isCopySubject() {
176: return copySubject;
177: }
178:
179: /**
180: * @param copySubject the copySubject to set
181: */
182: public void setCopySubject(boolean copySubject) {
183: this .copySubject = copySubject;
184: }
185:
186: /**
187: * @return the parameters
188: */
189: public Map getParameters() {
190: return parameters;
191: }
192:
193: /**
194: * @param parameters the parameters to set
195: */
196: public void setParameters(Map parameters) {
197: this .parameters = parameters;
198: }
199:
200: // Interface methods
201: // -------------------------------------------------------------------------
202:
203: public void activate() throws Exception {
204: if (wsdlResource != null) {
205: setDescription(parse(wsdlResource));
206: }
207: super .activate();
208: }
209:
210: // Implementation methods
211: // -------------------------------------------------------------------------
212:
213: /**
214: * Transform the content and send it back
215: */
216: protected void processInOut(MessageExchange exchange,
217: NormalizedMessage in, NormalizedMessage out)
218: throws Exception {
219: copyPropertiesAndAttachments(in, out);
220: transform(exchange, in, out);
221: }
222:
223: /**
224: * If enabled the properties and attachments are copied to the destination message
225: */
226: protected void copyPropertiesAndAttachments(
227: NormalizedMessage source, NormalizedMessage dest)
228: throws Exception {
229: if (isCopyProperties()) {
230: for (Iterator it = source.getPropertyNames().iterator(); it
231: .hasNext();) {
232: String name = (String) it.next();
233: dest.setProperty(name, source.getProperty(name));
234: }
235: }
236: if (isCopyAttachments()) {
237: for (Iterator it = source.getAttachmentNames().iterator(); it
238: .hasNext();) {
239: String name = (String) it.next();
240: dest.addAttachment(name, source.getAttachment(name));
241: }
242: }
243: if (isCopySubject()) {
244: dest.setSecuritySubject(source.getSecuritySubject());
245: }
246: }
247:
248: protected abstract void transform(MessageExchange exchange,
249: NormalizedMessage in, NormalizedMessage out)
250: throws Exception;
251:
252: protected Resource getDynamicResource(MessageExchange exchange,
253: NormalizedMessage in) throws Exception {
254: Object res = getExpression().evaluate(exchange, in);
255: if (res == null) {
256: return null;
257: }
258: if (res instanceof Resource) {
259: return (Resource) res;
260: }
261: if (res instanceof File) {
262: return new FileSystemResource((File) res);
263: }
264: if (res instanceof URL) {
265: return new UrlResource((URL) res);
266: }
267: if (res instanceof URI) {
268: return new UrlResource(((URI) res).toURL());
269: }
270: return new DefaultResourceLoader().getResource(res.toString());
271: }
272:
273: protected Document parse(Resource res) throws Exception {
274: URL url = null;
275: try {
276: url = res.getURL();
277: } catch (IOException e) {
278: // Ignore
279: }
280: DocumentBuilder builder = sourceTransformer
281: .createDocumentBuilder();
282: return builder.parse(res.getInputStream(), url != null ? url
283: .toExternalForm() : null);
284: }
285:
286: }
|