View Javadoc

1   package org.kuali.ole.service;
2   
3   import org.apache.commons.io.FileUtils;
4   import org.apache.commons.io.IOUtils;
5   import org.apache.commons.lang.StringUtils;
6   import org.apache.http.HttpEntity;
7   import org.apache.http.HttpResponse;
8   import org.apache.http.client.HttpClient;
9   import org.apache.http.client.methods.HttpPost;
10  import org.apache.http.entity.mime.MultipartEntity;
11  import org.apache.http.entity.mime.content.FileBody;
12  import org.apache.http.impl.client.DefaultHttpClient;
13  import org.kuali.ole.docstore.OleException;
14  import org.kuali.ole.docstore.model.xmlpojo.ingest.Request;
15  import org.kuali.ole.docstore.model.xmlpojo.ingest.Response;
16  import org.kuali.ole.docstore.model.xstream.ingest.RequestHandler;
17  import org.kuali.ole.docstore.model.xstream.ingest.ResponseHandler;
18  import org.kuali.ole.docstore.util.PropertyUtil;
19  import org.kuali.ole.utility.CompressUtils;
20  import org.slf4j.Logger;
21  import org.slf4j.LoggerFactory;
22  
23  import java.io.File;
24  import java.io.FileOutputStream;
25  import java.io.IOException;
26  import java.io.InputStream;
27  import java.util.ArrayList;
28  import java.util.List;
29  
30  /**
31   * MultiPartBagRequestClient to run all requests in a given folder for Handling Multi Part Zip Bag contents.
32   *
33   * @author Rajesh Chowdary K
34   * @created Jun 6, 2012
35   */
36  public class MultiPartBagRequestClient {
37      private Logger logger = LoggerFactory.getLogger(MultiPartBagRequestClient.class);
38      private static String dsDocumentURL = "http://localhost:9080/oledocstore/document";
39      private ResponseHandler rh = new ResponseHandler();
40  
41      static {
42          dsDocumentURL = PropertyUtil.getPropertyUtil().getProperty("docstore.document.url");
43      }
44  
45      /**
46       * Method to run MultiPart Requests @ Location 'requestsRootFolder'.
47       *
48       * @param requestsRootFolder
49       * @return
50       * @throws OleException
51       */
52      public List<Response> runMultiPartRequestsAtLocation(String requestsRootFolder, String restUrl) throws OleException, IOException {
53          boolean requestFound = false;
54          String operation = null;
55          List<Response> responses = new ArrayList<Response>();
56          File folder = new File(requestsRootFolder);
57          if (!folder.exists() || !folder.isDirectory())
58              throw new OleException("Given Path '" + requestsRootFolder + "' does not Exists or may not be a directory path");
59          File[] subFolders = folder.listFiles();
60          for (File file : subFolders)
61              if (file.isFile() && file.getName().equalsIgnoreCase("request.xml")) {
62                  operation = getOperation(file);
63                  responses.add(processRequest(file.getParentFile(), restUrl, operation));
64                  requestFound = true;
65              } else if (file.isDirectory() && new File(file.getAbsolutePath() + File.separator + "request.xml").exists()) {
66                  operation = getOperation(file);
67                  responses.add(processRequest(file, restUrl, operation));
68                  requestFound = true;
69              }
70          if (!requestFound)
71              throw new OleException("Given Folder '" + requestsRootFolder + "' don't Contains any Multi Part Requests");
72  
73          return responses;
74      }
75  
76      private String getOperation(File file) throws IOException {
77          String operation = null;
78          String fileContent = FileUtils.readFileToString(file);
79          logger.debug("file content in string -->" + fileContent);
80          RequestHandler requestHandler = new RequestHandler();
81          Request request = requestHandler.toObject(fileContent);
82          operation = request.getOperation();
83          logger.debug("operation--->" + operation);
84          return operation;
85  
86      }
87  
88      private Response processRequest(File folder, String restUrl, String operation) {
89          logger.info("processing request @ " + folder.getAbsolutePath());
90          Response dsResponse = null;
91          try {
92              CompressUtils compressUtils = new CompressUtils();
93              HttpPost httpPost = null;
94              HttpResponse response;
95              File reqFile = compressUtils.createZippedBagFile(folder);
96              if (!StringUtils.isBlank(restUrl)) {
97                  httpPost = new HttpPost(restUrl);
98              } else {
99                  httpPost = new HttpPost(dsDocumentURL);
100             }
101             HttpClient httpclient = new DefaultHttpClient();
102             FileBody uploadFilePart = new FileBody(reqFile);
103             MultipartEntity reqEntity = new MultipartEntity();
104             reqEntity.addPart("upload-file", uploadFilePart);
105             httpPost.setEntity(reqEntity);
106             response = httpclient.execute(httpPost);
107             reqFile.delete();
108             HttpEntity respEntity = response.getEntity();
109             InputStream outcome = respEntity.getContent();
110             File respZipFile = File.createTempFile("ds-", "-Resp.zip");
111             FileOutputStream fos = new FileOutputStream(respZipFile);
112             IOUtils.copy(outcome, fos);
113             fos.close();
114             File responseFolder = new File(folder.getAbsolutePath() + File.separator + "response-" + System.currentTimeMillis());
115             responseFolder.mkdirs();
116             compressUtils.extractZippedBagFile(respZipFile.getAbsolutePath(), responseFolder.getAbsolutePath());
117             respZipFile.delete();
118             dsResponse = rh.toObject(FileUtils.readFileToString(new File(responseFolder.getAbsolutePath() + File.separator + "response.xml")));
119             dsResponse.setStatusMessage("Processed Request @" + folder.getAbsolutePath() + " , It's Response is @" + responseFolder.getAbsolutePath()
120                     + " & The Result is: " + dsResponse.getStatusMessage());
121         } catch (Exception e) {
122             dsResponse = new Response();
123             dsResponse.setStatus("Failure");
124             dsResponse.setStatusMessage("Operation @ " + folder.getAbsolutePath() + " Failed, Cause: " + e.getMessage()
125                     + "\n check Logs for futher information");
126             logger.error("Operation for Processing Request @ " + folder.getAbsolutePath() + " Failed, Cause: " + e.getMessage(), e);
127         }
128 
129         return dsResponse;
130     }
131 }