001    //$HeadURL: svn+ssh://developername@svn.wald.intevation.org/deegree/base/trunk/resources/eclipse/files_template.xml $
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    package org.deegree.portal.standard.csw.control;
037    
038    import java.io.ByteArrayOutputStream;
039    import java.io.File;
040    import java.io.IOException;
041    import java.net.URL;
042    import java.net.URLEncoder;
043    import java.nio.charset.Charset;
044    import java.util.Enumeration;
045    import java.util.HashMap;
046    import java.util.Iterator;
047    import java.util.Locale;
048    import java.util.Map;
049    
050    import javax.servlet.http.HttpServletRequest;
051    import javax.xml.transform.dom.DOMSource;
052    import javax.xml.transform.stream.StreamResult;
053    
054    import org.apache.commons.httpclient.HttpMethod;
055    import org.deegree.enterprise.control.ajax.AbstractListener;
056    import org.deegree.enterprise.control.ajax.ResponseHandler;
057    import org.deegree.enterprise.control.ajax.WebEvent;
058    import org.deegree.framework.log.ILogger;
059    import org.deegree.framework.log.LoggerFactory;
060    import org.deegree.framework.util.HttpUtils;
061    import org.deegree.framework.util.KVP2Map;
062    import org.deegree.framework.xml.XMLFragment;
063    import org.deegree.framework.xml.XSLTDocument;
064    import org.deegree.i18n.Messages;
065    import org.deegree.portal.cataloguemanager.model.ExceptionBean;
066    
067    /**
068     * TODO add class documentation here
069     * 
070     * @author <a href="mailto:name@deegree.org">Andreas Poth</a>
071     * @author last edited by: $Author: admin $
072     * 
073     * @version $Revision: $, $Date: $
074     */
075    public class FullMetadataSetListener extends AbstractListener {
076    
077        private static final ILogger LOG = LoggerFactory.getLogger( FullMetadataSetListener.class );
078    
079        private URL xslURL;
080    
081        private Locale loc;
082    
083        /*
084         * (non-Javadoc)
085         * 
086         * @see
087         * org.deegree.enterprise.control.ajax.AbstractListener#actionPerformed(org.deegree.enterprise.control.ajax.WebEvent
088         * , org.deegree.enterprise.control.ajax.ResponseHandler)
089         */
090        @SuppressWarnings("unchecked")
091        public void actionPerformed( WebEvent event, ResponseHandler responseHandler )
092                                throws IOException {
093    
094            loc = getRequest().getLocale();
095    
096            String p = event.getAbsolutePath( getInitParameter( "XSLT" ) );
097            xslURL = new File( p ).toURL();
098    
099            Map<String, String> param = event.getParameter();
100            System.out.println( param );
101            String request = param.get( "request" );
102            XMLFragment resultXML = null;
103            try {
104                resultXML = performQuery( request );
105            } catch ( Exception e ) {
106                LOG.logError( e );
107                ExceptionBean bean = new ExceptionBean( this.getClass().getName(), e.getMessage() );
108                responseHandler.writeAndClose( true, bean );
109                return;
110            }
111            writeHTML( responseHandler, resultXML );
112    
113        }
114    
115        private void writeHTML( ResponseHandler responseHandler, XMLFragment resultXML )
116                                throws IOException {
117            String html;
118            try {
119                html = formatResult( resultXML );
120            } catch ( Exception e ) {
121                LOG.logError( e );
122                ExceptionBean bean = new ExceptionBean( this.getClass().getName(), e.getMessage() );
123                responseHandler.writeAndClose( true, bean );
124                return;
125            }
126            String charEnc = Charset.defaultCharset().displayName();
127            responseHandler.setContentType( "application/json; charset=" + charEnc );
128            responseHandler.writeAndClose( html );
129        }
130    
131        /**
132         * 
133         * @param resultXML
134         * @return
135         * @throws Exception
136         */
137        private String formatResult( XMLFragment resultXML )
138                                throws Exception {
139            XSLTDocument xsl = new XSLTDocument( xslURL );
140            ByteArrayOutputStream bos = new ByteArrayOutputStream( 10000 );
141            DOMSource xmlSource = new DOMSource( resultXML.getRootElement() );
142            DOMSource xslSource = new DOMSource( xsl.getRootElement().getOwnerDocument(),
143                                                 xsl.getSystemId() == null ? null : xsl.getSystemId().toString() );
144            StreamResult sr = new StreamResult( bos );
145            Map<String, String> param = new HashMap<String, String>();
146            param.put( "TITLE", Messages.get( loc, "CATMANAGE_RESULT_TITLE" ) );
147            param.put( "ABSTRACT", Messages.get( loc, "CATMANAGE_RESULT_ABSTRACT" ) );
148            param.put( "TOPICCATEGORY", Messages.get( loc, "CATMANAGE_RESULT_TOPICCATEGORY" ) );
149            param.put( "HIERARCHYLEVEL", Messages.get( loc, "CATMANAGE_RESULT_HIERARCHYLEVEL" ) );
150            param.put( "GEOGRDESC", Messages.get( loc, "CATMANAGE_RESULT_GEOGRDESC" ) );
151            param.put( "CREATIONDATE", Messages.get( loc, "CATMANAGE_RESULT_CREATIONDATE" ) );
152            param.put( "PUBLICATIONDATE", Messages.get( loc, "CATMANAGE_RESULT_PUBLICATIONDATE" ) );
153            param.put( "REVISIONDATE", Messages.get( loc, "CATMANAGE_RESULT_REVISIONDATE" ) );
154            param.put( "CONTACT", Messages.get( loc, "CATMANAGE_RESULT_CONTACT" ) );
155            param.put( "NAME", Messages.get( loc, "CATMANAGE_RESULT_NAME" ) );
156            param.put( "ORGANISATION", Messages.get( loc, "CATMANAGE_RESULT_ORGANISATION" ) );
157            param.put( "ADDRESS", Messages.get( loc, "CATMANAGE_RESULT_ADDRESS" ) );
158            param.put( "VOICE", Messages.get( loc, "CATMANAGE_RESULT_VOICE" ) );
159            param.put( "FAX", Messages.get( loc, "CATMANAGE_RESULT_FAX" ) );
160            param.put( "EMAIL", Messages.get( loc, "CATMANAGE_RESULT_EMAIL" ) );
161    
162            XSLTDocument.transform( xmlSource, xslSource, sr, null, param );
163            return new String( bos.toByteArray() );
164        }
165    
166        /**
167         * @param cswAddress
168         * @return
169         */
170        @SuppressWarnings("unchecked")
171        private XMLFragment performQuery( String request )
172                                throws Exception {
173            LOG.logDebug( "GetRecordById: ", request );
174            Enumeration<String> en = ( (HttpServletRequest) getRequest() ).getHeaderNames();
175            Map<String, String> map = new HashMap<String, String>();
176            while ( en.hasMoreElements() ) {
177                String name = (String) en.nextElement();
178                if ( !name.equalsIgnoreCase( "accept-encoding" ) && !name.equalsIgnoreCase( "content-length" )
179                     && !name.equalsIgnoreCase( "user-agent" ) ) {
180                    map.put( name, ( (HttpServletRequest) getRequest() ).getHeader( name ) );
181                }
182            }
183            URL url = new URL( request );
184            map = KVP2Map.toMap( url.getQuery() );
185            StringBuilder sb = new StringBuilder( url.toExternalForm().split( "\\?" )[0] );
186            sb.append( '?' );
187            Iterator<String> iter = map.keySet().iterator();
188            while ( iter.hasNext() ) {
189                String key = (String) iter.next();
190                sb.append( key ).append( '=' ).append(
191                                                       URLEncoder.encode( map.get( key ),
192                                                                          Charset.defaultCharset().displayName() ) );
193                if ( iter.hasNext() ) {
194                    sb.append( '&' );
195                }
196            }
197            HttpMethod method = HttpUtils.performHttpGet( sb.toString(), null, 60000, null, null, map );
198            XMLFragment xml = new XMLFragment();
199            xml.load( method.getResponseBodyAsStream(), request );
200            if ( LOG.getLevel() == ILogger.LOG_DEBUG ) {
201                LOG.logDebug( "GetRecordById result: ", xml.getAsPrettyString() );
202            }
203            return xml;
204        }
205    
206    }