View Javadoc

1   /**
2    * This file Copyright (c) 2003-2011 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 org.apache.commons.lang.StringUtils;
44  import org.slf4j.Logger;
45  import org.slf4j.LoggerFactory;
46  
47  import javax.servlet.ServletException;
48  import javax.servlet.http.HttpServletRequest;
49  import javax.servlet.http.HttpServletResponse;
50  
51  import java.text.MessageFormat;
52  
53  
54  /**
55   * @author Fabrizio Giustina
56   * @version $Id: ImportPage.java 54096 2012-02-07 13:40:31Z mdivilek $
57   */
58  public class ImportPage extends ExportPage {
59  
60      /**
61       * Stable serialVersionUID.
62       */
63      private static final long serialVersionUID = 222L;
64  
65      /**
66       * Logger.
67       */
68      private static Logger log = LoggerFactory.getLogger(ImportPage.class);
69  
70      private Document mgnlFileImport;
71  
72      private String mgnlRedirect;
73  
74      private int mgnlUuidBehavior;
75  
76      /**
77       * @param name
78       * @param request
79       * @param response
80       */
81      public ImportPage(String name, HttpServletRequest request, HttpServletResponse response) {
82          super(name, request, response);
83      }
84  
85      /**
86       * @see info.magnolia.module.admininterface.pages.ExportPage#getCommand()
87       */
88      @Override
89      public String getCommand() {
90          if ("POST".equals(getRequest().getMethod())) {
91              return "importxml"; // a post request is an import request, preserve the behaviour from the servlet
92          }
93          return super.getCommand();
94      }
95  
96      /**
97       * Getter for <code>mgnlFileImport</code>.
98       * @return Returns the mgnlFileImport.
99       */
100     public Document getMgnlFileImport() {
101         return this.mgnlFileImport;
102     }
103 
104     /**
105      * Setter for <code>mgnlFileImport</code>.
106      * @param mgnlFileImport The mgnlFileImport to set.
107      */
108     public void setMgnlFileImport(Document mgnlFileImport) {
109         this.mgnlFileImport = mgnlFileImport;
110     }
111 
112     /**
113      * Getter for <code>mgnlRedirect</code>.
114      * @return Returns the mgnlRedirect.
115      */
116     public String getMgnlRedirect() {
117         return this.mgnlRedirect;
118     }
119 
120     /**
121      * Setter for <code>mgnlRedirect</code>.
122      * @param mgnlRedirect The mgnlRedirect to set.
123      */
124     public void setMgnlRedirect(String mgnlRedirect) {
125         this.mgnlRedirect = mgnlRedirect;
126     }
127 
128     /**
129      * Getter for <code>mgnlUuidBehavior</code>.
130      * @return Returns the mgnlUuidBehavior.
131      */
132     public int getMgnlUuidBehavior() {
133         return this.mgnlUuidBehavior;
134     }
135 
136     /**
137      * Setter for <code>mgnlUuidBehavior</code>.
138      * @param mgnlUuidBehavior The mgnlUuidBehavior to set.
139      */
140     public void setMgnlUuidBehavior(int mgnlUuidBehavior) {
141         this.mgnlUuidBehavior = mgnlUuidBehavior;
142     }
143 
144     /**
145      * @throws Exception
146      */
147     public String importxml() throws Exception {
148 
149         if (log.isDebugEnabled()) {
150             log.debug("Import request received.");
151         }
152 
153         if (StringUtils.isEmpty(mgnlRepository)) {
154             mgnlRepository = RepositoryConstants.WEBSITE;
155         }
156         if (StringUtils.isEmpty(mgnlPath)) {
157             mgnlPath = "/";
158         }
159 
160         if (!checkPermissions(request, mgnlRepository, mgnlPath, Permission.WRITE)) {
161 
162             AlertUtil.setMessage("Write permission needed for export. User not allowed to WRITE path [" + mgnlPath + "]");
163 
164             throw new ServletException(new AccessDeniedException(
165                 "Write permission needed for import. User not allowed to WRITE path ["
166                     + mgnlPath
167                     + "]"));
168         }
169         try{
170             DataTransporter.importDocument(
171                     mgnlFileImport,
172                     mgnlRepository,
173                     mgnlPath,
174                     mgnlKeepVersions,
175                     mgnlUuidBehavior,
176                     true,
177                     true);
178         } finally {
179             mgnlFileImport.getFile().delete();
180         }
181 
182         log.info("Import done");
183 
184         if (StringUtils.isNotBlank(mgnlRedirect)) {
185             if (log.isInfoEnabled()) {
186                 log.info(MessageFormat.format("Redirecting to [{0}]",
187                     new Object[]{mgnlRedirect}));
188             }
189             response.sendRedirect(mgnlRedirect);
190             return null;
191         }
192         return this.show();
193     }
194 
195 }