@@ -156,7 +156,106 @@ public Response setUserPath( @CookieParam(Fields.COOKIE_PATH) String oldUserPath
156156 .entity (result ).location (newLocation ).build ();
157157
158158 }
159+
160+ @ GET
161+ @ Path ("/history/{history}" )
162+ @ Produces ( MediaType .TEXT_PLAIN )
163+ public Response setWorkspaceHistory ( @ CookieParam (Fields .COOKIE_PATH ) String oldUserPath , @ PathParam ("history" ) String historyListString , @ CookieParam (Fields .COOKIE_WORKSPACE_HISTORY ) String historyCookie , @ Context HttpServletRequest requestContext ) {
164+
165+ if ( Fields .ALLOW_SHARING_HISTORY == false ) {
166+ // this endpoint is not activated
167+ LOGGER .warn ("Attempted access to the share history endpoint declined, because ALLOW_SHARING_HISTORY is not set to true" );
168+ return buildTextErrorResponse (400 , null , "This endpoint is not enabled. Please set ALLOW_SHARING_HISTORY to true." );
169+ }
170+
171+ // clean up String
172+ historyListString = historyListString .trim ();
173+ String [] historyList = historyListString .split ("," );
174+
175+ if ( historyList .length == 0 ) {
176+ // not a single entry was passed
177+ return buildTextErrorResponse (500 , null , "No history entry was passed." );
178+ }
179+
180+ // user stuff
181+ // sets first workspace as active one
182+ UserManager user = null ;
183+ try {
184+ user = new UserManager ( historyList [0 ] );
185+ } catch (IOException e ) {
186+ LOGGER .error (e , "Cannot create user" );
187+ return buildErrorResponse (500 , null , "user not creatable!" , e .getMessage () );
188+ }
159189
190+ WorkspaceHistory history = null ;
191+ try {
192+ if ( historyCookie != null && !historyCookie .isEmpty () ) {
193+ history = WorkspaceHistory .fromCookieJson (historyCookie );
194+ }
195+
196+ if ( history == null )
197+ history = new WorkspaceHistory ();
198+
199+
200+ for ( String historyEntry : historyList ) {
201+ // put workspace into history cookie
202+ if ( historyEntry == null || historyEntry .isEmpty () )
203+ continue ;
204+
205+ // first checks, if the workspace actually exists, so we do not pollute the history with empty workspaces
206+ if ( WorkspaceManager .getInstance ().hasWorkspace (historyEntry ) ) {
207+ Workspace workspace = WorkspaceManager .getInstance ().getWorkspace (historyEntry );
208+ if ( history .containsWorkspace (workspace .getWorkspaceId ()) == false ) {
209+ history .getRecentWorkspaces ().add ( workspace );
210+ LOGGER .debug ("Added shared workspace " , workspace .getWorkspaceId (), " to history. Wasn't added yet." );
211+ }
212+ }
213+ }
214+
215+ // set first history entry as current
216+ history .setCurrentWorkspace ( user .getWorkspaceId () );
217+ LOGGER .info ("Set current workspace id to " , user .getWorkspaceId (), " from " , oldUserPath );
218+
219+ if ( oldUserPath != null && !oldUserPath .isEmpty () && history .containsWorkspace ( oldUserPath ) == false ) {
220+ Workspace workspace = WorkspaceManager .getInstance ().getWorkspace (oldUserPath );
221+ if ( workspace != null ) {
222+ history .getRecentWorkspaces ().add ( workspace );
223+ LOGGER .debug ("Added old workspace to history " , oldUserPath );
224+ }
225+ }
226+
227+ historyCookie = history .toCookieJson ();
228+
229+ } catch (IOException e ) {
230+ LOGGER .error (e , "Error parsing workspace history cookie " , historyCookie );
231+ return buildErrorResponse (500 , user , "Error parsing workspace history cookie " , historyCookie , e .getMessage ());
232+ }
233+
234+ String result = "setted " + user .getWorkspaceId ();
235+ URI newLocation = null ;
236+ try {
237+ if ( requestContext != null ) {
238+ String uri = requestContext .getRequestURL ().toString ();
239+ // String uri = requestContext.getRequestURI();
240+ uri = uri .substring (0 , uri .indexOf ("rest/" ));
241+ LOGGER .info ("redirect sharing link " , requestContext .getRequestURL (), " to " , uri );
242+ newLocation = new URI ( uri );
243+ // newLocation = new URI(requestContext.getScheme(), null, requestContext.getServerName(),
244+ // requestContext.getServerPort(), uri, requestContext.getQueryString(), null);
245+ }
246+ else
247+ newLocation = new URI ("../" );
248+
249+ } catch (URISyntaxException e ) {
250+ LOGGER .error (e , "Cannot generate relative URL to main app" );
251+ return null ;
252+ }
253+
254+ return buildResponse (302 , user )
255+ .cookie ( new NewCookie (Fields .COOKIE_WORKSPACE_HISTORY , historyCookie , "/" , null , null , Fields .COOKIE_AGE , false ) )
256+ .entity (result ).location (newLocation ).build ();
257+ }
258+
160259 @ GET
161260 @ Path ("/import" )
162261 @ Produces ( MediaType .TEXT_PLAIN )
@@ -552,11 +651,15 @@ else if( protocol.equals("post") && uploadedFiles != null && uploadedFiles.size(
552651 }
553652 }
554653 }
555- else
654+ else {
655+ output .close ();
556656 throw new ImporterException ("Unknown protocol " + protocol + " while adding " + remoteUri .toString ());
657+ }
557658
558- if ( input == null )
659+ if ( input == null ) {
660+ output .close ();
559661 throw new ImporterException ("Cannot open stream to import file: " + remoteUri .toString ());
662+ }
560663
561664 long downloadedFileSize = IOUtils .copy ( input , output );
562665
0 commit comments