001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.xml.ws.message;
038:
039: import com.sun.xml.ws.api.message.AttachmentSet;
040: import com.sun.xml.ws.api.message.Attachment;
041: import com.sun.xml.ws.encoding.MimeMultipartParser;
042: import com.sun.xml.ws.resources.EncodingMessages;
043: import com.sun.istack.Nullable;
044:
045: import javax.xml.ws.WebServiceException;
046: import java.util.Iterator;
047: import java.util.Map;
048: import java.util.HashMap;
049: import java.io.IOException;
050:
051: /**
052: * {@link AttachmentSet} backed by {@link com.sun.xml.ws.encoding.MimeMultipartParser}
053: *
054: * @author Vivek Pandey
055: */
056: public final class MimeAttachmentSet implements AttachmentSet {
057: private final MimeMultipartParser mpp;
058: private Map<String, Attachment> atts = new HashMap<String, Attachment>();
059:
060: public MimeAttachmentSet(MimeMultipartParser mpp) {
061: this .mpp = mpp;
062: }
063:
064: @Nullable
065: public Attachment get(String contentId) {
066: Attachment att;
067: /**
068: * First try to get the Attachment from internal map, maybe this attachment
069: * is added by the user.
070: */
071: att = atts.get(contentId);
072: if (att != null)
073: return att;
074: try {
075: /**
076: * Attachment is not found in the internal map, now do look in
077: * the mpp, if found add to the internal Attachment map.
078: */
079: att = mpp.getAttachmentPart(contentId);
080: if (att != null) {
081: atts.put(contentId, att);
082: }
083: } catch (IOException e) {
084: throw new WebServiceException(EncodingMessages
085: .NO_SUCH_CONTENT_ID(contentId), e);
086: }
087: return att;
088: }
089:
090: /**
091: * This is expensive operation, its going to to read all the underlying
092: * attachments in {@link MimeMultipartParser}.
093: */
094: public boolean isEmpty() {
095: return atts.size() <= 0 && mpp.getAttachmentParts().isEmpty();
096: }
097:
098: public void add(Attachment att) {
099: atts.put(att.getContentId(), att);
100: }
101:
102: /**
103: * Expensive operation.
104: */
105: public Iterator<Attachment> iterator() {
106: /**
107: * Browse thru all the attachments in the mpp, add them to #atts,
108: * then return whether its empty.
109: */
110: Map<String, Attachment> attachments = mpp.getAttachmentParts();
111: for (Map.Entry<String, Attachment> att : attachments.entrySet()) {
112: if (atts.get(att.getKey()) == null) {
113: atts.put(att.getKey(), att.getValue());
114: }
115: }
116:
117: return atts.values().iterator();
118: }
119: }
|