@Test public void shouldReturnRedirectUrlWhenDocumentExists() throws Exception { HttpServletRequest request = Mockito.mock(HttpServletRequest.class); SessionProviderService sessionProviderService = Mockito.mock(SessionProviderService.class); RepositoryService repositoryService = Mockito.mock(RepositoryService.class); DocumentService documentService = Mockito.mock(DocumentService.class); SessionProvider sessionProvider = Mockito.mock(SessionProvider.class); ExtendedSession session = Mockito.mock(ExtendedSession.class); Node node = Mockito.mock(Node.class); Mockito.when(sessionProviderService.getSystemSessionProvider(Mockito.any())).thenReturn(sessionProvider); Mockito.when(sessionProvider.getSession(Mockito.anyString(), Mockito.any())).thenReturn(session); Mockito.when(session.getNodeByIdentifier(Mockito.anyString())).thenReturn(node); DocumentsAppRedirectService documentsAppRedirectService = new DocumentsAppRedirectService(sessionProviderService, repositoryService, documentService); Response response = documentsAppRedirectService.redirect(request, "collaboration", "123"); assertNotNull(response); assertEquals(307, response.getStatus()); }
@GET @RolesAllowed("users") @Path("/{workspaceName}/{docId}") public Response redirect(@Context HttpServletRequest request, @PathParam("workspaceName") String workspaceName, @PathParam("docId") String docId) throws IOException { if(StringUtils.isEmpty(workspaceName) || StringUtils.isEmpty(docId)) { return Response.serverError().entity("Parameters workspaceName and docId are mandatory").build(); } LOG.debug("Requesting Documents app redirection for doc with id {0}", docId); try { SessionProvider systemSessionProvider = sessionProviderService.getSystemSessionProvider(null); ExtendedSession session = (ExtendedSession) systemSessionProvider.getSession(workspaceName, repositoryService.getCurrentRepository()); Node node = session.getNodeByIdentifier(docId); if(node != null) { String linkInDocumentsApp = documentService.getLinkInDocumentsApp(node.getPath()); URI redirectUrl = new URI(getURLPrefix(request) + linkInDocumentsApp); return Response.temporaryRedirect(redirectUrl).build(); } } catch(ItemNotFoundException e) { LOG.error("Cannot get node " + docId + " : " + e.getMessage(), e); return Response.status(404).build(); } catch(Exception e) { LOG.error("Cannot get node " + docId + " : " + e.getMessage(), e); return Response.serverError().build(); } return Response.serverError().build(); }
DocumentsAppRedirectService implements ResourceContainer { @GET @RolesAllowed("users") @Path("/{workspaceName}/{docId}") public Response redirect(@Context HttpServletRequest request, @PathParam("workspaceName") String workspaceName, @PathParam("docId") String docId) throws IOException { if(StringUtils.isEmpty(workspaceName) || StringUtils.isEmpty(docId)) { return Response.serverError().entity("Parameters workspaceName and docId are mandatory").build(); } LOG.debug("Requesting Documents app redirection for doc with id {0}", docId); try { SessionProvider systemSessionProvider = sessionProviderService.getSystemSessionProvider(null); ExtendedSession session = (ExtendedSession) systemSessionProvider.getSession(workspaceName, repositoryService.getCurrentRepository()); Node node = session.getNodeByIdentifier(docId); if(node != null) { String linkInDocumentsApp = documentService.getLinkInDocumentsApp(node.getPath()); URI redirectUrl = new URI(getURLPrefix(request) + linkInDocumentsApp); return Response.temporaryRedirect(redirectUrl).build(); } } catch(ItemNotFoundException e) { LOG.error("Cannot get node " + docId + " : " + e.getMessage(), e); return Response.status(404).build(); } catch(Exception e) { LOG.error("Cannot get node " + docId + " : " + e.getMessage(), e); return Response.serverError().build(); } return Response.serverError().build(); } }
DocumentsAppRedirectService implements ResourceContainer { @GET @RolesAllowed("users") @Path("/{workspaceName}/{docId}") public Response redirect(@Context HttpServletRequest request, @PathParam("workspaceName") String workspaceName, @PathParam("docId") String docId) throws IOException { if(StringUtils.isEmpty(workspaceName) || StringUtils.isEmpty(docId)) { return Response.serverError().entity("Parameters workspaceName and docId are mandatory").build(); } LOG.debug("Requesting Documents app redirection for doc with id {0}", docId); try { SessionProvider systemSessionProvider = sessionProviderService.getSystemSessionProvider(null); ExtendedSession session = (ExtendedSession) systemSessionProvider.getSession(workspaceName, repositoryService.getCurrentRepository()); Node node = session.getNodeByIdentifier(docId); if(node != null) { String linkInDocumentsApp = documentService.getLinkInDocumentsApp(node.getPath()); URI redirectUrl = new URI(getURLPrefix(request) + linkInDocumentsApp); return Response.temporaryRedirect(redirectUrl).build(); } } catch(ItemNotFoundException e) { LOG.error("Cannot get node " + docId + " : " + e.getMessage(), e); return Response.status(404).build(); } catch(Exception e) { LOG.error("Cannot get node " + docId + " : " + e.getMessage(), e); return Response.serverError().build(); } return Response.serverError().build(); } DocumentsAppRedirectService(SessionProviderService sessionProviderService, RepositoryService repositoryService, DocumentService documentService); }
DocumentsAppRedirectService implements ResourceContainer { @GET @RolesAllowed("users") @Path("/{workspaceName}/{docId}") public Response redirect(@Context HttpServletRequest request, @PathParam("workspaceName") String workspaceName, @PathParam("docId") String docId) throws IOException { if(StringUtils.isEmpty(workspaceName) || StringUtils.isEmpty(docId)) { return Response.serverError().entity("Parameters workspaceName and docId are mandatory").build(); } LOG.debug("Requesting Documents app redirection for doc with id {0}", docId); try { SessionProvider systemSessionProvider = sessionProviderService.getSystemSessionProvider(null); ExtendedSession session = (ExtendedSession) systemSessionProvider.getSession(workspaceName, repositoryService.getCurrentRepository()); Node node = session.getNodeByIdentifier(docId); if(node != null) { String linkInDocumentsApp = documentService.getLinkInDocumentsApp(node.getPath()); URI redirectUrl = new URI(getURLPrefix(request) + linkInDocumentsApp); return Response.temporaryRedirect(redirectUrl).build(); } } catch(ItemNotFoundException e) { LOG.error("Cannot get node " + docId + " : " + e.getMessage(), e); return Response.status(404).build(); } catch(Exception e) { LOG.error("Cannot get node " + docId + " : " + e.getMessage(), e); return Response.serverError().build(); } return Response.serverError().build(); } DocumentsAppRedirectService(SessionProviderService sessionProviderService, RepositoryService repositoryService, DocumentService documentService); @GET @RolesAllowed("users") @Path("/{workspaceName}/{docId}") Response redirect(@Context HttpServletRequest request,
                           @PathParam("workspaceName") String workspaceName,
                           @PathParam("docId") String docId); }
DocumentsAppRedirectService implements ResourceContainer { @GET @RolesAllowed("users") @Path("/{workspaceName}/{docId}") public Response redirect(@Context HttpServletRequest request, @PathParam("workspaceName") String workspaceName, @PathParam("docId") String docId) throws IOException { if(StringUtils.isEmpty(workspaceName) || StringUtils.isEmpty(docId)) { return Response.serverError().entity("Parameters workspaceName and docId are mandatory").build(); } LOG.debug("Requesting Documents app redirection for doc with id {0}", docId); try { SessionProvider systemSessionProvider = sessionProviderService.getSystemSessionProvider(null); ExtendedSession session = (ExtendedSession) systemSessionProvider.getSession(workspaceName, repositoryService.getCurrentRepository()); Node node = session.getNodeByIdentifier(docId); if(node != null) { String linkInDocumentsApp = documentService.getLinkInDocumentsApp(node.getPath()); URI redirectUrl = new URI(getURLPrefix(request) + linkInDocumentsApp); return Response.temporaryRedirect(redirectUrl).build(); } } catch(ItemNotFoundException e) { LOG.error("Cannot get node " + docId + " : " + e.getMessage(), e); return Response.status(404).build(); } catch(Exception e) { LOG.error("Cannot get node " + docId + " : " + e.getMessage(), e); return Response.serverError().build(); } return Response.serverError().build(); } DocumentsAppRedirectService(SessionProviderService sessionProviderService, RepositoryService repositoryService, DocumentService documentService); @GET @RolesAllowed("users") @Path("/{workspaceName}/{docId}") Response redirect(@Context HttpServletRequest request,
                           @PathParam("workspaceName") String workspaceName,
                           @PathParam("docId") String docId);  }