<%-- Copyright 2005 - 2020 Alfresco Software Limited. This file is part of the Alfresco software. If the software was purchased under a paid Alfresco license, the terms of the paid license agreement will prevail. Otherwise, the software is provided under the following open source license terms: Alfresco is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. Alfresco is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with Alfresco. If not, see <http://www.gnu.org/licenses/>. --%> <%@ 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.util.regex.*" %> <%@ page import="org.apache.http.client.methods.HttpGet" %> <%@ page import="org.apache.http.impl.client.CloseableHttpClient" %> <%@ page import="org.apache.http.impl.client.HttpClients" %> <%@ page import="org.apache.http.util.EntityUtils" %> <% // retrieve userId and context String userid = (String)session.getAttribute(SlingshotUserFactory.SESSION_ATTRIBUTE_KEY_USER_ID); RequestContext context = (RequestContext)request.getAttribute(RequestContext.ATTR_REQUEST_CONTEXT); // no user dashboard page found! create initial dashboard for this user if (!context.getObjectService().hasPage("user/" + userid + "/dashboard")){ Map<String, String> tokens = new HashMap<String, String>(); tokens.put("userid", userid); FrameworkUtil.getServiceRegistry().getPresetsManager().constructPreset("user-dashboard", tokens); } // API call to retrieve sites String siteApiUrl = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath() + "/proxy/alfresco/api/people/" + URLEncoder.encode(userid) + "/sites"; String siteApiUrlResponse = ""; try (CloseableHttpClient httpClient = HttpClients.createDefault()) { HttpGet requestSites = new HttpGet(siteApiUrl); String jsessionid = session.getId(); requestSites.setHeader("Cookie", "JSESSIONID=" + jsessionid); siteApiUrlResponse = EntityUtils.toString(httpClient.execute(requestSites).getEntity()); System.out.println("Response JSON: " + siteApiUrlResponse); } catch (Exception e) { System.out.println("Error: " + e.getMessage()); } Pattern pattern = Pattern.compile("\\\"shortName\\\"[ ]*:[ ]*\\\"([a-zA-Z0-9_-]+)\\\""); Matcher matcher = pattern.matcher(siteApiUrlResponse); List<String> sites = new ArrayList<>(); while (matcher.find()) { sites.add(matcher.group(1)); } System.out.println("Site available for user: " + sites); // if size sites == 1 redirect su http://localhost:8180/share/page/site/edottosgd/documentlibrary if(sites.size() == 1){ // forward to demo site dashboard String siteName = sites.get(0); String target = request.getContextPath() + "/page/site/" + URLEncoder.encode(siteName) + "/documentlibrary"; response.sendRedirect(target); } else { // if size sites > 1 redirect su http://localhost:8180/share/page/user/admin/user-sites String target = request.getContextPath() + "/page/user/user-sites"; response.sendRedirect(target); } %> |