001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.examples.rest;
014
015import java.util.*;
016
017import org.apache.juneau.examples.parser.*;
018import org.apache.juneau.examples.serializer.*;
019import org.apache.juneau.html.annotation.*;
020import org.apache.juneau.http.annotation.*;
021import org.apache.juneau.http.response.*;
022import org.apache.juneau.rest.annotation.*;
023import org.apache.juneau.rest.servlet.*;
024import org.apache.juneau.rest.widget.*;
025
026import java.awt.image.*;
027import java.net.*;
028
029/**
030 * Sample resource that allows images to be uploaded and retrieved.
031 *
032 * <h5 class='section'>See Also:</h5><ul>
033 *    <li class='link'><a class="doclink" href="../../../../../index.html#jrs.Marshalling">REST Marshalling</a>
034 *    <li class='jc'>{@link ImageSerializer}
035 *    <li class='jc'>{@link ImageParser}
036 * </ul>
037 */
038@Rest(
039   path="/photos",
040   messages="nls/PhotosResource",
041   title="Photo REST service",
042   description="Use a tool like Poster to upload and retrieve jpeg and png images."
043)
044@HtmlDocConfig(
045   widgets={
046      ContentTypeMenuItem.class
047   },
048   navlinks={
049      "api: servlet:/api",
050      "stats: servlet:/stats",
051      "$W{ContentTypeMenuItem}",
052      "source: $C{Source/gitHub}/org/apache/juneau/examples/rest/PhotosResource.java"
053   },
054   aside={
055      "<div class='text'>",
056      "  <p>Examples of serialized beans in the org.apache.juneau.rest.utilitybeans package.</p>",
057      "</div>"
058   },
059   asideFloat="RIGHT"
060)
061public class PhotosResource extends BasicRestServlet {
062
063   private static final long serialVersionUID = 1L;
064
065   // Our cache of photos
066   private Map<Integer,Photo> photos = new TreeMap<>();
067
068   /** Bean class for storing photos */
069   public static class Photo {
070      private int id;
071      BufferedImage image;
072
073      Photo(int id, BufferedImage image) {
074         this.id = id;
075         this.image = image;
076      }
077
078      /**
079       * The photo URL.
080       *
081       * @return The photo URL.
082       */
083      public URI getURI() {
084         try {
085            return new URI("photos/"+id);
086         } catch (URISyntaxException e) {
087            throw new IllegalStateException(e); // Shouldn't happen.
088         }
089      }
090
091      /**
092       * The photo ID
093       *
094       * @return The photo ID.
095       */
096      public int getID() {
097         return id;
098      }
099   }
100
101   /**
102    * [HTTP GET /photos]
103    * GET request handler for list of all photos.
104    *
105    * @return A list of photo beans.
106    */
107   @RestGet("/")
108   public Collection<Photo> getAllPhotos() {
109      return photos.values();
110   }
111
112   /**
113    * [HTTP GET /photos/{id}]
114    * GET request handler for single photo.
115    *
116    * @param id The photo ID.
117    * @return The photo image.
118    * @throws NotFound If photo not found.
119    */
120   @RestGet(path="/{id}", serializers=ImageSerializer.class)
121   public BufferedImage getPhoto(@Path("id") int id) throws NotFound {
122      Photo p = photos.get(id);
123      if (p == null)
124         throw new NotFound("Photo not found");
125      return p.image;
126   }
127
128   /**
129    * [HTTP PUT /photos/{id}]
130    * PUT request handler.
131    *
132    * @param id The photo ID.
133    * @param image The photo image.
134    * @return OK.
135    */
136   @RestPut(path="/{id}", parsers=ImageParser.class)
137   public Ok addPhoto(@Path("id") int id, @Content BufferedImage image) {
138      photos.put(id, new Photo(id, image));
139      return Ok.OK;
140   }
141
142   /**
143    * [HTTP POST /photos]
144    * POST request handler.
145    *
146    * @param image The photo image.
147    * @return The created photo bean.
148    */
149   @RestPost(path="/", parsers=ImageParser.class)
150   public Photo setPhoto(@Content BufferedImage image) {
151      int id = photos.size();
152      Photo p = new Photo(id, image);
153      photos.put(id, p);
154      return p;
155   }
156
157   /**
158    * [HTTP DELETE /photos/{id}]
159    * DELETE request handler
160    *
161    * @param id The photo ID.
162    * @return OK.
163    * @throws NotFound If photo not found.
164    */
165   @RestDelete("/{id}")
166   public Ok deletePhoto(@Path("id") int id) throws NotFound {
167      Photo p = photos.remove(id);
168      if (p == null)
169         throw new NotFound("Photo not found");
170      return Ok.OK;
171   }
172}