View Javadoc
1   /*
2    * Copyright (c) 2002-2020, City of Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.plugins.lutecetools.web;
35  
36  import java.util.Map;
37  
38  import javax.servlet.http.HttpServletRequest;
39  
40  import org.apache.commons.fileupload.FileItem;
41  
42  import fr.paris.lutece.plugins.lutecetools.service.Globals;
43  import fr.paris.lutece.plugins.lutecetools.service.LutecetoolsAsynchronousUploadHandler;
44  import fr.paris.lutece.plugins.lutecetools.service.XMLParser;
45  import fr.paris.lutece.portal.service.spring.SpringContextService;
46  import fr.paris.lutece.portal.util.mvc.commons.annotations.Action;
47  import fr.paris.lutece.portal.util.mvc.commons.annotations.View;
48  import fr.paris.lutece.portal.util.mvc.xpage.MVCApplication;
49  import fr.paris.lutece.portal.util.mvc.xpage.annotations.Controller;
50  import fr.paris.lutece.portal.web.xpages.XPage;
51  
52  /**
53   * This class provides a simple implementation of an XPage
54   */
55  @Controller( xpageName = "pomupdate", pageTitleI18nKey = "lutecetools.xpage.pomupdate.pageTitle", pagePathI18nKey = "lutecetools.xpage.pomupdate.pagePath" )
56  public class PomUpdateApp extends MVCApplication
57  {
58      private static final long serialVersionUID = 1871701739284867751L;
59      private static final String TEMPLATE_XPAGE = "/skin/plugins/lutecetools/pomupdate.html";
60      private static final String VIEW_HOME = "home";
61  
62      private static final String MARK_OUTPUT = "output";
63      private static final String MARK_HANDLER = "handler";
64      private static final String MARK_WARNING = "warning";
65  
66      private static final String INPUT_FIELD_NAME = "source";
67      private static final String ACTION_PROCESS = "process";
68  
69      private static final String ERROR_XML_PARSING = "Parsing error";
70      private static final String CANCELLED = "Cancelled";
71      private static final String ERROR_FILE_EXTENSION = "Wrong file extension";
72      private static final String ERROR_EMPTY_FIELD = "Empty field";
73  
74      private LutecetoolsAsynchronousUploadHandler _lutecetoolsAsynchronousUploadHandler = SpringContextService
75              .getBean( LutecetoolsAsynchronousUploadHandler.BEAN_NAME );
76      private String _strOutput = "";
77  
78      /**
79       * Returns the content of the page pomupdate.
80       * 
81       * @param request
82       *            The HTTP request
83       * @return The view
84       */
85      @View( value = VIEW_HOME, defaultView = true )
86      public XPage viewHome( HttpServletRequest request )
87      {
88          Map<String, Object> model = getModel( );
89  
90          if ( !Globals._strWarningPomMessage.isEmpty( ) )
91          {
92              model.put( MARK_WARNING, Globals._strWarningPomMessage );
93              Globals._strWarningPomMessage = "";
94          }
95          model.put( MARK_HANDLER, _lutecetoolsAsynchronousUploadHandler );
96          model.put( MARK_OUTPUT, _strOutput );
97  
98          return getXPage( TEMPLATE_XPAGE, request.getLocale( ), model );
99      }
100 
101     @Action( ACTION_PROCESS )
102     public XPage process( HttpServletRequest request )
103     {
104         FileItem fileInput = _lutecetoolsAsynchronousUploadHandler.getFile( request, INPUT_FIELD_NAME );
105 
106         if ( fileInput != null )
107         {
108             if ( fileInput.getContentType( ).endsWith( "xml" ) )
109             {
110                 _strOutput = XMLParser.updatePOM( fileInput );
111                 if ( _strOutput.isEmpty( ) )
112                 {
113                     _strOutput = ERROR_XML_PARSING;
114                 }
115                 else
116                     if ( _strOutput.equals( CANCELLED ) )
117                     {
118                         _strOutput = CANCELLED;
119                     }
120             }
121             else
122             {
123                 _strOutput = ERROR_FILE_EXTENSION;
124             }
125         }
126         else
127         {
128             _strOutput = ERROR_EMPTY_FIELD;
129         }
130 
131         return redirectView( request, VIEW_HOME );
132     }
133 }