Integrating Oracle ADF application with Oracle Document Cloud service (DCS)

JSP File:

<!DOCTYPE html>
<%@ page contentType="text/html;charset=UTF-8"%>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <title>view</title>
        <script>
            function OnMessage(evt) {
                /*Created By Sheka 14042015, Attach cloud document to my inline frame*/
                if (evt.data.message === 'appLinkReady') {
                  var iframe = document.getElementById('if1');
                  var iframewindow = iframe.contentWindow ? iframe.contentWindow : iframe.contentDocument.defaultView;
                  var dAppLinkRefreshToken = document.getElementById('refreshTokenField').value;
                  var dAppLinkAccessToken = document.getElementById('accessTokenField').value;
                  var dAppLinkRoleName = document.getElementById('roleField').value;
                  var embedPreview = true;
               
                  var msg = {
                      message : 'setAppLinkTokens', appLinkRefreshToken : dAppLinkRefreshToken, appLinkAccessToken : dAppLinkAccessToken, appLinkRoleName : dAppLinkRoleName, embedPreview : embedPreview
                  };
                  iframewindow.postMessage(msg, '*');
              }
            }
            function assignMessageListener(){
                window.addEventListener('message', OnMessage, false);
            }
        </script>
    </head>
    <body onload="assignMessageListener();">
    <iframe id="if1" src="${sessionScope.frameSource}" width="100%" height="700px"></iframe>
    <input type="hidden" id="frameSourceField" value="${sessionScope.frameSource}" size="500"/>
    <input type="hidden" id="refreshTokenField" value="${sessionScope.refreshToken}" size="500"/>
    <input type="hidden" id="accessTokenField" value="${sessionScope.accessToken}" size="500"/>
    <input type="hidden" id="roleField" value="${sessionScope.role}" size="500"/>
 
    </body>
</html>

-----------------------------------------------------------
managed bean
-----------------
package org.dhaman.view.bean;
import org.apache.myfaces.trinidad.util.Service;
import documents.cloud.globals.MetaData;
import documents.cloud.pojos.AppLink;
import documents.cloud.restsamples.CreateFileAppLink;
import documents.cloud.restsamples.PostUploadFile;

import java.io.File;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import java.io.OutputStream;

import java.util.Properties;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import oracle.adf.view.rich.component.rich.RichPopup;

import org.apache.myfaces.trinidad.model.UploadedFile;
import org.apache.myfaces.trinidad.render.ExtendedRenderKitService;

public class Cloud {
    private String frameSource;
    private String protocole = "https";
    private String host = "dhaman-doc.documents.us2.oraclecloud.com";
    private String contextroot = "documents";
    private String username;
    private String password;
    private String fileId;
    private String fileName = "testFile.pdf";
    private UploadedFile uploadedFile;  
    ////// metadata of uploaded file
    private String uploadedFileName;
    private String owner;
    private String creatoe;
    private String cloudFileId;
    private String version;
    private String url;
    private String refreshToken;
      private String accessToken;
      private String role;

    public void setRefreshToken(String refreshToken) {
        this.refreshToken = refreshToken;
    }

    public String getRefreshToken() {
        return refreshToken;
    }

    public void setAccessToken(String accessToken) {
        this.accessToken = accessToken;
    }

    public String getAccessToken() {
        return accessToken;
    }

    public void setRole(String role) {
        this.role = role;
    }

    public String getRole() {
        return role;
    }


    public Cloud() {
    }

    public String loginAction() {
        return "success";
    }

    public String viewFileAction() {
        FacesContext ctx = getFacesContext();
        HttpSession session = (HttpSession)ctx.getExternalContext().getSession(true);
             
        AppLink al = CreateFileAppLink.getFileUrl(getFileId());
              System.out.println(al);
              setFrameSource(al.getAppLinkUrl());
              setRefreshToken(al.getRefreshToken());
              setAccessToken(al.getAccessToken());
              setRole(al.getRole());
           
        session.setAttribute("frameSource", getFrameSource());
        session.setAttribute("refreshToken", getRefreshToken());
        session.setAttribute("accessToken", getAccessToken());
        session.setAttribute("role", getRole());
     
        FacesContext context = FacesContext.getCurrentInstance();
                    HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
                    String targetURL = request.getRequestURL().toString();
                    targetURL = targetURL.substring(0, targetURL.lastIndexOf("/") + 1) + "view.jsp";
                    System.out.println(targetURL);
                    ExtendedRenderKitService service = Service.getRenderKitService(context, ExtendedRenderKitService.class);
                    String script =
                        "window.open('" + targetURL + "', '', 'location=0, status=0, resizable=1, scrollbars=0');";
                    service.addScript(FacesContext.getCurrentInstance(), script);
           
        return "viewer";
    }

