View Javadoc

1   /**
2    * This file Copyright (c) 2013 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.commands.impl;
35  
36  import info.magnolia.cms.security.AccessDeniedException;
37  import info.magnolia.cms.security.Permission;
38  import info.magnolia.context.Context;
39  import info.magnolia.context.MgnlContext;
40  import info.magnolia.importexport.DataTransporter;
41  import info.magnolia.repository.RepositoryConstants;
42  
43  import java.io.IOException;
44  import java.io.InputStream;
45  
46  import javax.jcr.ImportUUIDBehavior;
47  import javax.jcr.RepositoryException;
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   * Generic Import Command.<br>
56   * Based on an InputStream representing a XML node structure, create the Node
57   * tree under the <br>
58   * specified workspace and path.
59   */
60  public class ImportCommand extends ExportCommand {
61  
62      private static final Logger log = LoggerFactory.getLogger(ImportCommand.class);
63  
64      public static final String IMPORT_XML_STREAM = "xmlStream";
65      private InputStream xmlStream;
66      public static final String IMPORT_XML_FILE_NAME = "xmlFileName";
67      private String xmlFileName;
68      public static final String IMPORT_IDENTIFIER_BEHAVIOR = "identifierBehavior";
69      private int identifierBehavior;
70      private boolean forceUnpublishState;
71  
72      @Override
73      public boolean execute(Context context) throws Exception {
74          String repository = StringUtils.isBlank(getRepository()) ? RepositoryConstants.WEBSITE : getRepository();
75          String path = StringUtils.isBlank(getPath()) ? "/" : getPath();
76          identifierBehavior = (getIdentifierBehavior() != -1) ? getIdentifierBehavior() : ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW;
77  
78          log.debug("Will import content in {} repository with and path {}", new Object[] { getRepository(), getPath() });
79  
80          // Check Path
81          if (!MgnlContext.getJCRSession(repository).nodeExists(path)) {
82              throw new RepositoryException("Path " + path + " Do not exist in the following workspace " + repository);
83          }
84  
85          // Check Permission
86          if (!checkPermissions(repository, path, Permission.WRITE)) {
87              // escape to prevent XSS attack
88              throw new AccessDeniedException("Write permission needed for import. User not allowed to Write path [" + StringEscapeUtils.escapeHtml(path) + "]");
89          }
90          try {
91              DataTransporter.importXmlStream(xmlStream, repository, path, xmlFileName, false, forceUnpublishState, identifierBehavior, true, true);
92          } catch (IOException ioe) {
93              throw ioe;
94          } finally {
95              if (xmlStream != null) {
96                  xmlStream.close();
97              }
98          }
99          log.info("Import done");
100         return true;
101     }
102 
103     /**
104      * @return the xmlStream
105      */
106     public InputStream getXmlStream() {
107         return xmlStream;
108     }
109 
110     /**
111      * @param xmlStream
112      *            the xmlStream to set
113      */
114     public void setXmlStream(InputStream xmlStream) {
115         this.xmlStream = xmlStream;
116     }
117 
118     /**
119      * @return the forceUnpublishState
120      */
121     public boolean isForceUnpublishState() {
122         return forceUnpublishState;
123     }
124 
125     /**
126      * @param forceUnpublishState
127      */
128     public void setForceUnpublishState(boolean forceUnpublishState) {
129         this.forceUnpublishState = forceUnpublishState;
130     }
131 
132     /**
133      * @return the xmlFileName
134      */
135     public String getXmlFileName() {
136         return xmlFileName;
137     }
138 
139     /**
140      * @param xmlFileName
141      *            the xmlFileName to set
142      */
143     public void setXmlFileName(String xmlFileName) {
144         this.xmlFileName = xmlFileName;
145     }
146 
147     /**
148      * @return the identifierBehavior
149      */
150     public int getIdentifierBehavior() {
151         return identifierBehavior;
152     }
153 
154     /**
155      * See {@link javax.jcr.ImportUUIDBehavior} constants definition.
156      * 
157      * @param identifierBehavior the identifierBehavior to set
158      */
159     public void setIdentifierBehavior(int identifierBehavior) {
160         this.identifierBehavior = identifierBehavior;
161     }
162 
163 }