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