ossiejhmoore

OT WebService - Upload Using HTTP Streaming

Mar 9th, 2017
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.92 KB | None | 0 0
  1.     /**
  2.      * Takes a bit more work but this technique uses much smaller amounts of RAM.
  3.      * Additionally, it will work with any size file. For example, I have used
  4.      * this technique to upload 50GB files using tiny amounts of RAM. Here, we
  5.      * are uploading a file to the user's personal workspace.
  6.      */
  7.     @SuppressWarnings({"ConvertToTryWithResources", "CallToPrintStackTrace"})
  8.     private static void hybridWebSerivceWithHttpPost() throws Exception {
  9.  
  10.         final URL wsdl = new URL(props.getProperty("wsdl.Authentication"));
  11.         System.out.printf("[hybridWebSerivceWithHttpPost] Demonstrate upload using hybrid method to %s.%n",
  12.                 InetAddress.getByName(wsdl.getHost()));
  13.  
  14.         final File file = new File(props.getProperty("path.smallToMediumSizeFile"));
  15.         final byte[] chunk = new byte[1024000];
  16.         for (int i = 0; i < chunk.length; i++) {
  17.             chunk[i] = ((byte)(((int)(Math.random()*26.0))+65));
  18.         }
  19.         int loops = 100;
  20.  
  21.         String authToken = getAuthenticationToken(wsdlAuthentication, username, password);
  22.         OTAuthentication otAuth = getOTAuthentication(authToken);
  23.         DocumentManagement svcDocMan = getDocumentManagementClient(wsdlDocumentManagement, otAuth);
  24.         long expire = System.currentTimeMillis() + (1000 * 60 * 5);
  25.  
  26.         //get reference to user's personal workspace
  27.         final com.opentext.livelink.service.docman.Node pw = svcDocMan.getRootNode("PersonalWS");
  28.  
  29.         System.out.printf("[hybridWebSerivceWithHttpPost] [%s] Initialize upload file.%n", sdf.format(new Date()));
  30.         final OutputStream os = new FileOutputStream(file);
  31.         for (int i = 0; i < loops; i++) {
  32.             os.write(chunk);
  33.             os.flush();
  34.         }
  35.         os.close();
  36.  
  37.         while (true) {
  38.             if (System.currentTimeMillis() > expire) {
  39.                 System.out.printf("[hybridWebSerivceWithHttpPost] [%s] Refresh web service session.%n", sdf.format(new Date()));
  40.                 authToken = getAuthenticationToken(wsdlAuthentication, username, password);
  41.                 otAuth = getOTAuthentication(authToken);
  42.                 svcDocMan = getDocumentManagementClient(wsdlDocumentManagement, otAuth);
  43.                 expire = System.currentTimeMillis() + (1000 * 60 * 5);
  44.             }
  45.             System.out.printf("[hybridWebSerivceWithHttpPost] Upload %sMB file %s%n", (file.length()/(1024000.0)), file.getAbsolutePath());
  46.             final String name = String.format("example-document-%s", System.nanoTime());
  47.             final String comment = null; //null or empty if no comment.
  48.             final Metadata meta = null; //null if inheriting categories, etc.
  49.             final String contextId = svcDocMan.createDocumentContext(pw.getID(), name, comment, Boolean.FALSE, meta);
  50.             final long contextIdAge = System.currentTimeMillis();
  51.             final URL uri = getHttpUrl(authToken, contextId, file.getName(), file.length());
  52.             final HttpURLConnection urlCon = (HttpURLConnection) (uri.openConnection());
  53.             urlCon.setDoOutput(true);
  54.             urlCon.setDoInput(true);
  55.             urlCon.setUseCaches(false);
  56.             urlCon.setRequestMethod("POST");
  57.             urlCon.setRequestProperty("Content-Type", "application/octet-stream");
  58.             urlCon.setRequestProperty("Content-Length", String.valueOf(file.length()));
  59.             urlCon.setFixedLengthStreamingMode(file.length());
  60.             urlCon.setReadTimeout(Integer.MAX_VALUE);
  61.             urlCon.connect();
  62.  
  63.             final InputStream fileStream = new FileInputStream(file);
  64.             final OutputStream uploadStream = urlCon.getOutputStream();
  65.  
  66.             int bytesRead;
  67.             long sent = 0;
  68.             int blocksize = 1024000;
  69.             final byte[] buffer = new byte[blocksize];
  70.             try {
  71.                 int counter = 0;
  72.                 while ((bytesRead = fileStream.read(buffer)) > 0) {
  73.                     sent += bytesRead;
  74.                     uploadStream.write(buffer, 0, bytesRead);
  75.                     uploadStream.flush();
  76.                     counter++;
  77.                     if (counter % 1 == 0) {
  78.                         System.out.print(".");
  79.                         if (counter % 10 == 0) {
  80.                             System.out.printf("%s %s contextIdAge: %s, sessionAge: %s%n", counter, sent, System.currentTimeMillis() - contextIdAge, System.currentTimeMillis() - (expire - (1000 * 60 * 5)));
  81.                         }
  82.                     }
  83.                 }
  84.                 fileStream.close();
  85.                 uploadStream.close();
  86.  
  87.                 if (urlCon.getResponseCode() == 200) {
  88.                     final InputStream inStream = new BufferedInputStream(urlCon.getInputStream());
  89.                     final Integer objectId = parseHttpResponse(inStream);
  90.                     inStream.close();
  91.                     final com.opentext.livelink.service.docman.Node document = svcDocMan.getNode(objectId);
  92.                     System.out.printf("%n[hybridWebSerivceWithHttpPost] Created object %s (%s) using %s.%n",
  93.                             document.getName(), document.getID(), file.getAbsolutePath());
  94.                     int dataid = document.getID();
  95.                     System.out.printf("[hybridWebSerivceWithHttpPost] [%s] Delete object %s (%s).%n",
  96.                             sdf.format(new Date()), name, dataid, file.getAbsolutePath());
  97.                     svcDocMan.deleteNode(dataid);
  98.                     System.out.printf("[hybridWebSerivceWithHttpPost] [%s] Recycle object %s (%s).%n",
  99.                             sdf.format(new Date()), name, dataid, file.getAbsolutePath());
  100.                     svcDocMan.deleteNode(dataid);
  101.                 } else {
  102.                     System.out.printf("%n[hybridWebSerivceWithHttpPost] Error: %s - %s%n", urlCon.getResponseCode(), urlCon.getResponseMessage());
  103.                 }
  104.             } catch (Exception e) {
  105.                 System.out.printf("%n[hybridWebSerivceWithHttpPost] Error: %s%n", e.getLocalizedMessage());
  106.                 e.printStackTrace();
  107.             }
  108.             System.out.printf("[hybridWebSerivceWithHttpPost] [%s] Swell file.%n", sdf.format(new Date()));
  109.  
  110.             final RandomAccessFile raf = new RandomAccessFile(file, "rw");
  111.             raf.seek(raf.length());
  112.             for (int i = 0; i < loops; i++) {
  113.                 raf.write(chunk);
  114.             }
  115.             raf.close();
  116.         }
  117.     }
  118.  
  119.  
  120.     private static URL getHttpUrl(String token, String contextId, String fileName, long fileSize) throws Exception {
  121.         StringBuilder uri = new StringBuilder(props.getProperty("http.ContentHandler"));
  122.         uri.append(String.format("?token=%s&contextID=%s",
  123.                 URLEncoder.encode(token, "UTF-8"), contextId));
  124.         if (fileName != null) {
  125.             uri.append(String.format("&fileName=%s&fileSize=%s", fileName, fileSize));
  126.         }
  127.         return new URL(uri.toString());
  128.     }
Add Comment
Please, Sign In to add comment