-
Notifications
You must be signed in to change notification settings - Fork 3
/
AlbumResource.java
168 lines (134 loc) · 5.99 KB
/
AlbumResource.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package com.mgu.photoalbum.webapp.resource;
import com.codahale.metrics.annotation.Timed;
import com.google.common.base.Optional;
import com.google.inject.Inject;
import com.mgu.photoalbum.webapp.converter.AlbumReprConverter;
import com.mgu.photoalbum.domain.Album;
import com.mgu.photoalbum.domain.Photo;
import com.mgu.photoalbum.webapp.representation.AlbumRepr;
import com.mgu.photoalbum.webapp.representation.UploadPhotoRepr;
import com.mgu.photoalbum.security.Authorization;
import com.mgu.photoalbum.security.Principal;
import com.mgu.photoalbum.security.UserIsNotAuthorizedException;
import com.mgu.photoalbum.service.AlbumCommandService;
import com.mgu.photoalbum.service.AlbumQueryService;
import com.mgu.photoalbum.service.PhotoCommandService;
import com.mgu.photoalbum.service.PhotoQueryService;
import io.dropwizard.auth.Auth;
import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataParam;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import static java.lang.Math.max;
import static java.lang.Math.min;
@Path("/albums/{albumId}")
public class AlbumResource {
private static final int MAX_PAGE_SIZE = 100;
private static final int DEFAULT_OFFSET = 0;
private static final int DEFAULT_PAGE_SIZE = 10;
private static final List<String> DEFAULT_FILTER_BY_TAGS = new ArrayList<>();
private final AlbumQueryService albumQueryService;
private final AlbumCommandService albumCommandService;
private final PhotoCommandService photoCommandService;
private final PhotoQueryService photoQueryService;
private final Authorization authorization;
private final AlbumReprConverter albumConverter;
private final LinkScheme linkScheme;
@Inject
public AlbumResource(
final AlbumCommandService albumCommandService,
final AlbumQueryService albumQueryService,
final PhotoQueryService photoQueryService,
final PhotoCommandService photoCommandService,
final Authorization authorization,
final AlbumReprConverter albumConverter) {
this.albumCommandService = albumCommandService;
this.albumQueryService = albumQueryService;
this.photoQueryService = photoQueryService;
this.photoCommandService = photoCommandService;
this.authorization = authorization;
this.albumConverter = albumConverter;
this.linkScheme = new LinkScheme();
}
@GET
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Timed
public Response listAlbum(
@Auth Principal principal,
@PathParam("albumId") String albumId,
@QueryParam("offset") Optional<Integer> optionalOffset,
@QueryParam("pageSize") Optional<Integer> optionalPageSize,
@QueryParam("tags") Optional<String> optionalTags) {
final Album album = albumQueryService.albumById(albumId);
if (!authorization.isAuthorized(principal, album)) {
throw new UserIsNotAuthorizedException(principal);
}
final int offset = optionalOffset
.transform(wrappedOffset -> max(DEFAULT_OFFSET, wrappedOffset))
.or(DEFAULT_OFFSET);
final int pageSize = optionalPageSize
.transform(wrappedPageSize -> min(wrappedPageSize, MAX_PAGE_SIZE))
.or(DEFAULT_PAGE_SIZE);
final List<String> filterByTags = parseTags(optionalTags).or(DEFAULT_FILTER_BY_TAGS);
final List<Photo> photos = photoQueryService.search(albumId, filterByTags, offset, pageSize);
final AlbumRepr albumRepr = albumConverter.convert(album, photos, offset, pageSize, filterByTags);
return Response.ok(albumRepr).build();
}
private Optional<List<String>> parseTags(Optional<String> optionalTags) {
return optionalTags.transform(i -> Arrays.asList(i.split(",")).stream().filter(s -> !s.isEmpty()).collect(Collectors.toList()));
}
@POST
@Consumes("multipart/form-data")
@Produces(MediaType.APPLICATION_JSON)
@Timed
public Response upload(
@Auth Principal principal,
@PathParam("albumId") String albumId,
@FormDataParam("image") InputStream fileInputStream,
@FormDataParam("image") FormDataContentDisposition contentDisposition) {
if (fileInputStream == null || contentDisposition == null) {
return Response.status(422).build(); // throw exception!
}
final Album album = albumQueryService.albumById(albumId);
if (!authorization.isAuthorized(principal, album)) {
throw new UserIsNotAuthorizedException(principal);
}
final String photoId = photoCommandService.uploadPhoto(
principal.getUserId(),
albumId,
contentDisposition.getFileName(),
fileInputStream);
return Response.created(linkScheme.toPhoto(albumId, photoId)).build();
}
@DELETE
@Timed
public Response deleteAlbum(
@Auth Principal principal,
@PathParam("albumId") String albumId) {
final Album album = albumQueryService.albumById(albumId);
if (!authorization.isAuthorized(principal, album)) {
throw new UserIsNotAuthorizedException(principal);
}
albumCommandService.deleteAlbum(albumId);
return Response.noContent().build();
}
@OPTIONS
@Timed
public Response preflight() {
return Response
.ok()
.header("Access-Control-Allow-Origin", "*")
.header("Access-Control-Allow-Methods", "GET,POST,DELETE")
.header("Access-Control-Allow-Headers", "Origin, Content-Type, Accept, Authorization")
.encoding("UTF-8")
.allow("GET", "POST", "DELETE")
.build();
}
}