Document preview in portal side using alfresco

cancel
Showing results for 
Search instead for 
Did you mean: 
kranthi
Active Member II

Document preview in portal side using alfresco

I want to preview the document in portal side using alfresco.For this I am writing webscript here I need to get document ID but how can we get that to alfresco.

9 Replies
afaust
Master

Re: Document preview in portal side using alfresco

How can you get that "to" Alfresco? I suggest using either a URL parameter, HTTP header or HTTP request body to pass data to Alfresco...

You may want to expand / clarify your original question, because currently it is not clear what you have already tried and where you are stuck, so I can only give a very general response. More context information is also usually appreciated, i.e. what portal are you using, what kind of document / preview do you want to display, is the document stored in Alfresco or in the portal, etc...

kranthi
Active Member II

Re: Document preview in portal side using alfresco

Documents are storing in Alfresco (documents are coming to alfresco after

uploading documents in portal).Now if I want to preview that documents in

portal(portal is for citizen complaints). I want to create link in portal

if user click that link document should be open in alfresco.How can I pass

the document Id of document from portal to Alfresco.Is there I need to

write to webscript ??? or tell me how can we pass that value.

On Mon, Aug 14, 2017 at 1:39 PM, afaust <kristen.gastaldo@alfresco.com>

afaust
Master

Re: Document preview in portal side using alfresco

"I suggest using either a URL parameter, HTTP header or HTTP request body to pass data to Alfresco..."

I don't know what more you need to know in order to pass an ID (or any parameter for that matter) using standard HTTP / URL semantics...

kranthi
Active Member II

Re: Document preview in portal side using alfresco

I am writing simple html page for creating document link to open alfresco for with noderef (which is the default noderef) by clicking that it is going to alfresco where we need do authentication but I want to do it in automatic with admin/admin is it possible to that with external html.

afaust
Master

Re: Document preview in portal side using alfresco

No - for various reasons... First of all, the user / password won't always be admin/admin (admin password should always be changed to something else) and second of all, you cannot create a link that automatically logs a user in as a specific user - unless you have logged in as that user yourself before and include a token/ticket in that link. Such an approach is quite insecure as every user will have the same privileges (as you suggested those of an admin), and if logged in they can do everything inside Alfresco.

What you probably want to do is enabled proper SSO for end-users, so that each is automatically logged in with their personal account / credentials. See the Alfresco documentation about how to setup SSO with Kerberos or CAS (NTLM should be considered obsolete / insecure).

kranthi
Active Member II

Re: Document preview in portal side using alfresco

For example I said like that.

I did like this First I wrote the normal html by passing user credentials(for testing I am hard coding those credentials) and document noderef to javascript(which is in meta-inf installed alfresco directory ), in javascript I using Ajax call to webscript to validate and preview the document.But here from javascript it is calling webscript through ajax call.

Here what happening right now by clicking on that it just redirect to login page.What  I want do is document should open by checking user credentials(this is to be done automatically) in java-backend. Is this possible,if yes is this is the correct way of doing previewing document by doing java-backend authentication.if this is not correct guide me how to do.  

please see below files.

docPreview.html


<html>
<head>
<script type="text/javascript" src="C:/alfresco-DMS/tomcat/shared/classes/META-INF/js/docJs.js"></script>
</head>
<body>

<a href="http://localhost:8080/share/page/document-details?nodeRef=workspace://SpacesStore/9cfd0e0d-dcbd-44ba..." onclick="docPreviewAuthentication('kumar','Kum@r','workspace://SpacesStore/9cfd0e0d-dcbd-44ba-9e7f-4fec95fb285b');">
Document Link
</a>

</body>
</html>

docPreviewJs.js