    /**
     * @return null
     * This method is used to upoad file to oracle cloud
     * @author: tarek fathy bakr
     * @since 2015 april 01
     */
    public String uploadFileToCloud() {
        addFacesInformationMessage("Start");
        System.out.println("Tarek");
        if (this.getUploadedFile() != null) {
       
            Properties props = new Properties();
            props.put("protocol", getProtocole());
            props.put("host", getHost());
            props.put("contextroot", getContextroot());
            props.put("username", getUsername());
            props.put("password", getPassword());        
            File f = getFileFromUploadedFile(getUploadedFile());        
            PostUploadFile.transferFile(f, props);
        }
        return null;
    }
    private File getFileFromUploadedFile(UploadedFile uploadedFile) {
        System.out.println(uploadedFile.getFilename());
        File file = new File(uploadedFile.getFilename());
        InputStream inputStream = null;
        OutputStream outputStream = null;
        try {
            inputStream = uploadedFile.getInputStream();
            outputStream = new FileOutputStream(file);
            int read = 0;
            byte[] bytes = new byte[1024];
            while ((read = inputStream.read(bytes)) != -1) {
                outputStream.write(bytes, 0, read);
            }

            System.out.println("Done!");


        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (outputStream != null) {
                try {                  
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }
        return file;
    }

 

    public void setFrameSource(String frameSource) {
        this.frameSource = frameSource;
    }

    public String getFrameSource() {
        return frameSource;
    }

    public void setProtocole(String protocole) {
        this.protocole = protocole;
    }

    public String getProtocole() {
        return protocole;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public String getHost() {
        return host;
    }

    public void setContextroot(String contextroot) {
        this.contextroot = contextroot;
    }

    public String getContextroot() {
        return contextroot;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getUsername() {
        return username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPassword() {
        return password;
    }

    public void setFileId(String fileId) {
        this.fileId = fileId;
    }

    public String getFileId() {
        return fileId;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public String getFileName() {
        return fileName;
    }

    public void setUploadedFile(UploadedFile uploadedFile) {
        this.uploadedFile = uploadedFile;
    }

    public UploadedFile getUploadedFile() {
        return uploadedFile;
    }

///////////////// meta data of file
    public String getUploadedFileName() {
        return MetaData.getFilename();
    }


    public String getOwner() {
        return MetaData.getOwner();
    }

    public String getCreatoe() {
        return MetaData.getCreator();
    }


    public String getCloudFileId() {
        return MetaData.getFileId();
    }



    public String getVersion() {
        return MetaData.getVersion();
    }
    public String getUrl() {
        if(getFileId()!=null){
        return getProtocole() + "//" + getHost() + "/" + getContextroot() + "/fileview/" + getFileId() + "/_" +
                       getUploadedFile().getFilename();
        }else{
            return null;
        }
    }
    public static void addFacesInformationMessage(String msg) {
        FacesContext ctx = getFacesContext();
        FacesMessage fm = new FacesMessage(FacesMessage.SEVERITY_INFO, msg, "");
        ctx.addMessage(getRootViewComponentId(), fm);
    }
    public static FacesContext getFacesContext() {
        return FacesContext.getCurrentInstance();
    }
    public static String getRootViewComponentId() {
        return getFacesContext().getViewRoot().getId();
    }
}

Comments

Popular posts from this blog

Another user has changed the row with primary key oracle.jbo.Key[12 ].

Working With File Throgh WebUtill

weblogic windows JPS-01050: Opening of wallet based credential store failed. Reason java.io.IOException: Failed to lock cwallet.sso.lck