ossiejhmoore icon

OT WebService - Upload Using HTTP Streaming

ossiejhmoore | PRO | 03/09/17 08:55:26 PM UTC | 0 ⭐ | 263 👁️ | Never ⏰ | []
Java |

6.92 KB

|

None

|

0 👍

/

0 👎

    /**
     * Takes a bit more work but this technique uses much smaller amounts of RAM.
     * Additionally, it will work with any size file. For example, I have used
     * this technique to upload 50GB files using tiny amounts of RAM. Here, we
     * are uploading a file to the user's personal workspace.
     */
    @SuppressWarnings({"ConvertToTryWithResources", "CallToPrintStackTrace"})
    private static void hybridWebSerivceWithHttpPost() throws Exception {
 
        final URL wsdl = new URL(props.getProperty("wsdl.Authentication"));
        System.out.printf("[hybridWebSerivceWithHttpPost] Demonstrate upload using hybrid method to %s.%n",
                InetAddress.getByName(wsdl.getHost()));
 
        final File file = new File(props.getProperty("path.smallToMediumSizeFile"));
        final byte[] chunk = new byte[1024000];
        for (int i = 0; i < chunk.length; i++) {
            chunk[i] = ((byte)(((int)(Math.random()*26.0))+65));
        }
        int loops = 100;
 
        String authToken = getAuthenticationToken(wsdlAuthentication, username, password);
        OTAuthentication otAuth = getOTAuthentication(authToken);
        DocumentManagement svcDocMan = getDocumentManagementClient(wsdlDocumentManagement, otAuth);
        long expire = System.currentTimeMillis() + (1000 * 60 * 5);
 
        //get reference to user's personal workspace
        final com.opentext.livelink.service.docman.Node pw = svcDocMan.getRootNode("PersonalWS");
 
        System.out.printf("[hybridWebSerivceWithHttpPost] [%s] Initialize upload file.%n", sdf.format(new Date()));
        final OutputStream os = new FileOutputStream(file);
        for (int i = 0; i < loops; i++) {
            os.write(chunk);
            os.flush();
        }
        os.close();
 
        while (true) {
            if (System.currentTimeMillis() > expire) {
                System.out.printf("[hybridWebSerivceWithHttpPost] [%s] Refresh web service session.%n", sdf.format(new Date()));
                authToken = getAuthenticationToken(wsdlAuthentication, username, password);
                otAuth = getOTAuthentication(authToken);
                svcDocMan = getDocumentManagementClient(wsdlDocumentManagement, otAuth);
                expire = System.currentTimeMillis() + (1000 * 60 * 5);
            }
            System.out.printf("[hybridWebSerivceWithHttpPost] Upload %sMB file %s%n", (file.length()/(1024000.0)), file.getAbsolutePath());
            final String name = String.format("example-document-%s", System.nanoTime());
            final String comment = null; //null or empty if no comment.
            final Metadata meta = null; //null if inheriting categories, etc.
            final String contextId = svcDocMan.createDocumentContext(pw.getID(), name, comment, Boolean.FALSE, meta);
            final long contextIdAge = System.currentTimeMillis();
            final URL uri = getHttpUrl(authToken, contextId, file.getName(), file.length());
            final HttpURLConnection urlCon = (HttpURLConnection) (uri.openConnection());
            urlCon.setDoOutput(true);
            urlCon.setDoInput(true);
            urlCon.setUseCaches(false);
            urlCon.setRequestMethod("POST");
            urlCon.setRequestProperty("Content-Type", "application/octet-stream");
            urlCon.setRequestProperty("Content-Length", String.valueOf(file.length()));
            urlCon.setFixedLengthStreamingMode(file.length());
            urlCon.setReadTimeout(Integer.MAX_VALUE);
            urlCon.connect();
 
            final InputStream fileStream = new FileInputStream(file);
            final OutputStream uploadStream = urlCon.getOutputStream();
 
            int bytesRead;
            long sent = 0;
            int blocksize = 1024000;
            final byte[] buffer = new byte[blocksize];
            try {
                int counter = 0;
                while ((bytesRead = fileStream.read(buffer)) > 0) {
                    sent += bytesRead;
                    uploadStream.write(buffer, 0, bytesRead);
                    uploadStream.flush();
                    counter++;
                    if (counter % 1 == 0) {
                        System.out.print(".");
                        if (counter % 10 == 0) {
                            System.out.printf("%s %s contextIdAge: %s, sessionAge: %s%n", counter, sent, System.currentTimeMillis() - contextIdAge, System.currentTimeMillis() - (expire - (1000 * 60 * 5)));
                        }
                    }
                }
                fileStream.close();
                uploadStream.close();
 
                if (urlCon.getResponseCode() == 200) {
                    final InputStream inStream = new BufferedInputStream(urlCon.getInputStream());
                    final Integer objectId = parseHttpResponse(inStream);
                    inStream.close();
                    final com.opentext.livelink.service.docman.Node document = svcDocMan.getNode(objectId);
                    System.out.printf("%n[hybridWebSerivceWithHttpPost] Created object %s (%s) using %s.%n",
                            document.getName(), document.getID(), file.getAbsolutePath());
                    int dataid = document.getID();
                    System.out.printf("[hybridWebSerivceWithHttpPost] [%s] Delete object %s (%s).%n",
                            sdf.format(new Date()), name, dataid, file.getAbsolutePath());
                    svcDocMan.deleteNode(dataid);
                    System.out.printf("[hybridWebSerivceWithHttpPost] [%s] Recycle object %s (%s).%n",
                            sdf.format(new Date()), name, dataid, file.getAbsolutePath());
                    svcDocMan.deleteNode(dataid);
                } else {
                    System.out.printf("%n[hybridWebSerivceWithHttpPost] Error: %s - %s%n", urlCon.getResponseCode(), urlCon.getResponseMessage());
                }
            } catch (Exception e) {
                System.out.printf("%n[hybridWebSerivceWithHttpPost] Error: %s%n", e.getLocalizedMessage());
                e.printStackTrace();
            }
            System.out.printf("[hybridWebSerivceWithHttpPost] [%s] Swell file.%n", sdf.format(new Date()));
 
            final RandomAccessFile raf = new RandomAccessFile(file, "rw");
            raf.seek(raf.length());
            for (int i = 0; i < loops; i++) {
                raf.write(chunk);
            }
            raf.close();
        }
    }
 
 
    private static URL getHttpUrl(String token, String contextId, String fileName, long fileSize) throws Exception {
        StringBuilder uri = new StringBuilder(props.getProperty("http.ContentHandler"));
        uri.append(String.format("?token=%s&contextID=%s",
                URLEncoder.encode(token, "UTF-8"), contextId));
        if (fileName != null) {
            uri.append(String.format("&fileName=%s&fileSize=%s", fileName, fileSize));
        }
        return new URL(uri.toString());
    }

Comments