001 //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/portal/owswatch/validator/CSWGetRecordsValidator.java $ 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.portal.owswatch.validator; 038 039 import java.io.IOException; 040 import java.io.InputStream; 041 import java.io.Serializable; 042 import java.net.URI; 043 044 import org.apache.commons.httpclient.HttpMethodBase; 045 import org.deegree.framework.util.StringTools; 046 import org.deegree.framework.xml.NamespaceContext; 047 import org.deegree.framework.xml.XMLParsingException; 048 import org.deegree.framework.xml.XMLTools; 049 import org.deegree.ogcbase.CommonNamespaces; 050 import org.w3c.dom.Document; 051 052 import org.deegree.portal.owswatch.Status; 053 import org.deegree.portal.owswatch.ValidatorResponse; 054 055 /** 056 * A specific implementation of AbstractValidator 057 * 058 * @author <a href="mailto:elmasry@lat-lon.de">Moataz Elmasry</a> 059 * @author last edited by: $Author: jmays $ 060 * 061 * @version $Revision: 20271 $, $Date: 2009-10-21 13:07:15 +0200 (Mi, 21 Okt 2009) $ 062 */ 063 public class CSWGetRecordsValidator extends AbstractValidator implements Serializable { 064 065 /** 066 * 067 */ 068 private static final long serialVersionUID = 653569280799419300L; 069 070 /* 071 * (non-Javadoc) 072 * 073 * @see org.deegree.portal.owswatch.validator.AbstractValidator#validateAnswer(org.apache.commons.httpclient.HttpMethodBase, 074 * int) 075 */ 076 @Override 077 public ValidatorResponse validateAnswer( HttpMethodBase method, int statusCode ) { 078 079 String contentType = method.getResponseHeader( "Content-Type" ).getValue(); 080 String lastMessage = null; 081 Status status = null; 082 083 if ( !contentType.contains( "xml" ) ) { 084 status = Status.RESULT_STATE_UNEXPECTED_CONTENT; 085 lastMessage = StringTools.concat( 100, "Error: Response Content is ", contentType, " not xml" ); 086 return new ValidatorResponse( lastMessage, status ); 087 } 088 089 String xml = null; 090 try { 091 InputStream stream = method.getResponseBodyAsStream(); 092 stream.reset(); 093 xml = parseStream( stream ); 094 } catch ( IOException e ) { 095 status = Status.RESULT_STATE_BAD_RESPONSE; 096 lastMessage = status.getStatusMessage(); 097 return new ValidatorResponse( lastMessage, status ); 098 } 099 100 if ( xml.length() == 0 ) { 101 status = Status.RESULT_STATE_BAD_RESPONSE; 102 lastMessage = "Error: XML Response is empty"; 103 return new ValidatorResponse( lastMessage, status ); 104 } 105 106 if ( xml.contains( "ExceptionReport" ) ) { 107 validateXmlServiceException( method ); 108 return new ValidatorResponse( lastMessage, status ); 109 } 110 // If its an xml, and there's no service exception, then don't really parse the xml, 111 // we assume that its well formed, since there might be huge xmls, which would take time to be parsed 112 status = Status.RESULT_STATE_AVAILABLE; 113 lastMessage = status.getStatusMessage(); 114 return new ValidatorResponse( lastMessage, status ); 115 } 116 117 /* 118 * (non-Javadoc) 119 * 120 * @see org.deegree.portal.owswatch.validator.AbstractValidator#validateXmlServiceException(org.apache.commons.httpclient.HttpMethodBase) 121 */ 122 @Override 123 protected ValidatorResponse validateXmlServiceException( HttpMethodBase method ) { 124 125 Document doc = null; 126 String lastMessage = null; 127 Status status = null; 128 129 try { 130 InputStream stream = method.getResponseBodyAsStream(); 131 stream.reset(); 132 doc = instantiateParser().parse( stream ); 133 } catch ( Exception e ) { 134 status = Status.RESULT_STATE_INVALID_XML; 135 lastMessage = "Error: MalFormed XML Response"; 136 return new ValidatorResponse( lastMessage, status ); 137 } 138 try { 139 NamespaceContext cnxt = CommonNamespaces.getNamespaceContext(); 140 URI owsns = CommonNamespaces.OWSNS; 141 String prefix = doc.lookupPrefix( owsns.toASCIIString() ); 142 StringBuilder builder = new StringBuilder( 100 ); 143 builder.append( "./" ); 144 if ( prefix != null && prefix.length() > 0 ) { 145 builder.append( prefix ).append( ":" ); 146 cnxt.addNamespace( prefix, owsns ); 147 } 148 149 builder.append( "Exception" ); 150 status = Status.RESULT_STATE_SERVICE_UNAVAILABLE; 151 lastMessage = XMLTools.getNodeAsString( doc.getDocumentElement(), builder.toString(), cnxt, 152 "Service Unavailable. Unknown error" ); 153 return new ValidatorResponse( lastMessage, status ); 154 } catch ( XMLParsingException e ) { 155 lastMessage = "Service Unavailable"; 156 status = Status.RESULT_STATE_SERVICE_UNAVAILABLE; 157 return new ValidatorResponse( lastMessage, status ); 158 } 159 } 160 }