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.systest.provider;
019:
020: import java.io.ByteArrayOutputStream;
021: import java.io.IOException;
022: import java.io.InputStream;
023: import java.io.StringReader;
024: import java.util.ArrayList;
025: import java.util.HashMap;
026: import java.util.List;
027: import java.util.Map;
028:
029: import javax.activation.DataHandler;
030: import javax.annotation.Resource;
031: import javax.xml.transform.Transformer;
032: import javax.xml.transform.dom.DOMResult;
033: import javax.xml.transform.stream.StreamSource;
034: import javax.xml.ws.Provider;
035: import javax.xml.ws.Service;
036: import javax.xml.ws.ServiceMode;
037: import javax.xml.ws.WebServiceContext;
038: import javax.xml.ws.WebServiceProvider;
039: import javax.xml.ws.handler.MessageContext;
040:
041: import org.w3c.dom.Document;
042:
043: import org.apache.cxf.common.util.Base64Utility;
044: import org.apache.cxf.helpers.CastUtils;
045: import org.apache.cxf.helpers.IOUtils;
046: import org.apache.cxf.helpers.XMLUtils;
047: import org.apache.cxf.message.Message;
048:
049: @WebServiceProvider(serviceName="AttachmentStreamSourceXMLProvider")
050: @ServiceMode(value=Service.Mode.PAYLOAD)
051: @javax.xml.ws.BindingType(value="http://cxf.apache.org/bindings/xformat")
052: public class AttachmentStreamSourceXMLProvider implements
053: Provider<StreamSource> {
054:
055: @Resource
056: protected WebServiceContext wsContext;
057:
058: public StreamSource invoke(StreamSource source) {
059:
060: MessageContext mc = wsContext.getMessageContext();
061:
062: String httpMethod = (String) mc
063: .get(MessageContext.HTTP_REQUEST_METHOD);
064: if ("POST".equals(httpMethod)) {
065:
066: int count = 0;
067: // we really want to verify that a root part is a proper XML as expected
068: DOMResult result = new DOMResult();
069: try {
070: Transformer transformer = XMLUtils.newTransformer();
071: transformer.transform(source, result);
072: count = Integer.parseInt(((Document) result.getNode())
073: .getDocumentElement().getAttribute("count"));
074: } catch (Exception ex) {
075: // ignore
076: }
077:
078: Map<String, DataHandler> dataHandlers = CastUtils
079: .cast((Map) mc
080: .get(MessageContext.INBOUND_MESSAGE_ATTACHMENTS));
081: StringBuilder buf = new StringBuilder();
082: buf.append("<response>");
083: int i = 0;
084: for (Map.Entry<String, DataHandler> entry : dataHandlers
085: .entrySet()) {
086: if (i++ > count) {
087: break;
088: }
089: try {
090: InputStream is = entry.getValue().getInputStream();
091: ByteArrayOutputStream bous = new ByteArrayOutputStream();
092: IOUtils.copy(is, bous);
093:
094: buf.append("<att contentId=\"" + entry.getKey()
095: + "\">");
096: buf
097: .append(Base64Utility.encode(bous
098: .toByteArray()));
099: buf.append("</att>");
100:
101: } catch (IOException ioe) {
102: ioe.printStackTrace();
103: }
104: }
105: buf.append("</response>");
106:
107: Map<String, List<String>> respHeaders = CastUtils
108: .cast((Map<?, ?>) mc
109: .get(MessageContext.HTTP_RESPONSE_HEADERS));
110: if (respHeaders == null) {
111: respHeaders = new HashMap<String, List<String>>();
112: mc.put(MessageContext.HTTP_RESPONSE_HEADERS,
113: respHeaders);
114: }
115:
116: List<String> contentTypeValues = new ArrayList<String>();
117: contentTypeValues.add("application/xml+custom");
118: respHeaders.put(Message.CONTENT_TYPE, contentTypeValues);
119:
120: return new StreamSource(new StringReader(buf.toString()));
121: }
122: return source;
123:
124: }
125:
126: }
|