001    //$$Header: $$
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.enterprise.servlet;
037    
038    import java.io.IOException;
039    import java.io.StringReader;
040    import java.net.URLDecoder;
041    import java.util.UUID;
042    
043    import javax.servlet.ServletException;
044    import javax.servlet.http.HttpServlet;
045    import javax.servlet.http.HttpServletRequest;
046    import javax.servlet.http.HttpServletResponse;
047    import javax.servlet.http.HttpSession;
048    import javax.xml.parsers.DocumentBuilder;
049    import javax.xml.parsers.DocumentBuilderFactory;
050    import javax.xml.parsers.ParserConfigurationException;
051    
052    import org.deegree.framework.log.ILogger;
053    import org.deegree.framework.log.LoggerFactory;
054    import org.deegree.framework.util.StringTools;
055    import org.deegree.framework.xml.XMLParsingException;
056    import org.deegree.framework.xml.XMLTools;
057    import org.deegree.portal.owswatch.CommonNamepspaces;
058    import org.deegree.portal.owswatch.ConfigurationsException;
059    import org.deegree.portal.owswatch.Constants;
060    import org.deegree.portal.owswatch.JSPagesReference;
061    import org.deegree.portal.owswatch.Messages;
062    import org.deegree.portal.owswatch.ServiceConfiguration;
063    import org.deegree.portal.owswatch.ServiceLog;
064    import org.deegree.portal.owswatch.ServiceWatcher;
065    import org.deegree.portal.owswatch.ServiceWatcherFactory;
066    import org.deegree.portal.owswatch.ServicesConfigurationFactory;
067    import org.deegree.portal.owswatch.ServicesConfigurationWriter;
068    import org.deegree.portal.owswatch.configs.OwsWatchConfig;
069    import org.w3c.dom.Document;
070    import org.w3c.dom.Element;
071    import org.xml.sax.InputSource;
072    
073    /**
074     * The owsWatch Servlet to handle the request of the owswatch portal
075     *
076     * @author <a href="mailto:ncho@lat-lon.de">ncho</a>
077     * @author last edited by: $Author: elmasry $
078     *
079     * @version $Revision: 1.3 $, $Date: 2008-03-20 16:33:27 $
080     */
081    public class OWSWatch extends HttpServlet {
082    
083        /**
084         *
085         */
086        private static final long serialVersionUID = -3136555273953816219L;
087    
088        private final String SESSIONID_KEY = Constants.SESSIONID_KEY;
089    
090        private static final ILogger LOG = LoggerFactory.getLogger( OWSWatch.class );
091    
092        private ServiceWatcher watcher = null;
093    
094        private ServicesConfigurationWriter servicesWriter = null;
095    
096        private String webinfPath = null;
097    
098        private String confFilePath = null;
099    
100        private ServiceWatcherFactory factory = null;
101    
102        private OwsWatchConfig conf = null;
103    
104        /*
105         * (non-Javadoc)
106         *
107         * @see javax.servlet.GenericServlet#init()
108         */
109        @Override
110        public void init()
111                                throws ServletException {
112    
113            confFilePath = this.getServletContext().getRealPath( this.getInitParameter( "owsWatchConfiguration" ) );
114            webinfPath = this.getServletContext().getRealPath( "WEB-INF/conf/owswatch" );
115            if ( !webinfPath.endsWith( "/" ) ) {
116                webinfPath = webinfPath.concat( "/" );
117            }
118    
119            try {
120                factory = ServiceWatcherFactory.getInstance( confFilePath, webinfPath );
121                watcher = factory.getServiceWatcherInstance();
122                conf = factory.getConf();
123    
124            } catch ( ConfigurationsException e ) {
125                LOG.logError( e.getLocalizedMessage() );
126            }
127            // An error has occured parsing the configurations file
128            if ( factory == null || conf == null ) {
129                LOG.logError( "There seems to be a problem with your configurations file. owsWatch will not start" );
130                return;
131            }
132    
133            watcher.compileDownTimeReport( webinfPath, conf );
134            watcher.start();
135        }
136    
137        /*
138         * (non-Javadoc)
139         *
140         * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest,
141         *      javax.servlet.http.HttpServletResponse)
142         */
143        @Override
144        public void doPost( HttpServletRequest request, HttpServletResponse response ) {
145            performAction( request, response );
146        }
147    
148        /*
149         * (non-Javadoc)
150         *
151         * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
152         *      javax.servlet.http.HttpServletResponse)
153         */
154        @Override
155        public void doGet( HttpServletRequest request, HttpServletResponse response ) {
156            performAction( request, response );
157        }
158    
159        /**
160         * determines which action is to be executed and calls the corresponding method of that action: LOGIN - send a
161         * REQUEST - read a PROTOCOL
162         *
163         * @param request
164         * @param response
165         * @return boolean if everything went fine. false otherwise. This class will handle the Errors
166         */
167        private boolean performAction( HttpServletRequest request, HttpServletResponse response ) {
168    
169            if ( request == null ) {
170                gotoErrorPage( request, response, Messages.getMessage( "ERROR_NULL_OBJ", "HttpServletRequest" ), null, null );
171                return false;
172            }
173    
174            if ( response == null ) {
175                gotoErrorPage( request, response, Messages.getMessage( "ERROR_NULL_OBJ", "HttpServletResponse" ), null,
176                               null );
177                return false;
178            }
179    
180            String action = request.getParameter( "action" );
181    
182            LOG.logDebug( "The action is: ", action );
183    
184            if ( watcher == null || ( !isLoggedIn( request ) && !"LOGIN".equals( action ) ) ) {
185                return handleLogout( request, response );
186            }
187    
188            if ( action != null ) {
189                if ( action.equals( "LOGIN" ) ) {
190                    handleLogin( request, response );
191                } else if ( action.equals( "LOGOUT" ) ) {
192                    handleLogout( request, response );
193                } else if ( action.equals( "stopServiceMonitor" ) ) {
194                    stopServiceMonitor( request, response );
195                } else if ( action.equals( "startServiceMonitor" ) ) {
196                    startServiceMonitor( request, response );
197                } else if ( action.equals( "logout" ) ) {
198                    handleLogout( request, response );
199                } else if ( action.equals( "gotoLogin" ) ) {
200                    gotoLoginPage( request, response );
201                } else if ( action.equals( "serviceDelete" ) ) {
202                    handleServiceDelete( request, response );
203                } else if ( action.equals( "serviceEdit" ) ) {
204                    handleServiceEdit( request, response );
205                } else if ( action.equals( "serviceTest" ) ) {
206                    handleServiceTest( request, response );
207                } else if ( action.equals( "refreshPage" ) ) {
208                    handleRefreshPage( request, response );
209                } else if ( action.equals( "cancelServiceManager" ) ) {
210                    handleRefreshPage( request, response );
211                } else if ( action.equals( "addService" ) ) {
212                    handleAddService( request, response );
213                } else {
214                    gotoErrorPage( request, response, StringTools.concat( 100,
215                                                                          Messages.getMessage( "ERROR_PARAM_UNEXPECTED",
216                                                                                               "action", action ) ), null,
217                                   null );
218                    return false;
219                }
220            } else {
221                // action is null. The request has to be an rpc
222                String requestName = request.getParameter( "rpc" );
223                if ( requestName != null ) {
224                    return handleRPCRequest( request, response, requestName );
225                }
226                return true;
227            }
228            return false;
229        }
230    
231        /**
232         * Takes in a POST request and directs it to the corresponding function
233         *
234         * @param requestValue
235         *            String sent from the POST form
236         */
237        private boolean handleRPCRequest( HttpServletRequest request, HttpServletResponse response, String requestValue ) {
238    
239            Document doc = null;
240            try {
241                doc = parseDocument( requestValue );
242            } catch ( XMLParsingException e ) {
243                gotoErrorPage( request, response, e.getLocalizedMessage(), "Go Back to the main page",
244                               JSPagesReference.getString( "OWSWatch.owswatchMonitorList" ) );
245                return false;
246            }
247            if ( doc == null ) {
248                gotoErrorPage( request, response, "The RPC request is null", Messages.getString( "MESSAGE_GOTO_MAIN" ),
249                               JSPagesReference.getString( "OWSWatch.owswatchMonitorList" ) );
250                return false;
251            }
252            Element root = doc.getDocumentElement();
253            String rpcType = root.getAttribute( "type" );
254            if ( "SaveService".equals( rpcType ) ) {
255                if ( handleSaveService( request, response, root ) ) {
256                    return handleRefreshPage( request, response );
257                } else {
258                    return false;
259                }
260            } else if ( "SaveConfigs".equals( rpcType ) ) {
261                // TODO will be used later to edit the serviceconfig.xml
262                return true;
263            } else {
264                return true;
265            }
266        }
267    
268        /**
269         * Takes in a addService Request and directs to the corresponding page
270         *
271         * @return boolean if no errors were thrown, false otherwise
272         */
273        private boolean handleAddService( HttpServletRequest request, HttpServletResponse response ) {
274    
275            storeSelectedServiceInSession( request );
276            HttpSession session = request.getSession( true );
277            // Indicates this is a new service
278            session.setAttribute( "EditService", Boolean.valueOf( false ) );
279            String[] ns = { factory.getServicesParser().getPrefix(), CommonNamepspaces.DEEGREEWSNS.toASCIIString() };
280            session.setAttribute( "PREFIX_NS", ns );
281            session.setAttribute( "ServiceConfigs", watcher.getServices() );
282            String nextpage = JSPagesReference.getString( "OWSWatch.editTest" );
283            try {
284                response.sendRedirect( nextpage );
285            } catch ( Exception e ) {
286                gotoErrorPage( request, response, Messages.getMessage( "ERROR_PAGE_NOT_FOUND", nextpage ),
287                               Messages.getString( "MESSAGE_GOTO_MAIN" ),
288                               JSPagesReference.getString( "OWSWatch.owswatchMonitorList" ) );
289                return false;
290            }
291            return true;
292        }
293    
294        /**
295         * handles SaveService requests whether its a new service or edited service
296         *
297         * @param rpcRequest
298         * @return boolean if no errors happened
299         */
300        private boolean handleSaveService( HttpServletRequest request, HttpServletResponse response, Element rpcRequest ) {
301    
302            ServicesConfigurationFactory parser = factory.getServicesParser();
303            ServiceConfiguration service = null;
304            try {
305                String xPath = StringTools.concat( 100, "./ServiceXML/", factory.getServicesParser().getPrefix(),
306                                                   ":SERVICE" );
307                Element serviceElem = XMLTools.getElement( rpcRequest, xPath, ServicesConfigurationFactory.getCnxt() );
308                service = parser.parseService( serviceElem, factory.getServicesParser().getPrefix() );
309            } catch ( Exception e ) {
310                String errorMsg = Messages.arrayToString( new String[] { Messages.getString( "ERROR_SAVE_SERVICE" ),
311                                                                        e.getLocalizedMessage() }, "\n" );
312                gotoErrorPage( request, response, errorMsg, Messages.getString( "MESSAGE_GOTO_MAIN" ),
313                               JSPagesReference.getString( "OWSWatch.owswatchMonitorList" ) );
314                return false;
315            }
316    
317            ServiceLog serviceLog = null;
318            try {
319                if ( watcher.getServices().containsKey( service.getServiceid() ) ) {
320                    serviceLog = watcher.getServiceLogs().get( service.getServiceid() );
321                } else {
322    
323                    serviceLog = new ServiceLog( factory.getProtDirPath(), service.getServiceid(),
324                                                 service.getServiceName(), service.getServiceType(),
325                                                 factory.getServletAddr(), factory.getSender() );
326                }
327                watcher.addService( service, serviceLog );
328                servicesWriter.writeDocument( watcher.getServices() );
329            } catch ( Exception e ) {
330                LOG.logError( e.getMessage(), e );
331                gotoErrorPage( request, response, e.getMessage(), Messages.getMessage( "MESSAGE_GOTO_MAIN" ),
332                               JSPagesReference.getString( "OWSWatch.login" ) );
333                return false;
334            }
335    
336            return true;
337        }
338    
339        /**
340         * handles the administrator login
341         *
342         * @return true if no errors happened, false otherwise
343         */
344        private boolean handleLogin( HttpServletRequest request, HttpServletResponse response ) {
345    
346            // If the configuration files could not be parsed correctly, the program should not proceed
347            if ( factory == null || conf == null ) {
348                gotoErrorPage( request, response, Messages.getMessage( "INCORRECT_LOGIN" ), null, null );
349            }
350    
351            String user = request.getParameter( "username" );
352            String pwd = request.getParameter( "password" );
353    
354            try {
355                if ( conf.isAuthenticatedUser( user, pwd ) ) {
356                    servicesWriter = new ServicesConfigurationWriter( webinfPath
357                                                                      + conf.getGeneral().getServiceInstancesPath(),
358                                                                      factory.getServicesParser().getPrefix() );
359                    HttpSession session = request.getSession( true );
360                    session.setAttribute( "Services", this.watcher.getServices() );
361                    session.setAttribute( "Logs", watcher.getServiceLogs() );
362                    session.setAttribute( "GLOBAL_REFRESH", conf.getGeneral().getGlobalRefreshRate() );
363                    session.setAttribute( "ThreadSuspended", false );
364                    session.setAttribute( "ServiceDescription", conf.getServiceConfig() );
365                    // isLoggedin
366                    String sessionId = UUID.randomUUID().toString();
367                    session.setAttribute( SESSIONID_KEY, sessionId );
368                    session.setAttribute( "isLoggedin", true );
369                    String nextpage = JSPagesReference.getString( "OWSWatch.owswatchMonitorList" );
370                    response.sendRedirect( nextpage );
371                } else {
372                    gotoErrorPage( request, response, Messages.getMessage( "INCORRECT_LOGIN" ),
373                                   Messages.getMessage( "MESSAGE_GOTO_MAIN" ),
374                                   JSPagesReference.getString( "OWSWatch.login" ) );
375                }
376            } catch ( Exception e ) {
377                String errorMsg = StringTools.concat( 100, Messages.getMessage( "ERROR_LOGIN" ), "</br>",
378                                                      e.getLocalizedMessage() );
379                gotoErrorPage( request, response, errorMsg, Messages.getMessage( "MESSAGE_GOTO_MAIN" ),
380                               JSPagesReference.getString( "OWSWatch.login" ) );
381                return false;
382            }
383            return true;
384        }
385    
386        /**
387         * Stops Monitoring a certain service. i.e. stop refreshing the page after time intervals
388         *
389         * @param request
390         * @param response
391         */
392        private boolean stopServiceMonitor( HttpServletRequest request, HttpServletResponse response ) {
393    
394            int index = Integer.parseInt( request.getParameter( "reqIndex" ) );
395            watcher.stopServiceConfiguration( index );
396            HttpSession session = request.getSession( true );
397            session.setAttribute( "Services", this.watcher.getServices() );
398            session.setAttribute( "Logs", watcher.getServiceLogs() );
399            String nextpage = JSPagesReference.getString( "OWSWatch.owswatchMonitorList" );
400            try {
401                response.sendRedirect( nextpage );
402            } catch ( Exception e ) {
403                LOG.logError( "The ServiceConfiguration could not be stopped", e );
404                return false;
405            }
406            return true;
407        }
408    
409        /**
410         * Starts monitoring a certain service. i.e. starts sending GetCapabilities requests after predefined intervals
411         *
412         */
413        private boolean startServiceMonitor( HttpServletRequest request, HttpServletResponse response ) {
414    
415            int index = Integer.parseInt( request.getParameter( "reqIndex" ) );
416            watcher.startServiceConfiguration( index );
417            HttpSession session = request.getSession( true );
418            session.setAttribute( "Services", this.watcher.getServices() );
419            session.setAttribute( "Logs", watcher.getServiceLogs() );
420            String nextpage = JSPagesReference.getString( "OWSWatch.owswatchMonitorList" );
421    
422            try {
423                response.sendRedirect( nextpage );
424            } catch ( Exception e ) {
425                LOG.logError( "The ServiceConfiguration could not be started", e );
426                return false;
427            }
428            return true;
429        }
430    
431        /**
432         * Logs the user out
433         *
434         * @return true if logout successfully, false otherwise
435         */
436        private boolean handleLogout( HttpServletRequest request, HttpServletResponse response ) {
437    
438            HttpSession session = request.getSession( true );
439            session.setAttribute( "isLoggedin", false );
440            session.removeAttribute( SESSIONID_KEY );
441    
442            String nextPage = JSPagesReference.getString( "OWSWatch.login" );
443            try {
444                response.sendRedirect( nextPage );
445            } catch ( Exception e ) {
446                gotoErrorPage( request, response, Messages.getMessage( "ERROR_PAGE_NOT_FOUND", nextPage ), null, null );
447                return false;
448            }
449    
450            return true;
451        }
452    
453        /**
454         * goto the first page
455         *
456         * @return true if no error happened, false otherwise
457         */
458        private boolean gotoLoginPage( HttpServletRequest request, HttpServletResponse response ) {
459    
460            String nextPage = JSPagesReference.getString( "OWSWatch.owswatchMonitorList" );
461            try {
462                response.sendRedirect( nextPage );
463            } catch ( Exception e ) {
464                gotoErrorPage( request, response, Messages.getMessage( "ERROR_PAGE_NOT_FOUND", nextPage ), null, null );
465                return false;
466            }
467    
468            return true;
469        }
470    
471        /**
472         * deletes a service
473         *
474         * @return true if no errors happened, false otherwise
475         */
476        private boolean handleServiceDelete( HttpServletRequest request, HttpServletResponse response ) {
477    
478            storeSelectedServiceInSession( request );
479            int serviceId = Integer.valueOf( request.getParameter( "serviceId" ) );
480            try {
481                watcher.removeService( serviceId );
482                servicesWriter.writeDocument( watcher.getServices() );
483            } catch ( Exception e ) {
484                gotoErrorPage( request, response, e.getMessage(), Messages.getMessage( "MESSAGE_GOTO_MAIN" ),
485                               JSPagesReference.getString( "OWSWatch.owswatchMonitorList" ) );
486            }
487            HttpSession session = request.getSession( true );
488            session.setAttribute( "Services", watcher.getServices() );
489            session.setAttribute( "Logs", watcher.getServiceLogs() );
490            String nextpage = JSPagesReference.getString( "OWSWatch.owswatchMonitorList" );
491    
492            try {
493                response.sendRedirect( nextpage );
494            } catch ( Exception e ) {
495                gotoErrorPage( request, response, Messages.getMessage( "ERROR_PAGE_NOT_FOUND", nextpage ),
496                               Messages.getMessage( "MESSAGE_GOTO_MAIN" ),
497                               JSPagesReference.getString( "OWSWatch.owswatchMonitorList" ) );
498                return false;
499            }
500            return true;
501        }
502    
503        /**
504         * Redirects the reuqest to the ServiceManager Dialogue
505         *
506         * @return ture if no errors happened, false otherwise
507         */
508        private boolean handleServiceEdit( HttpServletRequest request, HttpServletResponse response ) {
509    
510            storeSelectedServiceInSession( request );
511            HttpSession session = request.getSession( true );
512            // Indicates this is an existing service
513            session.setAttribute( "EditService", Boolean.valueOf( true ) );
514            ServiceConfiguration service = null;
515            try {
516                int serviceId = Integer.parseInt( request.getParameter( "serviceId" ) );
517                service = watcher.getService( serviceId );
518            } catch ( Exception e ) {
519                gotoErrorPage( request, response, Messages.getMessage( "ERROR_MISSING_KEY", "ServiceId" ),
520                               Messages.getMessage( "MESSAGE_GOTO_MAIN" ),
521                               JSPagesReference.getString( "OWSWatch.owswatchMonitorList" ) );
522                return false;
523            }
524    
525            String[] ns = { factory.getServicesParser().getPrefix(), CommonNamepspaces.DEEGREEWSNS.toASCIIString() };
526            session.setAttribute( "PREFIX_NS", ns );
527            // The service to edit in the jsp page
528            session.setAttribute( "ServiceToEdit", service );
529            String nextpage = JSPagesReference.getString( "OWSWatch.editTest" );
530    
531            try {
532                response.sendRedirect( nextpage );
533            } catch ( Exception e ) {
534                gotoErrorPage( request, response, Messages.getMessage( "ERROR_PAGE_NOT_FOUND", nextpage ),
535                               Messages.getMessage( "MESSAGE_GOTO_MAIN" ),
536                               JSPagesReference.getString( "OWSWatch.owswatchMonitorList" ) );
537                return false;
538            }
539            return true;
540        }
541    
542        /**
543         * Executes the given test once
544         *
545         * @return true if no errors happened, false otherwise
546         */
547        private boolean handleServiceTest( HttpServletRequest request, HttpServletResponse response ) {
548    
549            storeSelectedServiceInSession( request );
550            String Id = request.getParameter( "serviceId" );
551            watcher.execute( Integer.valueOf( Id ) );
552            HttpSession session = request.getSession( true );
553            session.setAttribute( "Services", watcher.getServices() );
554            session.setAttribute( "Logs", watcher.getServiceLogs() );
555            String nextpage = JSPagesReference.getString( "OWSWatch.owswatchMonitorList" );
556    
557            try {
558                response.sendRedirect( nextpage );
559            } catch ( Exception e ) {
560                gotoErrorPage( request, response, Messages.getMessage( "ERROR_PAGE_NOT_FOUND", nextpage ),
561                               Messages.getMessage( "MESSAGE_GOTO_MAIN" ),
562                               JSPagesReference.getString( "OWSWatch.owswatchMonitorList" ) );
563                return false;
564            }
565            return true;
566        }
567    
568        /**
569         * Gets the selectedservice(case exists) Id and stores in the session
570         *
571         */
572        private void storeSelectedServiceInSession( HttpServletRequest request ) {
573            HttpSession session = request.getSession( true );
574            String tmp = (String) request.getParameter( "selectedService" );
575            if ( tmp != null & session != null ) {
576                try {
577                    session.setAttribute( "selectedService", new Integer( tmp ) );
578                } catch ( Exception e ) {
579                    session.setAttribute( "selectedService", new Integer( -1 ) );
580                }
581            }
582        }
583    
584        private void gotoErrorPage( HttpServletRequest request, HttpServletResponse response, String error, String urlText,
585                                    String url ) {
586    
587            LOG.logError( "The error occured is: " + error );
588            HttpSession session = request.getSession( true );
589    
590            session.setAttribute( "message", StringTools.replace( error, "\n", "<br/>", true ) );
591            if ( error == null ) {
592                error = "An unknown error has occured";
593            }
594            if ( urlText == null ) {
595                urlText = "";
596            }
597            session.setAttribute( "URLText", urlText );
598            if ( url == null ) {
599                url = "";
600            }
601            session.setAttribute( "URLAdd", url );
602            try {
603                String nextpage = JSPagesReference.getString( "OWSWatch.error" );
604                response.sendRedirect( nextpage );
605            } catch ( Exception e ) {
606                LOG.logError( "The page could not be redirected to the error page" );
607            }
608        }
609    
610        /**
611         * goto the main Monitor list
612         *
613         * @return true if no errors happened, false otherwise
614         */
615        private boolean handleRefreshPage( HttpServletRequest request, HttpServletResponse response ) {
616    
617            storeSelectedServiceInSession( request );
618            HttpSession session = request.getSession( true );
619            String nextpage = null;
620            if ( watcher != null ) {
621                session.setAttribute( "Services", watcher.getServices() );
622                session.setAttribute( "Logs", watcher.getServiceLogs() );
623                nextpage = JSPagesReference.getString( "OWSWatch.owswatchMonitorList" );
624            }
625    
626            try {
627                response.sendRedirect( nextpage );
628            } catch ( Exception e ) {
629                gotoErrorPage( request, response, Messages.getMessage( "ERROR_PAGE_NOT_FOUND", nextpage ), null, null );
630                return false;
631            }
632            return true;
633        }
634    
635        /**
636         * Creates a new instance of DocumentBuilder
637         *
638         * @return DocumentBuilder
639         * @throws IOException
640         */
641        private DocumentBuilder instantiateParser()
642                                throws IOException {
643    
644            DocumentBuilder parser = null;
645    
646            try {
647                DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();
648                fac.setNamespaceAware( true );
649                fac.setValidating( false );
650                fac.setIgnoringElementContentWhitespace( true );
651                parser = fac.newDocumentBuilder();
652                return parser;
653            } catch ( ParserConfigurationException e ) {
654                throw new IOException( "Unable to initialize DocumentBuilder: " + e.getMessage() );
655            }
656        }
657    
658        /**
659         * Converts a given xml string to a document
660         *
661         * @param xmlText
662         * @return Document
663         */
664        private Document parseDocument( String xmlText )
665                                throws XMLParsingException {
666    
667            Document doc = null;
668            try {
669                String dec = URLDecoder.decode( xmlText, "UTF-8" );
670                doc = instantiateParser().parse( new InputSource( new StringReader( dec ) ) );
671            } catch ( Exception e ) {
672                throw new XMLParsingException( "Error parsing xml document\n" + e.getLocalizedMessage() );
673            }
674            return doc;
675        }
676    
677        /**
678         * Verifies that this user is loggedin through comparing the session ID from the request paarameter with that saved
679         * in the session
680         *
681         * @param request
682         * @return true if the user is loggedin, false otherwise
683         */
684        protected boolean isLoggedIn( HttpServletRequest request ) {
685            HttpSession session = request.getSession( true );
686            String requestSession = request.getParameter( SESSIONID_KEY );
687            String sessionId = (String) session.getAttribute( SESSIONID_KEY );
688            if ( requestSession == null || sessionId == null || !requestSession.equals( sessionId ) ) {
689                return false;
690            }
691            return true;
692        }
693    }