Thu, 31 Aug 2017 15:18:52 +0800
merge
1 /*
2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
26 package com.sun.xml.internal.ws.message;
28 import com.sun.xml.internal.ws.api.message.AttachmentSet;
29 import com.sun.xml.internal.ws.api.message.Attachment;
30 import com.sun.xml.internal.ws.encoding.MimeMultipartParser;
31 import com.sun.xml.internal.ws.resources.EncodingMessages;
32 import com.sun.istack.internal.Nullable;
34 import javax.xml.ws.WebServiceException;
35 import java.util.Iterator;
36 import java.util.Map;
37 import java.util.HashMap;
38 import java.io.IOException;
40 /**
41 * {@link AttachmentSet} backed by {@link com.sun.xml.internal.ws.encoding.MimeMultipartParser}
42 *
43 * @author Vivek Pandey
44 */
45 public final class MimeAttachmentSet implements AttachmentSet {
46 private final MimeMultipartParser mpp;
47 private Map<String, Attachment> atts = new HashMap<String, Attachment>();
50 public MimeAttachmentSet(MimeMultipartParser mpp) {
51 this.mpp = mpp;
52 }
54 @Nullable
55 public Attachment get(String contentId) {
56 Attachment att;
57 /**
58 * First try to get the Attachment from internal map, maybe this attachment
59 * is added by the user.
60 */
61 att = atts.get(contentId);
62 if(att != null)
63 return att;
64 try {
65 /**
66 * Attachment is not found in the internal map, now do look in
67 * the mpp, if found add to the internal Attachment map.
68 */
69 att = mpp.getAttachmentPart(contentId);
70 if(att != null){
71 atts.put(contentId, att);
72 }
73 } catch (IOException e) {
74 throw new WebServiceException(EncodingMessages.NO_SUCH_CONTENT_ID(contentId), e);
75 }
76 return att;
77 }
79 /**
80 * This is expensive operation, its going to to read all the underlying
81 * attachments in {@link MimeMultipartParser}.
82 */
83 public boolean isEmpty() {
84 return atts.size() <= 0 && mpp.getAttachmentParts().isEmpty();
85 }
87 public void add(Attachment att) {
88 atts.put(att.getContentId(), att);
89 }
91 /**
92 * Expensive operation.
93 */
94 public Iterator<Attachment> iterator() {
95 /**
96 * Browse thru all the attachments in the mpp, add them to #atts,
97 * then return whether its empty.
98 */
99 Map<String, Attachment> attachments = mpp.getAttachmentParts();
100 for(Map.Entry<String, Attachment> att : attachments.entrySet()) {
101 if(atts.get(att.getKey()) == null){
102 atts.put(att.getKey(), att.getValue());
103 }
104 }
106 return atts.values().iterator();
107 }
108 }