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.util.*; 020 021import org.apache.juneau.examples.parser.*; 022import org.apache.juneau.examples.serializer.*; 023import org.apache.juneau.html.annotation.*; 024import org.apache.juneau.http.annotation.*; 025import org.apache.juneau.http.response.*; 026import org.apache.juneau.rest.annotation.*; 027import org.apache.juneau.rest.servlet.*; 028import org.apache.juneau.rest.widget.*; 029 030import java.awt.image.*; 031import java.net.*; 032 033/** 034 * Sample resource that allows images to be uploaded and retrieved. 035 * 036 * <h5 class='section'>See Also:</h5><ul> 037 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/Marshalling">Marshalling</a> 038 * <li class='jc'>{@link ImageSerializer} 039 * <li class='jc'>{@link ImageParser} 040 * </ul> 041 */ 042@Rest( 043 path="/photos", 044 messages="nls/PhotosResource", 045 title="Photo REST service", 046 description="Use a tool like Poster to upload and retrieve jpeg and png images." 047) 048@HtmlDocConfig( 049 widgets={ 050 ContentTypeMenuItem.class 051 }, 052 navlinks={ 053 "api: servlet:/api", 054 "stats: servlet:/stats", 055 "$W{ContentTypeMenuItem}", 056 "source: $C{Source/gitHub}/org/apache/juneau/examples/rest/PhotosResource.java" 057 }, 058 aside={ 059 "<div class='text'>", 060 " <p>Examples of serialized beans in the org.apache.juneau.rest.utilitybeans package.</p>", 061 "</div>" 062 }, 063 asideFloat="RIGHT" 064) 065public class PhotosResource extends BasicRestServlet { 066 067 private static final long serialVersionUID = 1L; 068 069 // Our cache of photos 070 private Map<Integer,Photo> photos = new TreeMap<>(); 071 072 /** Bean class for storing photos */ 073 public static class Photo { 074 private int id; 075 BufferedImage image; 076 077 Photo(int id, BufferedImage image) { 078 this.id = id; 079 this.image = image; 080 } 081 082 /** 083 * The photo URL. 084 * 085 * @return The photo URL. 086 */ 087 public URI getURI() { 088 try { 089 return new URI("photos/"+id); 090 } catch (URISyntaxException e) { 091 throw new IllegalStateException(e); // Shouldn't happen. 092 } 093 } 094 095 /** 096 * The photo ID 097 * 098 * @return The photo ID. 099 */ 100 public int getID() { 101 return id; 102 } 103 } 104 105 /** 106 * [HTTP GET /photos] 107 * GET request handler for list of all photos. 108 * 109 * @return A list of photo beans. 110 */ 111 @RestGet("/") 112 public Collection<Photo> getAllPhotos() { 113 return photos.values(); 114 } 115 116 /** 117 * [HTTP GET /photos/{id}] 118 * GET request handler for single photo. 119 * 120 * @param id The photo ID. 121 * @return The photo image. 122 * @throws NotFound If photo not found. 123 */ 124 @RestGet(path="/{id}", serializers=ImageSerializer.class) 125 public BufferedImage getPhoto(@Path("id") int id) throws NotFound { 126 Photo p = photos.get(id); 127 if (p == null) 128 throw new NotFound("Photo not found"); 129 return p.image; 130 } 131 132 /** 133 * [HTTP PUT /photos/{id}] 134 * PUT request handler. 135 * 136 * @param id The photo ID. 137 * @param image The photo image. 138 * @return OK. 139 */ 140 @RestPut(path="/{id}", parsers=ImageParser.class) 141 public Ok addPhoto(@Path("id") int id, @Content BufferedImage image) { 142 photos.put(id, new Photo(id, image)); 143 return Ok.OK; 144 } 145 146 /** 147 * [HTTP POST /photos] 148 * POST request handler. 149 * 150 * @param image The photo image. 151 * @return The created photo bean. 152 */ 153 @RestPost(path="/", parsers=ImageParser.class) 154 public Photo setPhoto(@Content BufferedImage image) { 155 int id = photos.size(); 156 Photo p = new Photo(id, image); 157 photos.put(id, p); 158 return p; 159 } 160 161 /** 162 * [HTTP DELETE /photos/{id}] 163 * DELETE request handler 164 * 165 * @param id The photo ID. 166 * @return OK. 167 * @throws NotFound If photo not found. 168 */ 169 @RestDelete("/{id}") 170 public Ok deletePhoto(@Path("id") int id) throws NotFound { 171 Photo p = photos.remove(id); 172 if (p == null) 173 throw new NotFound("Photo not found"); 174 return Ok.OK; 175 } 176}