1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.kuali.rice.kew.batch.web; |
18 | |
|
19 | |
import org.apache.log4j.Logger; |
20 | |
import org.apache.struts.action.ActionForm; |
21 | |
import org.apache.struts.action.ActionForward; |
22 | |
import org.apache.struts.action.ActionMapping; |
23 | |
import org.apache.struts.upload.FormFile; |
24 | |
import org.kuali.rice.kew.batch.*; |
25 | |
import org.kuali.rice.kew.service.KEWServiceLocator; |
26 | |
import org.kuali.rice.kew.util.Utilities; |
27 | |
import org.kuali.rice.kew.web.session.UserSession; |
28 | |
import org.kuali.rice.kim.bo.types.dto.AttributeSet; |
29 | |
import org.kuali.rice.kim.service.KIMServiceLocator; |
30 | |
import org.kuali.rice.kim.util.KimCommonUtils; |
31 | |
import org.kuali.rice.kim.util.KimConstants; |
32 | |
import org.kuali.rice.kns.exception.AuthorizationException; |
33 | |
import org.kuali.rice.kns.util.KNSConstants; |
34 | |
import org.kuali.rice.kns.web.struts.action.KualiAction; |
35 | |
|
36 | |
import javax.servlet.http.HttpServletRequest; |
37 | |
import javax.servlet.http.HttpServletResponse; |
38 | |
import java.io.File; |
39 | |
import java.io.FileOutputStream; |
40 | |
import java.io.IOException; |
41 | |
import java.util.ArrayList; |
42 | |
import java.util.Collection; |
43 | |
import java.util.List; |
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | 0 | public class IngesterAction extends KualiAction { |
53 | 0 | private static final Logger LOG = Logger.getLogger(IngesterAction.class); |
54 | |
|
55 | |
public ActionForward execute( |
56 | |
ActionMapping mapping, |
57 | |
ActionForm form, |
58 | |
HttpServletRequest request, |
59 | |
HttpServletResponse response) |
60 | |
throws Exception { |
61 | |
|
62 | 0 | checkAuthorization(form, ""); |
63 | |
|
64 | 0 | LOG.debug(request.getMethod()); |
65 | 0 | if (!"post".equals(request.getMethod().toLowerCase())) { |
66 | 0 | LOG.debug("returning to view"); |
67 | 0 | return mapping.findForward("view"); |
68 | |
} |
69 | |
|
70 | 0 | IngesterForm iform = (IngesterForm) form; |
71 | |
|
72 | 0 | List<String> messages = new ArrayList<String>(); |
73 | 0 | List<File> tempFiles = new ArrayList<File>(); |
74 | |
try { |
75 | 0 | Collection<FormFile> files = iform.getFiles(); |
76 | 0 | List<XmlDocCollection> collections = new ArrayList<XmlDocCollection>(files.size()); |
77 | 0 | LOG.debug(files); |
78 | 0 | LOG.debug("" + files.size()); |
79 | |
|
80 | 0 | for (FormFile file1 : files) |
81 | |
{ |
82 | 0 | if (file1.getFileName() == null || file1.getFileName().length() == 0) continue; |
83 | 0 | if (file1.getFileData() == null) |
84 | |
{ |
85 | 0 | messages.add("File '" + file1.getFileName() + "' contained no data"); |
86 | 0 | continue; |
87 | |
} |
88 | 0 | LOG.debug("Processing file: " + file1.getFileName()); |
89 | |
|
90 | |
|
91 | |
|
92 | 0 | FileOutputStream fos = null; |
93 | 0 | File temp = null; |
94 | |
try |
95 | |
{ |
96 | 0 | temp = File.createTempFile("ingester", null); |
97 | 0 | tempFiles.add(temp); |
98 | 0 | fos = new FileOutputStream(temp); |
99 | 0 | fos.write(file1.getFileData()); |
100 | 0 | } catch (IOException ioe) |
101 | |
{ |
102 | 0 | messages.add("Error copying file data for '" + file1.getFileName() + "': " + ioe); |
103 | 0 | continue; |
104 | |
} finally |
105 | |
{ |
106 | 0 | if (fos != null) try |
107 | |
{ |
108 | 0 | fos.close(); |
109 | 0 | } catch (IOException ioe) |
110 | |
{ |
111 | 0 | LOG.error("Error closing temp file output stream: " + temp, ioe); |
112 | 0 | } |
113 | 0 | } |
114 | 0 | if (file1.getFileName().toLowerCase().endsWith(".zip")) |
115 | |
{ |
116 | |
try |
117 | |
{ |
118 | 0 | collections.add(new ZipXmlDocCollection(temp)); |
119 | 0 | } catch (IOException ioe) |
120 | |
{ |
121 | 0 | String message = "Unable to load file: " + file1; |
122 | 0 | LOG.error(message); |
123 | 0 | messages.add(message); |
124 | 0 | } |
125 | 0 | } else if (file1.getFileName().endsWith(".xml")) |
126 | |
{ |
127 | 0 | collections.add(new FileXmlDocCollection(temp, file1.getFileName())); |
128 | |
} else |
129 | |
{ |
130 | 0 | messages.add("Ignoring extraneous file: " + file1.getFileName()); |
131 | |
} |
132 | 0 | } |
133 | |
|
134 | 0 | if (collections.size() == 0) { |
135 | 0 | String message = "No valid files to ingest"; |
136 | 0 | LOG.debug(message); |
137 | 0 | messages.add(message); |
138 | 0 | } else { |
139 | |
|
140 | 0 | CompositeXmlDocCollection compositeCollection = new CompositeXmlDocCollection(collections); |
141 | 0 | int totalProcessed = 0; |
142 | 0 | List<XmlDocCollection> c = new ArrayList<XmlDocCollection>(1); |
143 | 0 | c.add(compositeCollection); |
144 | |
try { |
145 | 0 | Collection<XmlDocCollection> failed = KEWServiceLocator.getXmlIngesterService().ingest(c, UserSession.getAuthenticatedUser().getPrincipal().getPrincipalId()); |
146 | 0 | boolean txFailed = failed.size() > 0; |
147 | 0 | if (txFailed) { |
148 | 0 | messages.add("Ingestion failed"); |
149 | |
} |
150 | 0 | for (XmlDocCollection collection1 : collections) |
151 | |
{ |
152 | 0 | List<? extends XmlDoc> docs = collection1.getXmlDocs(); |
153 | 0 | for (XmlDoc doc1 : docs) |
154 | |
{ |
155 | 0 | if (doc1.isProcessed()) |
156 | |
{ |
157 | 0 | if (!txFailed) |
158 | |
{ |
159 | 0 | totalProcessed++; |
160 | 0 | messages.add("Ingested xml doc: " + doc1.getName() + (doc1.getProcessingMessage() == null ? "" : "\n" + doc1.getProcessingMessage())); |
161 | |
} else |
162 | |
{ |
163 | 0 | messages.add("Rolled back doc: " + doc1.getName() + (doc1.getProcessingMessage() == null ? "" : "\n" + doc1.getProcessingMessage())); |
164 | |
} |
165 | |
} else |
166 | |
{ |
167 | 0 | messages.add("Failed to ingest xml doc: " + doc1.getName() + (doc1.getProcessingMessage() == null ? "" : "\n" + doc1.getProcessingMessage())); |
168 | |
} |
169 | |
} |
170 | 0 | } |
171 | 0 | } catch (Exception e) { |
172 | 0 | String message = "Error during ingest"; |
173 | 0 | LOG.error(message, e); |
174 | 0 | messages.add(message + ": " + e + ":\n" + Utilities.collectStackTrace(e)); |
175 | 0 | } |
176 | 0 | if (totalProcessed == 0) { |
177 | 0 | String message = "No xml docs ingested"; |
178 | 0 | LOG.debug(message); |
179 | 0 | messages.add(message); |
180 | |
} |
181 | |
} |
182 | 0 | } finally { |
183 | 0 | if (tempFiles.size() > 0) { |
184 | 0 | for (File tempFile : tempFiles) |
185 | |
{ |
186 | 0 | if (!tempFile.delete()) |
187 | |
{ |
188 | 0 | LOG.warn("Error deleting temp file: " + tempFile); |
189 | |
} |
190 | |
} |
191 | |
} |
192 | 0 | } |
193 | |
|
194 | 0 | request.setAttribute("messages", messages); |
195 | 0 | return mapping.findForward("view"); |
196 | |
} |
197 | |
|
198 | |
protected void checkAuthorization( ActionForm form, String methodToCall) throws AuthorizationException |
199 | |
{ |
200 | 0 | String principalId = UserSession.getAuthenticatedUser().getPrincipalId(); |
201 | 0 | AttributeSet roleQualifier = new AttributeSet(); |
202 | 0 | AttributeSet permissionDetails = KimCommonUtils.getNamespaceAndActionClass(this.getClass()); |
203 | |
|
204 | 0 | if (!KIMServiceLocator.getIdentityManagementService().isAuthorizedByTemplateName(principalId, KNSConstants.KNS_NAMESPACE, |
205 | |
KimConstants.PermissionTemplateNames.USE_SCREEN, permissionDetails, roleQualifier )) |
206 | |
{ |
207 | 0 | throw new AuthorizationException(UserSession.getAuthenticatedUser().getPrincipalName(), |
208 | |
methodToCall, |
209 | |
this.getClass().getSimpleName()); |
210 | |
} |
211 | 0 | } |
212 | |
|
213 | |
|
214 | |
} |