001 //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/src/org/deegree/ogcwebservices/wfs/operation/LockFeatureDocument.java $
002 /*---------------- FILE HEADER ------------------------------------------
003
004 This file is part of deegree.
005 Copyright (C) 2001-2008 by:
006 Department of Geography, University of Bonn
007 http://www.giub.uni-bonn.de/deegree/
008 lat/lon GmbH
009 http://www.lat-lon.de
010
011 This library is free software; you can redistribute it and/or
012 modify it under the terms of the GNU Lesser General Public
013 License as published by the Free Software Foundation; either
014 version 2.1 of the License, or (at your option) any later version.
015
016 This library is distributed in the hope that it will be useful,
017 but WITHOUT ANY WARRANTY; without even the implied warranty of
018 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019 Lesser General Public License for more details.
020
021 You should have received a copy of the GNU Lesser General Public
022 License along with this library; if not, write to the Free Software
023 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024
025 Contact:
026
027 Andreas Poth
028 lat/lon GmbH
029 Aennchenstraße 19
030 53177 Bonn
031 Germany
032 E-Mail: poth@lat-lon.de
033
034 Prof. Dr. Klaus Greve
035 Department of Geography
036 University of Bonn
037 Meckenheimer Allee 166
038 53115 Bonn
039 Germany
040 E-Mail: greve@giub.uni-bonn.de
041
042 ---------------------------------------------------------------------------*/
043 package org.deegree.ogcwebservices.wfs.operation;
044
045 import java.io.IOException;
046 import java.net.URL;
047 import java.util.ArrayList;
048 import java.util.List;
049
050 import org.deegree.datatypes.QualifiedName;
051 import org.deegree.datatypes.time.TimeDuration;
052 import org.deegree.framework.xml.XMLParsingException;
053 import org.deegree.framework.xml.XMLTools;
054 import org.deegree.i18n.Messages;
055 import org.deegree.model.filterencoding.AbstractFilter;
056 import org.deegree.model.filterencoding.Filter;
057 import org.deegree.ogcwebservices.InvalidParameterValueException;
058 import org.deegree.ogcwebservices.wfs.operation.LockFeature.ALL_SOME_TYPE;
059 import org.w3c.dom.Element;
060 import org.xml.sax.SAXException;
061
062 /**
063 * Parser for "wfs:LockFeature" requests.
064 *
065 * @author <a href="mailto:schneider@lat-lon.de">Markus Schneider</a>
066 * @author last edited by: $Author: apoth $
067 *
068 * @version $Revision: 9345 $, $Date: 2007-12-27 17:22:25 +0100 (Do, 27 Dez 2007) $
069 */
070 public class LockFeatureDocument extends AbstractWFSRequestDocument {
071
072 private static final long serialVersionUID = 7168101158239058596L;
073
074 private static final String XML_TEMPLATE = "LockFeatureTemplate.xml";
075
076 /**
077 * Creates a skeleton document that contains the root element and the namespace bindings only.
078 *
079 * @throws IOException
080 * @throws SAXException
081 */
082 public void createEmptyDocument()
083 throws IOException, SAXException {
084 URL url = LockFeatureDocument.class.getResource( XML_TEMPLATE );
085 if ( url == null ) {
086 throw new IOException( "The resource '" + XML_TEMPLATE + " could not be found." );
087 }
088 load( url );
089 }
090
091 /**
092 * Parses the underlying "wfs:LockFeature" document into a {@link LockFeature} object.
093 *
094 * @param id
095 * @return corresponding <code>LockFeature</code> object
096 * @throws XMLParsingException
097 */
098 public LockFeature parse( String id )
099 throws XMLParsingException {
100
101 checkServiceAttribute();
102 String version = checkVersionAttribute();
103
104 Element root = this.getRootElement();
105 String handle = XMLTools.getNodeAsString( root, "@handle", nsContext, null );
106
107 long expiry = parseExpiry(root);
108
109 String lockActionString = XMLTools.getNodeAsString( root, "@lockAction", nsContext, "ALL" );
110 ALL_SOME_TYPE lockAction = ALL_SOME_TYPE.ALL;
111 try {
112 lockAction = LockFeature.validateLockAction( lockActionString );
113 } catch ( InvalidParameterValueException e ) {
114 throw new XMLParsingException( e.getMessage() );
115 }
116
117 List<Element> lockElements = XMLTools.getRequiredElements( root, "wfs:Lock", nsContext );
118 List<Lock> locks = new ArrayList<Lock>( lockElements.size() );
119 for ( Element lockElement : lockElements ) {
120 locks.add( parseLock( lockElement ) );
121 }
122 return new LockFeature( version, id, handle, expiry, lockAction, locks );
123 }
124
125 /**
126 * Parses the value of the expiry-attribute of the given node.
127 * <p>
128 * Handles both the WFS 1.1.0 and the WFS 1.2.0 formats:
129 * <ul>
130 * <li>WFS 1.1.0: specified as an xsd:positiveInteger (minutes)</li>
131 * <li>WFS 1.2.0: specified as an xsd:duration (identified by the leading P in the string)</li>
132 * </ul>
133 *
134 * @param root
135 * @return duration (in milliseconds)
136 */
137 static long parseExpiry( Element root )
138 throws XMLParsingException {
139
140 long millis = 0;
141 String expiry = XMLTools.getNodeAsString( root, "@expiry", nsContext,
142 LockFeature.DEFAULT_EXPIRY );
143
144 if ( expiry.length() < 1 ) {
145 String msg = "Attribute 'expiry' is empty.";
146 throw new XMLParsingException( msg );
147 }
148
149 if ( expiry.charAt( 0 ) == 'P' ) {
150 millis = TimeDuration.createTimeDuration( expiry ).getAsMilliSeconds();
151 } else {
152 try {
153 millis = ( (long) Integer.parseInt( expiry ) ) * 60 * 1000;
154 if ( millis < 1 ) {
155 throw new NumberFormatException();
156 }
157 } catch ( NumberFormatException e ) {
158 String msg = Messages.getMessage( "WFS_EXPIRY_ATTRIBUTE_INVALID", expiry );
159 throw new XMLParsingException( msg );
160 }
161 }
162 return millis;
163 }
164
165 /**
166 * Parses the given <code>wfs:Lock</code> element.
167 *
168 * @param lockElement
169 * @return corresponding <code>Lock</code> object
170 * @throws XMLParsingException
171 */
172 private Lock parseLock( Element lockElement )
173 throws XMLParsingException {
174
175 String handle = XMLTools.getNodeAsString( lockElement, "@handle", nsContext, null );
176 QualifiedName typeName = XMLTools.getRequiredNodeAsQualifiedName( lockElement, "@typeName",
177 nsContext );
178 Filter filter = null;
179 Element filterElement = (Element) XMLTools.getNode( lockElement, "ogc:Filter", nsContext );
180 if ( filterElement != null ) {
181 filter = AbstractFilter.buildFromDOM( filterElement );
182 }
183 return new Lock( handle, typeName, filter );
184 }
185 }