AnsweredAssumed Answered

Override site-index.jsp - Script Auth issues

Question asked by augustodelucena on Oct 3, 2015
Latest reply on Oct 22, 2015 by augustodelucena
Hello!

I want to override the site-index.jsp by changing it redirection option from the user dashboard to the site which the user is a member of, as I saw here:

https://forums.alfresco.com/comment/136003#comment-136003

I created my custom-site-index.jsp to test it following several guides/tutorials on the web but I can't get it to work due to an 401 error (Server returned HTTP response code: 401).
I read everywhere that I need to have an authentication(ticket/token) to be able to call the people API. However, I don't have any clue on how to get access to the People API.

How do I get access to the People API and is it possible to do what I want with the code below? 

Thanks in advance!

 
<%@ page import="org.alfresco.web.site.*" %>
<%@ page import="org.springframework.extensions.surf.*" %>
<%@ page import="org.springframework.extensions.surf.site.*" %>
<%@ page import="org.springframework.extensions.surf.util.*" %>
<%@ page import="java.util.*" %>
<%@ page import="java.io.IOException" %>
<%@ page import="java.net.URL" %>
<%@ page import="org.apache.commons.io.IOUtils" %>
<%@ page import="org.json.simple.JSONArray" %>
<%@ page import="org.json.simple.JSONObject" %>
<%@ page import="org.json.simple.JSONValue" %>
<%@ page import="org.json.simple.parser.ParseException" %>
<%@ page import="org.json.JSONException" %>


<%

//retrieve user name from the session (site-index OTB fragment)
String userid = (String)session.getAttribute(SlingshotUserFactory.SESSION_ATTRIBUTE_KEY_USER_ID);

//retrieve JSON array from the people/userid/sites
String url = "http://localhost:8080/alfresco/service/api/people/" + userid + "/sites";       
String peopleJson = IOUtils.toString(new URL(url));
JSONObject peopleJsonObject = (JSONObject) JSONValue.parseWithException(peopleJson);
           
// get the site user id(shortName)
JSONArray siteArray = (JSONArray) peopleJsonObject.get("shortName");
// get the first site
JSONObject usersitejson = (JSONObject) siteArray.get(0);
String usersite = usersitejson.toString();
       
// Admin redirection validation
if (usersite == null || usersite.length() == 0 || userid == "admin")

// forward to user specific dashboard page(admin)
{
response.sendRedirect(request.getContextPath() + "/page/user/" + URLEncoder.encode(userid) + "/dashboard");
}
else
// forward to user respective site documentlibrary
{
response.sendRedirect(request.getContextPath() + "/page/sites/" + URLEncoder.encode(usersite) + "/documentlibrary");
}
%>


Please forgive me for so many not-used dependencies being imported.

<blockcode>
SEVERE: Servlet.service() for servlet jsp threw exception
java.io.IOException: Server returned HTTP response code: 401 for URL: http://localhost:8080/alfresco/service/api/people/admin/sites
   at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1627)
   at java.net.URL.openStream(URL.java:1037)
   at org.apache.commons.io.IOUtils.toString(IOUtils.java:894)
   at org.apache.commons.io.IOUtils.toString(IOUtils.java:879)
   at org.apache.jsp.custom_002dsite_002dindex_jsp._jspService(custom_002dsite_002dindex_jsp.java:150)
   at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
   at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
   at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
   at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
   at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
   at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:749)
   at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:487)
   at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:412)
   at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:339)
   at org.springframework.extensions.surf.render.bean.JSPProcessor.doInclude(JSPProcessor.java:239)
   at org.springframework.extensions.surf.render.bean.JSPProcessor.executeBody(JSPProcessor.java:154)
   at org.springframework.extensions.surf.render.AbstractProcessor.execute(AbstractProcessor.java:57)
   at org.springframework.extensions.surf.render.RenderService.processTemplate(RenderService.java:720)
   at org.springframework.extensions.surf.render.bean.TemplateInstanceRenderer.calculateComponentDependencies(TemplateInstanceRenderer.java:75)
   at org.springframework.extensions.surf.render.bean.TemplateInstanceRenderer.body(TemplateInstanceRenderer.java:134)
   at org.springframework.extensions.surf.render.AbstractRenderer.render(AbstractRenderer.java:77)
   at org.springframework.extensions.surf.render.bean.PageRenderer.body(PageRenderer.java:85)
   at org.springframework.extensions.surf.render.AbstractRenderer.render(AbstractRenderer.java:77)
   at org.springframework.extensions.surf.render.RenderService.renderPage(RenderService.java:761)
   at org.springframework.extensions.surf.mvc.PageView.dispatchPage(PageView.java:411)
   at org.springframework.extensions.surf.mvc.PageView.renderView(PageView.java:306)
   at org.springframework.extensions.surf.mvc.AbstractWebFrameworkView.renderMergedOutputModel(AbstractWebFrameworkView.java:316)
   at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:264)
   at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1201)
   at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:986)
   at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:933)
   at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:851)
   at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:953)
   at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:844)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
   at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:829)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
   at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
   at org.tuckey.web.filters.urlrewrite.RuleChain.handleRewrite(RuleChain.java:176)
   at org.tuckey.web.filters.urlrewrite.RuleChain.doRules(RuleChain.java:145)
   at org.tuckey.web.filters.urlrewrite.UrlRewriter.processRequest(UrlRewriter.java:92)
   at org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:389)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
   at org.alfresco.web.site.servlet.SecurityHeadersFilter.doFilter(SecurityHeadersFilter.java:168)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
   at org.alfresco.web.site.servlet.CSRFFilter.doFilter(CSRFFilter.java:322)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
   at org.alfresco.web.site.servlet.SSOAuthenticationFilter.doFilter(SSOAuthenticationFilter.java:447)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
   at org.alfresco.web.site.servlet.MTAuthenticationFilter.doFilter(MTAuthenticationFilter.java:74)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
   at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
   at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
   at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
   at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
   at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
   at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
   at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
   at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
   at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
   at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
   at java.lang.Thread.run(Thread.java:745)

