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