Java tutorial
/* Copyright 2012 Simon Bear Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package au.csiro.notify.rest; import java.net.URI; import java.security.Principal; import javax.annotation.Resource; import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.Context; import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.Status; import javax.ws.rs.core.SecurityContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Controller; import au.csiro.notify.dao.beans.Notification; import au.csiro.notify.rest.dto.XMLNotificationType; import au.csiro.notify.rest.dto.XMLNotifications; import au.csiro.notify.service.NotificationService; import au.csiro.notify.service.exceptions.AlreadyExistsException; import au.csiro.notify.service.exceptions.InvalidDataException; import au.csiro.notify.service.exceptions.NotFoundException; import au.csiro.notify.util.NotificationAssembler; /** * RESTful Notification service implementation * * Copyright 2012, Simon Bear All rights reserved. * * @author Simon on 23/04/2012 * @version $Revision$ $Date$ */ @Controller("notificationRestService") public class NotificationRestService implements INotificationRestService { private static final Logger logger = LoggerFactory.getLogger(NotificationRestService.class); /** * The business layer service */ @Resource(name = "notificationService") private NotificationService notificationService; /** * The assembler that converts from notifications to their DTO */ @Resource(name = "notificationAssembler") private NotificationAssembler assembler; /** * The security context of the request */ @Context private SecurityContext sc; /* (non-Javadoc) * @see au.csiro.notify.web.rest.ICreateNotificationService#create(au.csiro.notify.web.rest.dto.XMLNotificationType) */ @Override public Response create(XMLNotificationType notification) { logger.trace("entering create({})", notification); try { long id = notificationService.create(assembler.createNotification(notification)); Response r = Response.created(URI.create("./" + id)).build(); logger.trace("exiting create={}", r); return r; } catch (AlreadyExistsException e) { throw new WebApplicationException(e, Response.status(Status.CONFLICT).build()); } catch (InvalidDataException e) { throw new WebApplicationException(e, Response.status(Status.BAD_REQUEST).build()); } } /* (non-Javadoc) * @see au.csiro.notify.web.rest.IRetrieveNotificationsService#getAll(boolean) */ @Override public XMLNotifications getAll(boolean includeRead) { logger.trace("entering getAll({})", includeRead); Principal p = sc.getUserPrincipal(); logger.debug("{}, {}", p.getName(), p); XMLNotifications response = assembler .createXMLNotifications(notificationService.retrieveAll(includeRead, sc.getUserPrincipal())); logger.trace("exiting getAll={}", response); return response; } /* (non-Javadoc) * @see au.csiro.notify.web.rest.IDeleteNotificationService#delete(long) */ @Override public Response delete(long id) { logger.trace("entering delete({})", id); try { notificationService.delete(id, sc.getUserPrincipal()); } catch (NotFoundException e) { logger.trace("exiting delete={}", e); throw new WebApplicationException(e, Status.NOT_FOUND); } logger.trace("exiting delete"); return Response.noContent().build(); } /* (non-Javadoc) * @see au.csiro.notify.web.rest.IRetrieveNotificationService#get(long) */ @Override public XMLNotificationType get(long id) { logger.trace("entering get({})", id); Notification n; try { n = notificationService.retrieve(id, sc.getUserPrincipal()); } catch (NotFoundException e) { logger.trace("exiting get={}", e); throw new WebApplicationException(e, Status.NOT_FOUND); } XMLNotificationType xn = assembler.createXMLNotificationType(n); logger.trace("exiting get={}", xn); return xn; } /* (non-Javadoc) * @see au.csiro.notify.web.rest.IUpdateReadNotificationService#update(long) */ @Override public Response updateRead(long id) { logger.trace("entering updateRead({})", id); try { notificationService.markRead(id, sc.getUserPrincipal()); } catch (NotFoundException e) { logger.trace("exiting updateRead={}", e); throw new WebApplicationException(e, Status.NOT_FOUND); } logger.trace("exiting updateRead"); return Response.noContent().build(); } }