@Override protected Widget createContentWidget( final HttpServletRequest request, final HttpServletResponse response, final HttpContext context) throws IOException, PermissionDeniedException, RedirectException, LoginRequiredException { try { logger.trace("printContent"); final ListWidget widgets = new ListWidget(); widgets.add(new H1Widget(getTitle())); final String id = request.getParameter(BlogGuiConstants.PARAMETER_BLOG_POST_ID); final String title = request.getParameter(BlogGuiConstants.PARAMETER_BLOG_POST_TITLE); final String content = request.getParameter(BlogGuiConstants.PARAMETER_BLOG_POST_CONTENT); final SessionIdentifier sessionIdentifier = authenticationService.createSessionIdentifier(request); final BlogPostIdentifier blogPostIdentifier = blogService.createBlogPostIdentifier(id); final BlogPost blogPost = blogService.getBlogPost(sessionIdentifier, blogPostIdentifier); if (title != null && content != null) { try { blogService.updateBlogPost(sessionIdentifier, blogPostIdentifier, title, content); logger.debug("new BlogPost updated"); throw new RedirectException(request.getContextPath() + "/" + BlogGuiConstants.NAME); } catch (final ValidationException e) { widgets.add("update blogPost failed!"); widgets.add(new ValidationExceptionWidget(e)); } } final FormWidget formWidget = new FormWidget().addMethod(FormMethod.POST); formWidget.addFormInputWidget( new FormInputHiddenWidget(BlogGuiConstants.PARAMETER_BLOG_POST_ID)); formWidget.addFormInputWidget( new FormInputTextWidget(BlogGuiConstants.PARAMETER_BLOG_POST_TITLE) .addDefaultValue(blogPost.getTitle()) .addLabel("Title") .addPlaceholder("title...")); formWidget.addFormInputWidget( new FormInputTextareaWidget(BlogGuiConstants.PARAMETER_BLOG_POST_CONTENT) .addDefaultValue(blogPost.getContent()) .addLabel("Content") .addPlaceholder("content...")); formWidget.addFormInputWidget(new FormInputSubmitWidget("update")); widgets.add(formWidget); return widgets; } catch (final AuthenticationServiceException e) { logger.debug(e.getClass().getName(), e); final ExceptionWidget widget = new ExceptionWidget(e); return widget; } catch (final BlogServiceException e) { logger.debug(e.getClass().getName(), e); final ExceptionWidget widget = new ExceptionWidget(e); return widget; } catch (final BlogPostNotFoundException e) { logger.debug(e.getClass().getName(), e); final ExceptionWidget widget = new ExceptionWidget(e); return widget; } }
@Override protected Widget createContentWidget( final HttpServletRequest request, final HttpServletResponse response, final HttpContext context) throws IOException, LoginRequiredException { try { logger.trace("printContent"); final ListWidget widgets = new ListWidget(); widgets.add(new H1Widget(getTitle())); final String password_old = request.getParameter(AuthenticationGuiConstants.PARAMETER_PASSWORD_OLD); final String password_new = request.getParameter(AuthenticationGuiConstants.PARAMETER_PASSWORD_NEW); final String password_repeat = request.getParameter(AuthenticationGuiConstants.PARAMETER_PASSWORD_NEW_REPEAT); if (password_old != null && password_new != null && password_repeat != null) { final SessionIdentifier sessionIdentifier = authenticationService.createSessionIdentifier(request); try { authenticationService.changePassword( sessionIdentifier, password_old, password_new, password_repeat); widgets.add("change password successful"); } catch (final ValidationException e) { widgets.add("change password failed!"); widgets.add(new ValidationExceptionWidget(e)); } } final FormWidget form = new FormWidget().addMethod(FormMethod.POST); form.addFormInputWidget( new FormInputPasswordWidget(AuthenticationGuiConstants.PARAMETER_PASSWORD_OLD) .addLabel("Old password") .addPlaceholder("Old password...")); form.addFormInputWidget( new FormInputPasswordWidget(AuthenticationGuiConstants.PARAMETER_PASSWORD_NEW) .addLabel("New password") .addPlaceholder("New password...")); form.addFormInputWidget( new FormInputPasswordWidget(AuthenticationGuiConstants.PARAMETER_PASSWORD_NEW_REPEAT) .addLabel("New password repeat") .addPlaceholder("New password repeat...")); form.addFormInputWidget(new FormInputSubmitWidget("change password")); widgets.add(form); final ListWidget links = new ListWidget(); links.add(authenticationGuiLinkFactory.userProfile(request)); widgets.add(links); return widgets; } catch (final AuthenticationServiceException e) { logger.debug(e.getClass().getName(), e); final ExceptionWidget widget = new ExceptionWidget(e); return widget; } }
@Override protected void doCheckPermission(final HttpServletRequest request) throws ServletException, IOException, PermissionDeniedException, LoginRequiredException { try { final SessionIdentifier sessionIdentifier = authenticationService.createSessionIdentifier(request); monitoringService.expectMonitoringAdminPermission(sessionIdentifier); } catch (final AuthenticationServiceException e) { throw new PermissionDeniedException(e); } catch (MonitoringServiceException e) { throw new PermissionDeniedException(e); } }
@Override protected void doService( final HttpServletRequest request, final HttpServletResponse response, final HttpContext context) throws ServletException, IOException, LoginRequiredException, PermissionDeniedException { try { final SessionIdentifier sessionIdentifier = authenticationService.createSessionIdentifier(request); final MonitoringNodeIdentifier monitoringNodeIdentifier = monitoringService.createNodeIdentifier( request.getParameter(MonitoringGuiConstants.PARAMETER_NODE_ID)); monitoringService.unsilentNode(sessionIdentifier, monitoringNodeIdentifier); } catch (final AuthenticationServiceException e) { logger.warn(e.getClass().getName(), e); } catch (MonitoringServiceException e) { logger.warn(e.getClass().getName(), e); } final RedirectWidget widget = new RedirectWidget(buildRefererUrl(request)); widget.render(request, response, context); }
@Override protected Widget createGalleryContentWidget(final HttpServletRequest request) throws IOException, PermissionDeniedException, RedirectException, LoginRequiredException { try { final ListWidget widgets = new ListWidget(); widgets.add(new H1Widget(getTitle())); final String id = request.getParameter(GalleryGuiConstants.PARAMETER_COLLECTION_ID); final String name = request.getParameter(GalleryGuiConstants.PARAMETER_COLLECTION_NAME); final String referer = request.getParameter(GalleryGuiConstants.PARAMETER_REFERER); final String prio = request.getParameter(GalleryGuiConstants.PARAMETER_COLLECTION_PRIO); final String shared = request.getParameter(GalleryGuiConstants.PARAMETER_COLLECTION_SHARED); final String groupId = request.getParameter(GalleryGuiConstants.PARAMETER_GROUP_ID); final SessionIdentifier sessionIdentifier = authenticationService.createSessionIdentifier(request); final GalleryCollectionIdentifier galleryCollectionIdentifier = galleryService.createCollectionIdentifier(id); final GalleryCollection galleryCollection = galleryService.getCollection(sessionIdentifier, galleryCollectionIdentifier); if (name != null && prio != null && groupId != null && shared != null) { try { final GalleryGroupIdentifier galleryGroupIdentifier = galleryService.createGroupIdentifier(groupId); updateCollection( sessionIdentifier, galleryCollectionIdentifier, galleryGroupIdentifier, name, prio, shared); if (referer != null && referer.length() > 0) { throw new RedirectException(referer); } else { throw new RedirectException( galleryGuiLinkFactory.entryListUrl(request, galleryCollectionIdentifier)); } } catch (final ValidationException e) { widgets.add("create collection => failed"); widgets.add(new ValidationExceptionWidget(e)); } } final FormWidget formWidget = new FormWidget(); formWidget.addFormInputWidget( new FormInputHiddenWidget(GalleryGuiConstants.PARAMETER_REFERER) .addDefaultValue(buildRefererUrl(request))); formWidget.addFormInputWidget( new FormInputHiddenWidget(GalleryGuiConstants.PARAMETER_COLLECTION_ID) .addValue(galleryCollection.getId())); formWidget.addFormInputWidget( new FormInputHiddenWidget(GalleryGuiConstants.PARAMETER_GROUP_ID) .addValue(galleryCollection.getGroupId())); formWidget.addFormInputWidget( new FormInputTextWidget(GalleryGuiConstants.PARAMETER_COLLECTION_NAME) .addLabel("Name...") .addDefaultValue(galleryCollection.getName())); formWidget.addFormInputWidget( new FormInputTextWidget(GalleryGuiConstants.PARAMETER_COLLECTION_PRIO) .addLabel("Prio...") .addDefaultValue(galleryCollection.getPriority())); formWidget.addFormInputWidget( new FormInputTextWidget(GalleryGuiConstants.PARAMETER_COLLECTION_SHARED) .addLabel("Shared") .addPlaceholder("shared...") .addDefaultValue(galleryCollection.getShared())); formWidget.addFormInputWidget(new FormInputSubmitWidget("update")); widgets.add(formWidget); return widgets; } catch (final GalleryServiceException e) { logger.debug(e.getClass().getName(), e); return new ExceptionWidget(e); } catch (final AuthenticationServiceException e) { logger.debug(e.getClass().getName(), e); return new ExceptionWidget(e); } }
@Test public void testService() throws Exception { final Logger logger = EasyMock.createNiceMock(Logger.class); EasyMock.replay(logger); final HttpServletResponse response = EasyMock.createMock(HttpServletResponse.class); response.setContentType("text/html"); response.setCharacterEncoding("UTF-8"); final StringWriter sw = new StringWriter(); final PrintWriter printWriter = new PrintWriter(sw); EasyMock.expect(response.getWriter()).andReturn(printWriter).anyTimes(); EasyMock.replay(response); final String sessionId = "324908234890"; final HttpSession session = EasyMock.createMock(HttpSession.class); EasyMock.expect(session.getId()).andReturn(sessionId).anyTimes(); EasyMock.replay(session); final HttpServletRequest request = EasyMock.createMock(HttpServletRequest.class); EasyMock.expect(request.getServerPort()).andReturn(80).anyTimes(); EasyMock.expect(request.getContextPath()).andReturn("/path").anyTimes(); EasyMock.expect(request.getSession()).andReturn(session).anyTimes(); EasyMock.expect(request.getScheme()).andReturn("http").anyTimes(); EasyMock.expect(request.getServerName()).andReturn("localhost").anyTimes(); EasyMock.expect(request.getRequestURI()).andReturn("/path").anyTimes(); EasyMock.expect(request.getParameterNames()) .andReturn(new EnumerationEmpty<String>()) .anyTimes(); EasyMock.replay(request); final TimeZone timeZone = EasyMock.createMock(TimeZone.class); EasyMock.replay(timeZone); final TimeZoneUtil timeZoneUtil = EasyMock.createMock(TimeZoneUtil.class); EasyMock.expect(timeZoneUtil.getUTCTimeZone()).andReturn(timeZone).anyTimes(); EasyMock.replay(timeZoneUtil); final long startTime = 42l; final long endTime = 1337l; final Calendar calendar = EasyMock.createMock(Calendar.class); EasyMock.expect(calendar.getTimeInMillis()).andReturn(startTime); EasyMock.expect(calendar.getTimeInMillis()).andReturn(endTime); EasyMock.replay(calendar); final CalendarUtil calendarUtil = EasyMock.createMock(CalendarUtil.class); EasyMock.expect(calendarUtil.now(timeZone)).andReturn(calendar).anyTimes(); EasyMock.replay(calendarUtil); final ParseUtil parseUtil = EasyMock.createMock(ParseUtil.class); EasyMock.expect(parseUtil.parseLong(String.valueOf(startTime), endTime)).andReturn(startTime); EasyMock.replay(parseUtil); final Map<String, String> data = new HashMap<String, String>(); final HttpContext httpContext = EasyMock.createMock(HttpContext.class); EasyMock.expect(httpContext.getData()).andReturn(data).anyTimes(); EasyMock.replay(httpContext); final NavigationWidget navigationWidget = EasyMock.createMock(NavigationWidget.class); navigationWidget.render(request, response, httpContext); EasyMock.replay(navigationWidget); final Provider<HttpContext> httpContextProvider = new ProviderAdapter<HttpContext>(httpContext); final SessionIdentifier sessionIdentifier = EasyMock.createMock(SessionIdentifier.class); EasyMock.replay(sessionIdentifier); final UserIdentifier userIdentifier = EasyMock.createMock(UserIdentifier.class); EasyMock.replay(userIdentifier); final AuthenticationService authenticationService = EasyMock.createMock(AuthenticationService.class); EasyMock.expect(authenticationService.isLoggedIn(EasyMock.anyObject(SessionIdentifier.class))) .andReturn(true) .anyTimes(); EasyMock.expect(authenticationService.createSessionIdentifier(request)) .andReturn(sessionIdentifier) .anyTimes(); EasyMock.expect(authenticationService.getCurrentUser(sessionIdentifier)) .andReturn(userIdentifier) .anyTimes(); EasyMock.replay(authenticationService); final RedirectUtil redirectUtil = EasyMock.createMock(RedirectUtil.class); EasyMock.replay(redirectUtil); final UrlUtil urlUtil = EasyMock.createMock(UrlUtil.class); EasyMock.replay(urlUtil); final AuthorizationService authorizationService = EasyMock.createMock(AuthorizationService.class); authorizationService.expectAdminRole(sessionIdentifier); EasyMock.expect(authorizationService.hasAdminRole(sessionIdentifier)).andReturn(true); EasyMock.replay(authorizationService); final StorageService storageService = EasyMock.createNiceMock(StorageService.class); EasyMock.replay(storageService); final CacheService cacheService = EasyMock.createMock(CacheService.class); EasyMock.expect(cacheService.get("hostname")).andReturn("localhost").anyTimes(); EasyMock.replay(cacheService); final StorageGuiLinkFactory storageGuiLinkFactory = EasyMock.createNiceMock(StorageGuiLinkFactory.class); EasyMock.replay(storageGuiLinkFactory); final StorageGuiServlet storageServlet = new StorageGuiServlet( logger, calendarUtil, timeZoneUtil, parseUtil, authenticationService, navigationWidget, httpContextProvider, urlUtil, authorizationService, storageService, cacheService, storageGuiLinkFactory); storageServlet.service(request, response); final String content = sw.getBuffer().toString(); assertNotNull(content); assertTrue(content.contains("<h1>Storage</h1>")); }