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.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 import java.text.MessageFormat;
50
51
52
53
54
55
56 public class ImportPage extends ExportPage {
57
58
59
60
61 private static final long serialVersionUID = 222L;
62
63
64
65
66 private static Logger log = LoggerFactory.getLogger(ImportPage.class);
67
68 private Document mgnlFileImport;
69
70 private String mgnlRedirect;
71
72 private int mgnlUuidBehavior;
73
74
75
76
77
78
79 public ImportPage(String name, HttpServletRequest request, HttpServletResponse response) {
80 super(name, request, response);
81 }
82
83
84
85
86 public String getCommand() {
87 if ("POST".equals(getRequest().getMethod())) {
88 return "importxml";
89 }
90 return super.getCommand();
91 }
92
93
94
95
96
97 public Document getMgnlFileImport() {
98 return this.mgnlFileImport;
99 }
100
101
102
103
104
105 public void setMgnlFileImport(Document mgnlFileImport) {
106 this.mgnlFileImport = mgnlFileImport;
107 }
108
109
110
111
112
113 public String getMgnlRedirect() {
114 return this.mgnlRedirect;
115 }
116
117
118
119
120
121 public void setMgnlRedirect(String mgnlRedirect) {
122 this.mgnlRedirect = mgnlRedirect;
123 }
124
125
126
127
128
129 public int getMgnlUuidBehavior() {
130 return this.mgnlUuidBehavior;
131 }
132
133
134
135
136
137 public void setMgnlUuidBehavior(int mgnlUuidBehavior) {
138 this.mgnlUuidBehavior = mgnlUuidBehavior;
139 }
140
141
142
143
144 public String importxml() throws Exception {
145
146 if (log.isDebugEnabled()) {
147 log.debug("Import request received.");
148 }
149
150 if (StringUtils.isEmpty(mgnlRepository)) {
151 mgnlRepository = ContentRepository.WEBSITE;
152 }
153 if (StringUtils.isEmpty(mgnlPath)) {
154 mgnlPath = "/";
155 }
156
157 if (!checkPermissions(request, mgnlRepository, mgnlPath, Permission.WRITE)) {
158
159 AlertUtil.setMessage("Write permission needed for export. User not allowed to WRITE path [" + mgnlPath + "]");
160
161 throw new ServletException(new AccessDeniedException(
162 "Write permission needed for import. User not allowed to WRITE path ["
163 + mgnlPath
164 + "]"));
165 }
166
167 DataTransporter.importDocument(
168 mgnlFileImport,
169 mgnlRepository,
170 mgnlPath,
171 mgnlKeepVersions,
172 mgnlUuidBehavior,
173 true,
174 true);
175
176 log.info("Import done");
177
178 if (StringUtils.isNotBlank(mgnlRedirect)) {
179 if (log.isInfoEnabled()) {
180 log.info(MessageFormat.format("Redirecting to [{0}]",
181 new Object[]{mgnlRedirect}));
182 }
183 response.sendRedirect(mgnlRedirect);
184 return null;
185 }
186 return this.show();
187 }
188
189 }