📄 attachmentsimpl.java
字号:
/* * Copyright 2001-2004 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *//* @author Rob Jellinghaus (robj@unrealities.com) *//* @author Rick Rineholt */package org.apache.axis.attachments;import org.apache.axis.AxisFault;import org.apache.axis.Part;import org.apache.axis.SOAPPart;import org.apache.axis.components.logger.LogFactory;import org.apache.axis.utils.Messages;import org.apache.commons.logging.Log;import javax.activation.DataHandler;import javax.activation.DataSource;import java.util.Collection;import java.util.HashMap;import java.util.LinkedList;/** * Implements the Attachment interface, via an actual Hashmap of actual * AttachmentParts. */public class AttachmentsImpl implements Attachments { protected static Log log = LogFactory.getLog(AttachmentsImpl.class.getName()); /** Field attachments. */ private HashMap attachments = new java.util.HashMap(); /** Field orderedAttachments. */ private LinkedList orderedAttachments = new LinkedList(); /** Field soapPart. */ protected SOAPPart soapPart = null; /** * The actual stream to manage the multi-related input stream. */ protected MultiPartInputStream mpartStream = null; /** * The form of the attachments, whether MIME or DIME. */ protected int sendtype= Attachments.SEND_TYPE_NOTSET; /** * This is the content location as specified in SOAP with Attachments. * This maybe null if the message had no Content-Location specifed. */ protected String contentLocation = null; /** * The HashMap for DataHandler Managements. */ private HashMap stackDataHandler = new HashMap(); /** * Used to distribute attachment streams without caching them. */ private IncomingAttachmentStreams _streams = null; private boolean _askedForAttachments = false; private boolean _askedForStreams = false; /** * Construct one of these on a parent Message. * Should only ever be called by Message constructor! * * @param intialContents should be anything but today only a stream is * supported. * @param contentType The mime content type of the stream for transports * that provide it. * @param contentLocation * * @throws org.apache.axis.AxisFault */ public AttachmentsImpl( Object intialContents, String contentType, String contentLocation) throws org.apache.axis.AxisFault { if (contentLocation != null) { contentLocation = contentLocation.trim(); if (contentLocation.length() == 0) { contentLocation = null; } } this.contentLocation = contentLocation; if (contentType != null) { if (contentType.equals(org.apache.axis.Message.MIME_UNKNOWN)) { } else { java.util.StringTokenizer st = new java.util.StringTokenizer(contentType, " \t;"); if (st.hasMoreTokens()) { String token = st.nextToken(); if (token.equalsIgnoreCase( org.apache.axis.Message.MIME_MULTIPART_RELATED)) { sendtype= SEND_TYPE_MIME; mpartStream = new org.apache.axis.attachments.MultiPartRelatedInputStream( contentType, (java.io.InputStream) intialContents); if (null == contentLocation) { // If the content location is not specified as // of the main message use the SOAP content location. contentLocation = mpartStream.getContentLocation(); if (contentLocation != null) { contentLocation = contentLocation.trim(); if (contentLocation.length() == 0) { contentLocation = null; } } } soapPart = new org.apache.axis.SOAPPart(null, mpartStream, false); MultiPartRelatedInputStream specificType = (MultiPartRelatedInputStream) mpartStream; _streams = new MultipartAttachmentStreams(specificType.boundaryDelimitedStream, specificType.orderedParts); } else if (token.equalsIgnoreCase(org.apache.axis.Message.MIME_APPLICATION_DIME)) { try{ mpartStream= new MultiPartDimeInputStream( (java.io.InputStream) intialContents); soapPart = new org.apache.axis.SOAPPart(null, mpartStream, false); }catch(Exception e){ throw org.apache.axis.AxisFault.makeFault(e);} sendtype= SEND_TYPE_DIME; MultiPartDimeInputStream specificType = (MultiPartDimeInputStream) mpartStream; _streams = new DimeAttachmentStreams(specificType.dimeDelimitedStream); } else if (token.indexOf(org.apache.axis.Message.CONTENT_TYPE_MTOM)!=-1){ sendtype = SEND_TYPE_MTOM; } } } } } /** * Copies attachment references from the multipartStream to local list. * Done only once per object creation. * * @throws AxisFault */ private void mergeinAttachments() throws AxisFault { if (mpartStream != null) { Collection atts = mpartStream.getAttachments(); if(contentLocation == null) contentLocation= mpartStream.getContentLocation(); mpartStream = null; setAttachmentParts(atts); } } /** * This method uses getAttacmentByReference() to look for attachment. * If attachment has been found, it will be removed from the list, and * returned to the user. * @param reference The reference that referers to an attachment. * * @return The part associated with the removed attachment, or null. * * @throws org.apache.axis.AxisFault */ public Part removeAttachmentPart(String reference) throws org.apache.axis.AxisFault { if (_askedForStreams) { throw new IllegalStateException(Messages.getMessage("concurrentModificationOfStream")); } multipart = null; dimemultipart = null; mergeinAttachments(); Part removedPart = getAttachmentByReference(reference); if (removedPart != null) { attachments.remove(removedPart.getContentId()); attachments.remove(removedPart.getContentLocation()); orderedAttachments.remove(removedPart); } return removedPart; } /** * Adds an existing attachment to this list. * Note: Passed part will be bound to this message. * @param newPart new part to add * @return Part old attachment with the same Content-ID, or null. * * @throws org.apache.axis.AxisFault */ public Part addAttachmentPart(Part newPart) throws org.apache.axis.AxisFault { if (_askedForStreams) { throw new IllegalStateException(Messages.getMessage("concurrentModificationOfStream")); } multipart = null; dimemultipart = null; mergeinAttachments(); Part oldPart = (Part) attachments.put(newPart.getContentId(), newPart); if (oldPart != null) { orderedAttachments.remove(oldPart); attachments.remove(oldPart.getContentLocation()); } orderedAttachments.add(newPart); if (newPart.getContentLocation() != null) { attachments.put(newPart.getContentLocation(), newPart); } return oldPart; } public Part createAttachmentPart(Object datahandler) throws org.apache.axis.AxisFault { // Searching for the same attachements Integer key = new Integer(datahandler.hashCode()); if (stackDataHandler.containsKey(key)) { return (Part)stackDataHandler.get(key); } multipart = null; dimemultipart = null; mergeinAttachments(); if (!(datahandler instanceof javax.activation.DataHandler)) { throw new org.apache.axis.AxisFault( Messages.getMessage( "unsupportedAttach", datahandler.getClass().getName(), javax.activation.DataHandler.class.getName())); } Part ret = new AttachmentPart((javax.activation.DataHandler) datahandler); addAttachmentPart(ret); // Store the current DataHandler with its key stackDataHandler.put(key, ret); return ret; } /** * Add the collection of parts. * * @param parts * * @throws org.apache.axis.AxisFault */ public void setAttachmentParts(java.util.Collection parts) throws org.apache.axis.AxisFault { if (_askedForStreams) { throw new IllegalStateException(Messages.getMessage("concurrentModificationOfStream")); } removeAllAttachments(); if ((parts != null) && !parts.isEmpty()) { for (java.util.Iterator i = parts.iterator(); i.hasNext();) { Object part = i.next(); if (null != part) { if(part instanceof Part) addAttachmentPart((Part)part); else createAttachmentPart(part); } } } } /** * This method should look at a refernce and determine if it is a CID: or * url to look for attachment. * <br> * Note: if Content-Id or Content-Location headers have changed by outside * code, lookup will not return proper values. In order to change these * values attachment should be removed, then added again. * @param reference The reference in the xml that referers to an attachment. * * @return The part associated with the attachment. * * @throws org.apache.axis.AxisFault */ public Part getAttachmentByReference(String reference) throws org.apache.axis.AxisFault { if (_askedForStreams) { throw new IllegalStateException(Messages.getMessage("concurrentModificationOfStream")); } if (null == reference) { return null; } reference = reference.trim(); if (0 == reference.length()) { return null; } mergeinAttachments(); //This search will pickit up if its fully qualified location or if it's a content-id // that is not prefixed by the cid. Part ret = (Part) attachments.get(reference); if( null != ret) return ret; if (!reference.startsWith(Attachments.CIDprefix) && (null != contentLocation)) { //Not a content-id check to see if its a relative location id. String fqreference = contentLocation; if (!fqreference.endsWith("/")) { fqreference += "/"; } if (reference.startsWith("/")) { fqreference += reference.substring(1); } else { fqreference += reference; } // lets see if we can get it as Content-Location ret = (AttachmentPart) attachments.get(fqreference); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -