1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
56
57
58
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
81 if (!MgnlContext.getJCRSession(repository).nodeExists(path)) {
82 throw new RepositoryException("Path " + path + " Do not exist in the following workspace " + repository);
83 }
84
85
86 if (!checkPermissions(repository, path, Permission.WRITE)) {
87
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
105
106 public InputStream getXmlStream() {
107 return xmlStream;
108 }
109
110
111
112
113
114 public void setXmlStream(InputStream xmlStream) {
115 this.xmlStream = xmlStream;
116 }
117
118
119
120
121 public boolean isForceUnpublishState() {
122 return forceUnpublishState;
123 }
124
125
126
127
128 public void setForceUnpublishState(boolean forceUnpublishState) {
129 this.forceUnpublishState = forceUnpublishState;
130 }
131
132
133
134
135 public String getXmlFileName() {
136 return xmlFileName;
137 }
138
139
140
141
142
143 public void setXmlFileName(String xmlFileName) {
144 this.xmlFileName = xmlFileName;
145 }
146
147
148
149
150 public int getIdentifierBehavior() {
151 return identifierBehavior;
152 }
153
154
155
156
157
158
159 public void setIdentifierBehavior(int identifierBehavior) {
160 this.identifierBehavior = identifierBehavior;
161 }
162
163 }