1- /*
2- * Knowage, Open Source Business Intelligence suite
3- * Copyright (C) 2016 Engineering Ingegneria Informatica S.p.A.
4- *
5- * Knowage is free software: you can redistribute it and/or modify
6- * it under the terms of the GNU Affero General Public License as published by
7- * the Free Software Foundation, either version 3 of the License, or
8- * (at your option) any later version.
9- *
10- * Knowage is distributed in the hope that it will be useful,
11- * but WITHOUT ANY WARRANTY; without even the implied warranty of
12- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13- * GNU Affero General Public License for more details.
14- *
15- * You should have received a copy of the GNU Affero General Public License
16- * along with this program. If not, see <http://www.gnu.org/licenses/>.
17- */
181package it .eng .spagobi .wapp .services ;
192
20- import java .io .IOException ;
213import java .util .ArrayList ;
224import java .util .Collection ;
5+ import java .util .Collections ;
6+
7+ import javax .servlet .http .HttpServletRequest ;
8+ import javax .ws .rs .Consumes ;
9+ import javax .ws .rs .POST ;
10+ import javax .ws .rs .Path ;
11+ import javax .ws .rs .Produces ;
12+ import javax .ws .rs .core .Context ;
13+ import javax .ws .rs .core .MediaType ;
14+ import javax .ws .rs .core .Response ;
2315
2416import org .apache .log4j .LogMF ;
2517import org .apache .log4j .Logger ;
2618
2719import it .eng .spago .security .IEngUserProfile ;
20+ import it .eng .spagobi .api .AbstractSpagoBIResource ;
2821import it .eng .spagobi .commons .bo .SessionUserProfile ;
2922import it .eng .spagobi .commons .bo .SessionUserProfileBuilder ;
3023import it .eng .spagobi .commons .bo .UserProfile ;
31- import it .eng .spagobi .commons .services .AbstractSpagoBIAction ;
3224import it .eng .spagobi .commons .utilities .StringUtilities ;
3325import it .eng .spagobi .commons .utilities .UserUtilities ;
3426import it .eng .spagobi .services .security .bo .SpagoBIUserProfile ;
35- import it .eng .spagobi .utilities .engines .SpagoBIEngineServiceException ;
3627import it .eng .spagobi .utilities .exceptions .SpagoBIServiceException ;
37- import it .eng .spagobi .utilities .service .JSONAcknowledge ;
38-
39- public class SetSessionRoleAction extends AbstractSpagoBIAction {
4028
41- private static Logger logger = Logger .getLogger (SetSessionRoleAction .class );
29+ @ Path ("/setsessionrole" )
30+ public class SetSessionRoleResource extends AbstractSpagoBIResource {
4231
43- UserProfile userProfile = null ;
44-
45- public static final String SERVICE_NAME = "SET_SESSION_ROLE_ACTION" ;
46- // REQUEST PARAMETERS
32+ private static Logger logger = Logger .getLogger (SetSessionRoleResource .class );
33+ public static final String SERVICE_NAME = "SET_SESSION_ROLE" ;
4734 public static final String SELECTED_ROLE = "SELECTED_ROLE" ;
4835
49- @ Override
50- public void doService () {
36+ @ POST
37+ @ Path ("/" )
38+ @ Consumes (MediaType .APPLICATION_FORM_URLENCODED )
39+ @ Produces (MediaType .APPLICATION_JSON )
40+ public Response setSessionRole (@ Context HttpServletRequest request ) {
41+
5142 logger .debug ("IN on service" );
5243 try {
5344
54- IEngUserProfile profile = this .getUserProfile ();
55-
56- String sessionRole = this .getAttributeAsString (SELECTED_ROLE );
45+ IEngUserProfile profile = getUserProfile ();
46+ String sessionRole = request .getParameter (SELECTED_ROLE );
5747 logger .debug ("Selected role " + sessionRole );
5848
5949 // check if selected role is part of the user ones
@@ -68,6 +58,7 @@ public void doService() {
6858 }
6959
7060 if (sessionRole != null && !roles .contains (sessionRole )) {
61+ UserProfile userProfile = (UserProfile ) profile ;
7162 logger .error ("Security alert. Role not among the user ones" );
7263 throw new SpagoBIServiceException (SERVICE_NAME , "Role selected is not permitted for user " + userProfile .getUserId ());
7364 }
@@ -78,7 +69,7 @@ public void doService() {
7869 logger .debug ("Creating an instance of SessionUserProfile..." );
7970 profile = SessionUserProfileBuilder .getDefaultUserProfile ((UserProfile ) profile );
8071 logger .debug ("Storing the SessionUserProfile in session in place of the previous profile object" );
81- storeProfileInSession (profile );
72+ storeProfileInSession (profile , request );
8273 }
8374 // at this moment, the profile is and instance of SessionUserProfile
8475 String previousSessionRole = ((SessionUserProfile ) profile ).getSessionRole ();
@@ -106,24 +97,25 @@ public void doService() {
10697 }
10798 logger .debug ("Filtered functionalities for selected role " + sessionRole );
10899
109- try {
110- writeBackToClient (new JSONAcknowledge ());
111- } catch (IOException e ) {
112- String message = "Impossible to write back the responce to the client" ;
113- throw new SpagoBIEngineServiceException (getActionName (), message , e );
114- }
100+ return Response .ok ().build ();
115101
102+ } catch (SpagoBIServiceException sbe ) {
103+ // Errori di validazione/servizio -> 400
104+ logger .error ("Service error in " + SERVICE_NAME , sbe );
105+ return Response .status (Response .Status .BAD_REQUEST ).entity (Collections .singletonMap ("error" , sbe .getMessage ())).build ();
116106 } catch (Exception e ) {
117- throw new SpagoBIServiceException (SERVICE_NAME , "Exception occurred while retrieving metadata" , e );
107+ // Errori generici -> 500
108+ logger .error ("Exception occurred while retrieving metadata" , e );
109+ return Response .status (Response .Status .INTERNAL_SERVER_ERROR )
110+ .entity (Collections .singletonMap ("error" , e .getMessage ())).build ();
118111 } finally {
119112 logger .debug ("OUT" );
120113 }
121-
122114 }
123115
124- private void storeProfileInSession (IEngUserProfile userProfile ) {
116+ private void storeProfileInSession (IEngUserProfile userProfile , HttpServletRequest request ) {
125117 logger .debug ("IN" );
126- getHttpRequest () .getSession ().setAttribute (IEngUserProfile .ENG_USER_PROFILE , userProfile );
118+ request .getSession ().setAttribute (IEngUserProfile .ENG_USER_PROFILE , userProfile );
127119 logger .debug ("OUT" );
128120 }
129121
@@ -148,4 +140,5 @@ private Collection getFunctionalitiesForSessionRole(IEngUserProfile engUserProfi
148140 }
149141 return toReturn ;
150142 }
143+
151144}
0 commit comments