View Javadoc
1   /*
2    * Copyright 2017 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  package org.openehealth.ipf.commons.ihe.ws.cxf;
17  
18  import javax.activation.DataHandler;
19  import javax.xml.bind.attachment.AttachmentMarshaller;
20  
21  /**
22   * An attachment marshaller implementation which does not read any data
23   * from the provided data handlers in order to keep all streams usable.
24   *
25   * @author Dmytro Rud
26   */
27  public class NonReadingAttachmentMarshaller extends AttachmentMarshaller {
28      @Override
29      public boolean isXOPPackage() {
30          return true;
31      }
32  
33      @Override
34      public String addMtomAttachment(DataHandler data, String elementNamespace, String elementLocalName) {
35          return attachmentDescription(data.getName(), null, data.getContentType());
36      }
37  
38      @Override
39      public String addMtomAttachment(byte[] data, int offset, int length, String mimeType,
40                                      String elementNamespace, String elementLocalName)
41      {
42          String size = Integer.toString(Math.min(length, data.length - offset));
43          return attachmentDescription(null, size, mimeType);
44      }
45  
46      @Override
47      public String addSwaRefAttachment(DataHandler data) {
48          return attachmentDescription(data.getName(), null, data.getContentType());
49      }
50  
51      private static String attachmentDescription(String name, String size, String contentType) {
52          return "Attachment: name='" + name != null ? name : "[unknown]" +
53                  "', size='" + size != null ? size : "[unknown]" +
54                  "', content type='" + contentType != null ? contentType : "[unknown]" + '\'';
55      }
56  
57  }