1 package org.kuali.ole.web;
2
3 import com.google.common.io.CharStreams;
4 import gov.loc.repository.bagit.utilities.FormatHelper;
5 import org.apache.commons.fileupload.FileItem;
6 import org.apache.commons.fileupload.disk.DiskFileItemFactory;
7 import org.apache.commons.fileupload.servlet.ServletFileUpload;
8 import org.apache.commons.io.FileUtils;
9 import org.apache.commons.lang.StringUtils;
10 import org.kuali.ole.docstore.common.document.BibMarc;
11 import org.kuali.ole.docstore.common.document.License;
12 import org.kuali.ole.docstore.common.document.LicenseAttachment;
13 import org.kuali.ole.docstore.common.document.Licenses;
14 import org.kuali.ole.docstore.common.exception.DocstoreException;
15 import org.kuali.ole.docstore.common.exception.DocstoreExceptionProcessor;
16 import org.kuali.ole.docstore.common.service.DocstoreService;
17 import org.kuali.ole.docstore.service.BeanLocator;
18 import org.kuali.ole.utility.CompressUtils;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 import javax.servlet.ServletException;
23 import javax.servlet.http.HttpServlet;
24 import javax.servlet.http.HttpServletRequest;
25 import javax.servlet.http.HttpServletResponse;
26 import java.io.File;
27 import java.io.IOException;
28 import java.io.PrintWriter;
29 import java.util.ArrayList;
30 import java.util.Iterator;
31 import java.util.List;
32
33
34
35
36
37
38
39
40 public class LicenseRestServlet extends HttpServlet {
41
42 private static final Logger LOG = LoggerFactory.getLogger(LicenseRestServlet.class);
43 private CompressUtils compressUtils = new CompressUtils();
44 DocstoreService ds = BeanLocator.getDocstoreService();
45 private static String responseUrl = "documentrest/license/";
46 private String extractFilePath = FileUtils.getTempDirectoryPath() + File.separator + "bagit";
47
48 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
49
50 PrintWriter out = resp.getWriter();
51 resp.setContentType("text/xml;charset=UTF-8");
52 String result = "";
53 Licenses licenses = null;
54 try {
55 ArrayList<File> files = extractBagFilesFromRequest(req, resp);
56 for(File file : files) {
57 if (file.getName().equalsIgnoreCase("licenses.xml")) {
58 String licensesXml = FileUtils.readFileToString(file);
59 licenses = (Licenses) Licenses.deserialize(licensesXml);
60 for(License license : licenses.getLicenses()) {
61 if(!license.getFormat().equals("onixpl")) {
62 LicenseAttachment licenseAttachment = (LicenseAttachment) license;
63 licenseAttachment.setFilePath(file.getParent());
64 }
65 }
66 }
67 }
68 ds.createLicenses(licenses);
69 compressUtils.deleteFiles(files);
70 File extractFile = new File(extractFilePath);
71 extractFile.delete();
72 } catch (Exception e) {
73 LOG.error("EXception : ", e);
74 }
75 StringBuffer ids = new StringBuffer();
76 for(License license : licenses.getLicenses()) {
77 ids.append(license.getId());
78 ids.append("/");
79 }
80
81 out.write(responseUrl+ ids.substring(0, (ids.length()-1)));
82 }
83
84 @Override
85 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
86 PrintWriter out = resp.getWriter();
87 resp.setContentType("text/xml;charset=UTF-8");
88 String result = "";
89 try {
90 result = retrieveLicense(req);
91 out.print(result);
92 } catch (DocstoreException de) {
93 LOG.error("Exception :", de);
94 out.print(DocstoreExceptionProcessor.toXml(de));
95 } catch (Exception e) {
96 LOG.error("Exception :", e);
97 out.print(e);
98 }
99 }
100
101 protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
102 PrintWriter out = resp.getWriter();
103 resp.setContentType("text/xml;charset=UTF-8");
104 String result = "";
105
106 try {
107 result = deleteLicense(req);
108 out.print(result);
109 } catch (DocstoreException de) {
110 LOG.error("Exception :", de);
111 out.print(DocstoreExceptionProcessor.toXml(de));
112 } catch (Exception e) {
113 LOG.error("Exception :", e);
114 out.print(e);
115 }
116 }
117
118 private String deleteLicense(HttpServletRequest req) {
119 DocstoreService ds = BeanLocator.getDocstoreService();
120 String licenseId = getIdFromPathInfo(req.getPathInfo());
121 ds.deleteLicense(licenseId);
122 return "Success";
123 }
124
125
126
127 private String retrieveLicense(HttpServletRequest req) {
128 DocstoreService ds = BeanLocator.getDocstoreService();
129 String id = getIdFromPathInfo(req.getPathInfo());
130 if (id.contains("licenseIds")) {
131 String[] splitString = id.split("=");
132 String[] licenseIds = splitString[1].split(",");
133 List<String> licenseIdList = new ArrayList<String>();
134 for (String bibId : licenseIds) {
135 licenseIdList.add(bibId);
136 }
137 Licenses licenses = null;
138 licenses = ds.retrieveLicenses(licenseIdList);
139 return Licenses.serialize(licenses);
140
141 } else {
142 License license = ds.retrieveLicense(id);
143 return license.serialize(license);
144 }
145
146 }
147
148
149 protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
150
151 PrintWriter out = resp.getWriter();
152 resp.setContentType("text/xml;charset=UTF-8");
153 String result = "";
154 try {
155 if (req.getPathInfo().startsWith("/trees")) {
156 result = updateLicenses(req);
157 } else {
158 result = updateLicense(req);
159 }
160 out.print(result);
161 } catch (DocstoreException de) {
162 LOG.error("Exception :", de);
163 out.print(DocstoreExceptionProcessor.toXml(de));
164 } catch (Exception e) {
165 LOG.error("Exception :", e);
166 out.print(e);
167 }
168 }
169
170 private String updateLicense(HttpServletRequest req) throws IOException {
171 DocstoreService ds = BeanLocator.getDocstoreService();
172 String requestBody = CharStreams.toString(req.getReader());
173
174 License license = new License();
175 License licenseObj = (License) license.deserialize(requestBody);
176 ds.updateLicense(licenseObj);
177 return responseUrl + licenseObj.getId();
178
179 }
180
181 private String updateLicenses(HttpServletRequest req) throws IOException {
182 DocstoreService ds = BeanLocator.getDocstoreService();
183 String requestBody = CharStreams.toString(req.getReader());
184 Licenses licenses = (Licenses) Licenses.deserialize(requestBody);
185 ds.updateLicenses(licenses);
186 return "";
187 }
188
189
190 private ArrayList<File> extractBagFilesFromRequest(HttpServletRequest req, HttpServletResponse res)
191 throws Exception {
192 File targetDir = null;
193 try {
194 File file = null;
195 DiskFileItemFactory fileItemFactory = new DiskFileItemFactory();
196 fileItemFactory.setSizeThreshold(1 * 1024 * 1024);
197 Iterator items = new ServletFileUpload(fileItemFactory).parseRequest(req).iterator();
198 while (items.hasNext()) {
199 FileItem item = (FileItem) items.next();
200 file = new File(FileUtils.getTempDirectory(), item.getName());
201 item.write(file);
202 }
203 targetDir = compressUtils.extractZippedBagFile(file.getAbsolutePath(), extractFilePath);
204 LOG.info("extractedBagFileLocation " + targetDir);
205 } catch (IOException e) {
206 LOG.error("IOException", e);
207
208 } catch (FormatHelper.UnknownFormatException unknownFormatException) {
209 LOG.error("unknownFormatException", unknownFormatException);
210
211 }
212 return compressUtils.getAllFilesList(targetDir);
213 }
214
215 private String getIdFromPathInfo(String pathInfo) {
216 String id = "";
217 if (StringUtils.isNotEmpty(pathInfo)) {
218 int length = pathInfo.length();
219 if (pathInfo.endsWith("/")) {
220 pathInfo = pathInfo.substring(0, length - 1);
221 }
222 id = pathInfo.substring(pathInfo.lastIndexOf("/") + 1);
223 }
224 return id;
225 }
226
227 }