View Javadoc
1   /**
2    * This file Copyright (c) 2003-2014 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.module.admininterface.pages;
35  
36  import info.magnolia.cms.beans.runtime.Document;
37  import info.magnolia.cms.security.AccessDeniedException;
38  import info.magnolia.cms.security.Permission;
39  import info.magnolia.cms.util.AlertUtil;
40  import info.magnolia.importexport.DataTransporter;
41  import info.magnolia.repository.RepositoryConstants;
42  
43  import java.text.MessageFormat;
44  
45  import javax.servlet.ServletException;
46  import javax.servlet.http.HttpServletRequest;
47  import javax.servlet.http.HttpServletResponse;
48  
49  import org.apache.commons.lang.StringEscapeUtils;
50  import org.apache.commons.lang.StringUtils;
51  import org.slf4j.Logger;
52  import org.slf4j.LoggerFactory;
53  
54  
55  
56  /**
57   * @author Fabrizio Giustina
58   * @version $Id$
59   */
60  public class ImportPage extends ExportPage {
61  
62      /**
63       * Stable serialVersionUID.
64       */
65      private static final long serialVersionUID = 222L;
66  
67      /**
68       * Logger.
69       */
70      private static Logger log = LoggerFactory.getLogger(ImportPage.class);
71  
72      private Document mgnlFileImport;
73  
74      private String mgnlRedirect;
75  
76      private int mgnlUuidBehavior;
77  
78      /**
79       * @param name
80       * @param request
81       * @param response
82       */
83      public ImportPage(String name, HttpServletRequest request, HttpServletResponse response) {
84          super(name, request, response);
85      }
86  
87      /**
88       * @see info.magnolia.module.admininterface.pages.ExportPage#getCommand()
89       */
90      @Override
91      public String getCommand() {
92          if ("POST".equals(getRequest().getMethod())) {
93              return "importxml"; // a post request is an import request, preserve the behaviour from the servlet
94          }
95          return super.getCommand();
96      }
97  
98      /**
99       * Getter for <code>mgnlFileImport</code>.
100      * @return Returns the mgnlFileImport.
101      */
102     public Document getMgnlFileImport() {
103         return this.mgnlFileImport;
104     }
105 
106     /**
107      * Setter for <code>mgnlFileImport</code>.
108      * @param mgnlFileImport The mgnlFileImport to set.
109      */
110     public void setMgnlFileImport(Document mgnlFileImport) {
111         this.mgnlFileImport = mgnlFileImport;
112     }
113 
114     /**
115      * Getter for <code>mgnlRedirect</code>.
116      * @return Returns the mgnlRedirect.
117      */
118     public String getMgnlRedirect() {
119         return this.mgnlRedirect;
120     }
121 
122     /**
123      * Setter for <code>mgnlRedirect</code>.
124      * @param mgnlRedirect The mgnlRedirect to set.
125      */
126     public void setMgnlRedirect(String mgnlRedirect) {
127         this.mgnlRedirect = mgnlRedirect;
128     }
129 
130     /**
131      * Getter for <code>mgnlUuidBehavior</code>.
132      * @return Returns the mgnlUuidBehavior.
133      */
134     public int getMgnlUuidBehavior() {
135         return this.mgnlUuidBehavior;
136     }
137 
138     /**
139      * Setter for <code>mgnlUuidBehavior</code>.
140      * @param mgnlUuidBehavior The mgnlUuidBehavior to set.
141      */
142     public void setMgnlUuidBehavior(int mgnlUuidBehavior) {
143         this.mgnlUuidBehavior = mgnlUuidBehavior;
144     }
145 
146     /**
147      * @throws Exception
148      */
149     public String importxml() throws Exception {
150 
151         if (mgnlFileImport == null) {
152             AlertUtil.setMessage("Select a file to import.");
153             throw new ServletException("Select a file to import.");
154         }
155 
156         if (log.isDebugEnabled()) {
157             log.debug("Import request received.");
158         }
159 
160         if (StringUtils.isEmpty(mgnlRepository)) {
161             mgnlRepository = RepositoryConstants.WEBSITE;
162         }
163         if (StringUtils.isEmpty(mgnlPath)) {
164             mgnlPath = "/";
165         }
166 
167         checkPath(); //check if path exists
168 
169         if (!checkPermissions(request, mgnlRepository, mgnlPath, Permission.WRITE)) {
170 
171             //escape to prevent XSS attack
172             AlertUtil.setMessage("Write permission needed for import. User not allowed to WRITE path [" + StringEscapeUtils.escapeHtml(mgnlPath) + "]");
173 
174             throw new ServletException(new AccessDeniedException(
175                 "Write permission needed for import. User not allowed to WRITE path ["
176                     + StringEscapeUtils.escapeHtml(mgnlPath) //escape to prevent XSS attack
177                     + "]"));
178         }
179         try{
180             DataTransporter.importDocument(
181                     mgnlFileImport,
182                     mgnlRepository,
183                     mgnlPath,
184                     false,
185                     mgnlUuidBehavior,
186                     true,
187                     true);
188 		} catch (Exception e) {
189 			AlertUtil.setMessage("Failed to import file. " + e.getMessage());
190 			throw new ServletException(e.getMessage());
191         } finally {
192             mgnlFileImport.getFile().delete();
193         }
194 
195         log.info("Import done");
196 
197         if (StringUtils.isNotBlank(mgnlRedirect)) {
198             if (log.isInfoEnabled()) {
199                 log.info(MessageFormat.format("Redirecting to [{0}]",
200                     new Object[]{mgnlRedirect}));
201             }
202             response.sendRedirect(mgnlRedirect);
203             return null;
204         }
205         return this.show();
206     }
207 }