001 //$HeadURL: $ 002 /*---------------------------------------------------------------------------- 003 This file is part of deegree, http://deegree.org/ 004 Copyright (C) 2001-2009 by: 005 Department of Geography, University of Bonn 006 and 007 lat/lon GmbH 008 009 This library is free software; you can redistribute it and/or modify it under 010 the terms of the GNU Lesser General Public License as published by the Free 011 Software Foundation; either version 2.1 of the License, or (at your option) 012 any later version. 013 This library is distributed in the hope that it will be useful, but WITHOUT 014 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 015 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 016 details. 017 You should have received a copy of the GNU Lesser General Public License 018 along with this library; if not, write to the Free Software Foundation, Inc., 019 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 020 021 Contact information: 022 023 lat/lon GmbH 024 Aennchenstr. 19, 53177 Bonn 025 Germany 026 http://lat-lon.de/ 027 028 Department of Geography, University of Bonn 029 Prof. Dr. Klaus Greve 030 Postfach 1147, 53001 Bonn 031 Germany 032 http://www.geographie.uni-bonn.de/deegree/ 033 034 e-mail: info@deegree.org 035 ----------------------------------------------------------------------------*/ 036 037 package org.deegree.enterprise.servlet; 038 039 import java.io.BufferedReader; 040 import java.io.IOException; 041 import java.io.InputStream; 042 import java.io.InputStreamReader; 043 044 import javax.mail.MessagingException; 045 import javax.mail.internet.ContentDisposition; 046 import javax.mail.internet.MimeBodyPart; 047 import javax.mail.internet.MimeMultipart; 048 import javax.mail.internet.MimeUtility; 049 import javax.mail.util.ByteArrayDataSource; 050 import javax.servlet.http.HttpServletRequest; 051 052 import org.deegree.framework.log.ILogger; 053 import org.deegree.framework.log.LoggerFactory; 054 import org.deegree.framework.xml.XMLException; 055 import org.deegree.framework.xml.XMLFragment; 056 import org.deegree.ogcbase.ExceptionCode; 057 import org.deegree.ogcwebservices.OGCWebServiceException; 058 import org.w3c.dom.Element; 059 import org.xml.sax.SAXException; 060 061 /** 062 * <code>RequestMultiPartHandler</code> handles the multiparts of a request with the content-type 063 * set to <code>multipart/form-data</code>. It appends the multiparts to an element which will be 064 * retrieved by calling the {@link #getElementForId(XMLFragment, String)} method which should be 065 * implemented by a sub-class. 066 * 067 * @author <a href="mailto:bezema@lat-lon.de">Rutger Bezema</a> 068 * 069 * @author last edited by: $Author:$ 070 * 071 * @version $Revision:$, $Date:$ 072 * 073 */ 074 public abstract class RequestMultiPartHandler { 075 076 private static ILogger LOG = LoggerFactory.getLogger( RequestMultiPartHandler.class ); 077 078 /** 079 * This method handles the multiparts of a ServletRequest. This method is called when the 080 * <code>content-type:form/multipart</code> header is set. For this method to take affect, a 081 * subclass must supply the xmlNode to which the multiparts will be appended. 082 * 083 * @param request 084 * the actual HttpServletRequest. 085 * @return all the XML-Representations of the incoming request including the multiparts. The 086 * xmlFragment at index 0 is the first mime-multipart, e.g. the request, and the 087 * following are the multi-part elements. These elements could be hooked into the 088 * request by calling the the {@link #getElementForId(XMLFragment, String )} method and 089 * thus receiving the element to which the multipart should be appended. If the stream 090 * didn't contain parsable data an array containing the data up-to that multipart will 091 * be returned never <code>null</code>. 092 * @throws OGCWebServiceException 093 * if an exception occurred while processing the mime parts. 094 */ 095 public XMLFragment[] handleMultiparts( HttpServletRequest request ) 096 throws OGCWebServiceException { 097 XMLFragment[] parsedData = new XMLFragment[0]; 098 try { 099 ByteArrayDataSource bads = new ByteArrayDataSource( request.getInputStream(), "application/xml" ); 100 LOG.logInfo( "Setting the 'mail.mime.multipart.ignoremissingendboundary' System property to false." ); 101 System.setProperty( "mail.mime.multipart.ignoremissingendboundary", "false" ); 102 MimeMultipart multi = new MimeMultipart( bads ); 103 parsedData = new XMLFragment[multi.getCount()]; 104 for ( int i = 0; i < multi.getCount(); i++ ) { 105 MimeBodyPart content = (MimeBodyPart) multi.getBodyPart( i ); 106 107 if ( !( content.isMimeType( "application/xml" ) || content.isMimeType( "text/xml" ) ) ) { 108 throw new OGCWebServiceException( 109 "Other than xml-encoded data can not be handled in the multiparts", 110 ExceptionCode.INVALID_FORMAT ); 111 } 112 String[] names = content.getHeader( "Content-Disposition" ); 113 String nameID = null; 114 if ( names != null ) { 115 for ( String name : names ) { 116 ContentDisposition cd = new ContentDisposition( name ); 117 String nm = cd.getParameter( "name" ); 118 if ( nm != null ) { 119 nameID = nm; 120 break; 121 } 122 } 123 } 124 125 if ( nameID == null ) { 126 nameID = content.getContentID(); 127 if ( nameID == null ) { 128 throw new OGCWebServiceException( 129 "Exactly one 'name' parameter must be set in the multipart-header.", 130 ExceptionCode.INVALID_FORMAT ); 131 } 132 } 133 134 if ( LOG.getLevel() == ILogger.LOG_DEBUG ) { 135 StringBuilder sb = new StringBuilder( "Handling Multipart (" ); 136 sb.append( ( i + 1 ) ); 137 sb.append( " of " ); 138 sb.append( multi.getCount() ); 139 sb.append( ")\ncontent id: " ).append( content.getContentID() ); 140 sb.append( "\ncontentType: " ).append( content.getContentType() ); 141 sb.append( "\ncontent name: " ).append( nameID ); 142 sb.append( "\nlineCount: " ).append( content.getLineCount() ); 143 sb.append( "\nencoding: " ).append( content.getEncoding() ); 144 LOG.logDebug( sb.toString() ); 145 } 146 147 InputStream contentIS = null; 148 if ( !"UTF-8".equalsIgnoreCase( content.getEncoding() ) ) { 149 contentIS = MimeUtility.decode( content.getInputStream(), content.getEncoding() ); 150 } else { 151 contentIS = content.getInputStream(); 152 } 153 BufferedReader reader = new BufferedReader( new InputStreamReader( contentIS ) ); 154 String firstLine = reader.readLine(); 155 if ( !reader.ready() || firstLine == null ) { 156 throw new OGCWebServiceException( "No characters found in multipart with id: " + nameID, 157 ExceptionCode.INVALID_FORMAT ); 158 } 159 LOG.logDebug( "first line of multipart: " + firstLine ); 160 // The root node is the first node to 161 parsedData[i] = new XMLFragment( reader, XMLFragment.DEFAULT_URL ); 162 // if not the root node, append the attribute originalNameID to the root element of 163 // the multipart. This 164 // way the multiparts may be found again. 165 if ( i != 0 ) { 166 Element root = parsedData[i].getRootElement(); 167 if ( root != null ) { 168 root.setAttribute( "originalNameID", nameID ); 169 } else { 170 LOG.logError( "Allthough the xml was parsed no root element was found, this is strange!!" ); 171 } 172 } 173 } 174 } catch ( IOException ioe ) { 175 throw new OGCWebServiceException( "Following error occurred while handling the mime multiparts:" 176 + ioe.getMessage(), ExceptionCode.INVALID_FORMAT ); 177 } catch ( MessagingException me ) { 178 throw new OGCWebServiceException( "Following error occurred while handling the mime multiparts:" 179 + me.getMessage(), ExceptionCode.INVALID_FORMAT ); 180 } catch ( XMLException xmle ) { 181 throw new OGCWebServiceException( "Following error occurred while handling the mime multiparts:" 182 + xmle.getMessage(), ExceptionCode.INVALID_FORMAT ); 183 } catch ( SAXException saxe ) { 184 throw new OGCWebServiceException( "Following error occurred while handling the mime multiparts:" 185 + saxe.getMessage(), ExceptionCode.INVALID_FORMAT ); 186 } 187 // finished so lets give back the xml-tree as a result. 188 return parsedData; 189 } 190 191 /** 192 * Sub-classes should implement this method to supply the xml-nodes to which the multiparts will 193 * be appended. 194 * 195 * @param xmlBody 196 * of the request. 197 * 198 * @param id 199 * of the multipart 200 * @return the Element to which the multipart with given id will be appended. 201 */ 202 public abstract Element getElementForId( XMLFragment xmlBody, String id ); 203 204 }