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