2015-10-03 16:06:57,589  ERROR [surf.render.RenderService] [http-bio-8081-exec-4] Unable to process template: custom-site-index
2015-10-03 16:06:57,592  ERROR [surf.render.RenderService] [http-bio-8081-exec-4] An exception occurred while rendering: custom-site-index
org.springframework.extensions.surf.exception.RendererExecutionException: Unable to execute 'body' JSP include: /custom-site-index.jsp
   at org.springframework.extensions.surf.render.bean.JSPProcessor.executeBody(JSPProcessor.java:158)
   at org.springframework.extensions.surf.render.AbstractProcessor.execute(AbstractProcessor.java:57)
   at org.springframework.extensions.surf.render.RenderService.processTemplate(RenderService.java:720)
   at org.springframework.extensions.surf.render.bean.TemplateInstanceRenderer.calculateComponentDependencies(TemplateInstanceRenderer.java:75)
   at org.springframework.extensions.surf.render.bean.TemplateInstanceRenderer.body(TemplateInstanceRenderer.java:134)
   at org.springframework.extensions.surf.render.AbstractRenderer.render(AbstractRenderer.java:77)
   at org.springframework.extensions.surf.render.bean.PageRenderer.body(PageRenderer.java:85)
   at org.springframework.extensions.surf.render.AbstractRenderer.render(AbstractRenderer.java:77)
   at org.springframework.extensions.surf.render.RenderService.renderPage(RenderService.java:761)
   at org.springframework.extensions.surf.mvc.PageView.dispatchPage(PageView.java:411)
   at org.springframework.extensions.surf.mvc.PageView.renderView(PageView.java:306)
   at org.springframework.extensions.surf.mvc.AbstractWebFrameworkView.renderMergedOutputModel(AbstractWebFrameworkView.java:316)
   at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:264)
   at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1201)
   at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:986)
   at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:933)
   at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:851)
   at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:953)
   at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:844)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
   at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:829)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
   at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
   at org.tuckey.web.filters.urlrewrite.RuleChain.handleRewrite(RuleChain.java:176)
   at org.tuckey.web.filters.urlrewrite.RuleChain.doRules(RuleChain.java:145)
   at org.tuckey.web.filters.urlrewrite.UrlRewriter.processRequest(UrlRewriter.java:92)
   at org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:389)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
   at org.alfresco.web.site.servlet.SecurityHeadersFilter.doFilter(SecurityHeadersFilter.java:168)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
   at org.alfresco.web.site.servlet.CSRFFilter.doFilter(CSRFFilter.java:322)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
   at org.alfresco.web.site.servlet.SSOAuthenticationFilter.doFilter(SSOAuthenticationFilter.java:447)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
   at org.alfresco.web.site.servlet.MTAuthenticationFilter.doFilter(MTAuthenticationFilter.java:74)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
   at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
   at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
   at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
   at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
   at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
   at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
   at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
   at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
   at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
   at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
   at java.lang.Thread.run(Thread.java:745)
Caused by: org.apache.jasper.JasperException: An exception occurred processing JSP page /custom-site-index.jsp at line 63

60: ]
61: */
62:        
63:             String peopleJson = IOUtils.toString(new URL(url));
64:             JSONObject peopleJsonObject = (JSONObject) JSONValue.parseWithException(peopleJson);
65:             // get the site user id(shortName)
66:             JSONArray siteArray = (JSONArray) peopleJsonObject.get("shortName");


Stacktrace:
   at org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568)
   at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:460)
   at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
   at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
   at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
   at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:749)
   at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:487)
   at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:412)
   at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:339)
   at org.springframework.extensions.surf.render.bean.JSPProcessor.doInclude(JSPProcessor.java:239)
   at org.springframework.extensions.surf.render.bean.JSPProcessor.executeBody(JSPProcessor.java:154)
   … 58 more
Caused by: java.io.IOException: Server returned HTTP response code: 401 for URL: http://localhost:8080/alfresco/service/api/people/admin/sites
   at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1627)
   at java.net.URL.openStream(URL.java:1037)
   at org.apache.commons.io.IOUtils.toString(IOUtils.java:894)
   at org.apache.commons.io.IOUtils.toString(IOUtils.java:879)
   at org.apache.jsp.custom_002dsite_002dindex_jsp._jspService(custom_002dsite_002dindex_jsp.java:150)
   at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
   at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
   … 72 more
</blockcode>


Edit:
I found that I can generate my ticket using this URL: http://localhost:8080/alfresco/service/api/login?u=admin&pw=admin

I used then the code above although it didn't work either.

String ticket = "http://localhost:8080/alfresco/service/api/login?u=admin&pw=admin";
String url = "http://localhost:8080/alfresco/service/api/people/" + userid + "/sites?alf_ticket="+ticket;

Is it supposed to work that way?

Outcomes