function docPreviewAuthentication(userName,password,noref) {
alert("hello");
Alfresco.util.Ajax.jsonRequest(
{
                  url: Alfresco.constants.URL_SERVICECONTEXT + "/docPreviewAuthentication?userName="+userName&"password="+password,
                  method: "get",
                  successCallback:{fn: function(response) {
                           var result = JSON.parse(response.serverResponse.responseText).result;
                              alert("result"+result);
                               if(result=="success")
                           {
                                 location.href ="http://localhost:8080/share/page/document-details?nodeRef="+noref;
                            }
                         }, scope: this},
                    failureMessage: "Error in Loading"
}); 
}

DocPreview.java

package com.test.service;

import java.io.IOException;

import org.apache.log4j.Logger;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.extensions.webscripts.AbstractWebScript;
import org.springframework.extensions.webscripts.WebScriptRequest;
import org.springframework.extensions.webscripts.WebScriptResponse;

public class DoctPreview extends AbstractWebScript {
            private static Logger logger=Logger.getLogger(DoctPreview.class);            

            private AuthenticationService authenticationService;

 
            public AuthenticationService getAuthenticationService() {
                        return authenticationService;
            }

            public void setAuthenticationService(AuthenticationService authenticationService) {
                        this.authenticationService = authenticationService;
             }

  @Override
public void execute(WebScriptRequest req, WebScriptResponse res) throws IOException {
               // TODO Auto-generated method stub
               final WebScriptRequest req1=req;
               final WebScriptResponse res1=res;
               AuthenticationUtil.RunAsWork task = new AuthenticationUtil.RunAsWork() {
               @Override
               public Object doWork() throws Exception {
                        notifyJob(req1,res1);
                        return null;
               }
};
AuthenticationUtil.runAs(task, AuthenticationUtil.getAdminUserName());
}

public void notifyJob(WebScriptRequest req, WebScriptResponse res) throws IOException,AuthenticationException{
         try{
                  String usrName=req.getParameter("userName");
                  String pswd=req.getParameter("password");
                  JSONObject r = new JSONObject();
                    logger.debug("UserName:"+usrName);

               authenticationService.authenticate(usrName, pswd.toCharArray());
               boolean userAuthenticationExists=authenticationService.authenticationExists(usrName);
               if(userAuthenticationExists !=true)
               {
                  try {
                              r.put("result", "Fail");
                        } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();    
                        }
              }   
            else
            {
               try {
                        r.put("result", "Success");
                     } catch (JSONException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
                     }
             }   
         res.getWriter().write(r.toString());
         return;
  }
catch(Exception e)
{
         res.getWriter().write("EXception:"+e);   
         e.printStackTrace();
}
}

}

kranthi
Active Member II

Re: Document preview in portal side using alfresco

How I need to send http request is that in javascript or java.if possible please give me example how to do that http request and also how to access that request in webscript.

afaust
Master

Re: Document preview in portal side using alfresco

You should never pass a password as a URL parameter. That is just an extreme security risk.

You should never hard-code a password into a client-side JavaScript file. That is just an extreme security risk.

You should never run code as the "admin" user (AuthenticationUtil.runAs(task, AuthenticationUtil.getAdminUserName())). The user "admin" may be deleted by any administrator and then any code that relies on it will stop to work. Use the system user for any inherent functionality. You also do not need to use runAs for performing an authentication.

I thought you said you wanted to preview the document from a "portal side"? The "Alfresco.util.Ajax" utility you are using is part of Alfresco Share and should normally not be used anywhere else (so not in a "portal").

Also, what has authentication to do with accessing the preview (which was what you asked about)? The user should be properly authenticated either via Single Sign On or entering their user credentials. For that it would be enough to redirect the user to the document-details page - if SSO is enabled they are automatically signed in and can see the preview, if not they first have to authenticated but will then be redirected to the details page.

It looks to me like there is not really a concept of what you want to do - at least not one that you have communicated so far that fits with that example code.

afaust
Master

Re: Document preview in portal side using alfresco

You can learn how to send HTTP requests (in various languages) in quite a few tutorials that are freely available on the Internet.

In a JavaScript-backed web script you can access request information via root objects. In Java you use the WebScriptRequest object you get passed as a parameter.