Skip to content

Commit 1451a47

Browse files
committed
thumb forbiddent exception
1 parent fe78f91 commit 1451a47

File tree

5 files changed

+122
-56
lines changed

5 files changed

+122
-56
lines changed

src/main/java/org/cbir/retrieval/config/SecurityConfiguration.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ protected void configure(HttpSecurity http) throws Exception {
121121
.frameOptions()
122122
.disable()
123123
.authorizeRequests()
124+
.antMatchers("/thumb/**").permitAll()
124125
.antMatchers("/app/rest/register").permitAll()
125126
.antMatchers("/app/rest/activate").permitAll()
126127
.antMatchers("/app/rest/authenticate").permitAll()

src/main/java/org/cbir/retrieval/service/StoreImageService.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,15 @@ private BufferedImage readImage(Long id,String path) throws IOException {
7777
return ImageIO.read(file);
7878
}
7979

80+
public BufferedImage tryReadIndexImage(Long id) throws IOException {
81+
String path = env.getProperty("retrieval.thumb.index");
82+
File dir = new File(path);
83+
File file = new File(dir,id+".png");
84+
log.info("Read IMAGE in "+file.getAbsolutePath());
85+
if(file.exists()) {
86+
return ImageIO.read(file);
87+
}
88+
return null;
89+
}
90+
8091
}

src/main/java/org/cbir/retrieval/web/rest/ImageResource.java

Lines changed: 7 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -244,17 +244,20 @@ ResponseEntity<ResultsJSON> search(
244244
@RequestParam(required = false) Long id,
245245
@RequestParam(value = "saveImage",defaultValue = "false") Boolean saveimage
246246
) throws CBIRException, IOException {
247-
log.debug("REST request to get CBIR results : max=" + max + " storages=" + storages);
247+
log.debug("REST request to get CBIR results : max=" + max + " url=" + url + " id=" +id+" storages=" + storages);
248248
BufferedImage image = null;
249249

250-
251250
try {
252251

253252
if(id!=null) {
254-
image = storeImageService.readIndexImage(id);
255-
} else {
253+
image = storeImageService.tryReadIndexImage(id);
254+
}
255+
256+
//if no id or id has no valid images
257+
if(image==null) {
256258
image = ImageIO.read(new URL(url));
257259
}
260+
258261
} catch (IOException ex) {
259262
throw new ResourceNotValidException("Image not valid:" + ex.toString());
260263
}
@@ -263,44 +266,6 @@ ResponseEntity<ResultsJSON> search(
263266
}
264267

265268

266-
267-
@RequestMapping(value = "/images/{id}/thumb",
268-
method = RequestMethod.GET,
269-
produces = MediaType.IMAGE_PNG_VALUE)
270-
@Timed
271-
@RolesAllowed(AuthoritiesConstants.USER)
272-
ResponseEntity<byte[]> getStoreImage(@PathVariable Long id,@RequestParam(required = false) Integer size,@RequestParam(required = false,defaultValue = "index") String type) throws ResourceNotFoundException, NoValidPictureException {
273-
log.debug("REST request to get image thumb : " + id);
274-
275-
try {
276-
BufferedImage image = null;
277-
if(type.equals("index")) {
278-
image = storeImageService.readIndexImage(id);
279-
} else {
280-
image = storeImageService.readSearchImage(id);
281-
}
282-
283-
if(size!=null) {
284-
SizeUtils sizeCompute = new SizeUtils(image.getWidth(),image.getHeight(),size,size);
285-
sizeCompute = sizeCompute.computeThumbSize();
286-
//resize image
287-
image = resizePicture(image,sizeCompute.getWidth(),sizeCompute.getHeight());
288-
}
289-
290-
291-
final HttpHeaders headers = new HttpHeaders();
292-
headers.setContentType(MediaType.IMAGE_PNG);
293-
ByteArrayOutputStream baos = new ByteArrayOutputStream();
294-
ImageIO.write( image, "png", baos );
295-
baos.flush();
296-
byte[] imageInByte = baos.toByteArray();
297-
baos.close();
298-
return new ResponseEntity<>(imageInByte, headers, HttpStatus.OK);
299-
} catch(IOException e) {
300-
throw new NoValidPictureException("Image with "+id + "cannot be read");
301-
}
302-
}
303-
304269
@RequestMapping(value = "/index/file",
305270
method = RequestMethod.POST,
306271
produces = MediaType.APPLICATION_JSON_VALUE)
@@ -450,16 +415,4 @@ private Map<String, String> indexPicture(Long idImage, String idStorage, String
450415
return storage.getProperties(id);
451416
}
452417

453-
private static BufferedImage resizePicture(BufferedImage image, int targetWidth, int targetHeight) {
454-
int type = image.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : image.getType();
455-
BufferedImage resizedImage = new BufferedImage(targetWidth, targetHeight, type);
456-
Graphics2D g = resizedImage.createGraphics();
457-
g.setComposite(AlphaComposite.Src);
458-
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
459-
g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_SPEED);
460-
g.drawImage(image, 0, 0, targetWidth, targetHeight, null);
461-
g.dispose();
462-
return resizedImage;
463-
464-
}
465418
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package org.cbir.retrieval.web.rest;
2+
3+
import com.codahale.metrics.annotation.Timed;
4+
import org.cbir.retrieval.security.AuthoritiesConstants;
5+
import org.cbir.retrieval.service.RetrievalService;
6+
import org.cbir.retrieval.service.StoreImageService;
7+
import org.cbir.retrieval.service.exception.*;
8+
import org.cbir.retrieval.web.rest.dto.ResultsJSON;
9+
import org.slf4j.Logger;
10+
import org.slf4j.LoggerFactory;
11+
import org.springframework.boot.json.JacksonJsonParser;
12+
import org.springframework.http.HttpHeaders;
13+
import org.springframework.http.HttpStatus;
14+
import org.springframework.http.MediaType;
15+
import org.springframework.http.ResponseEntity;
16+
import org.springframework.web.bind.annotation.*;
17+
import org.springframework.web.multipart.MultipartFile;
18+
import retrieval.client.RetrievalClient;
19+
import retrieval.dist.ResultsSimilarities;
20+
import retrieval.server.RetrievalServer;
21+
import retrieval.storage.Storage;
22+
import retrieval.storage.exception.AlreadyIndexedException;
23+
import retrieval.storage.exception.NoValidPictureException;
24+
import retrieval.utils.SizeUtils;
25+
26+
import javax.annotation.security.RolesAllowed;
27+
import javax.imageio.ImageIO;
28+
import javax.inject.Inject;
29+
import java.awt.*;
30+
import java.awt.image.BufferedImage;
31+
import java.io.*;
32+
import java.net.URL;
33+
import java.util.*;
34+
import java.util.List;
35+
import java.util.stream.Collectors;
36+
37+
/**
38+
* REST controller for managing images.
39+
*/
40+
@RestController
41+
@RequestMapping("/thumb")
42+
public class ThumbResource {
43+
44+
private final Logger log = LoggerFactory.getLogger(ThumbResource.class);
45+
46+
@Inject
47+
private RetrievalService retrievalService;
48+
49+
@Inject
50+
private StoreImageService storeImageService;
51+
52+
@RequestMapping(value = "/{id}",
53+
method = RequestMethod.GET,
54+
produces = MediaType.IMAGE_PNG_VALUE)
55+
@Timed
56+
@RolesAllowed(AuthoritiesConstants.ANONYMOUS)
57+
ResponseEntity<byte[]> getStoreImage(@PathVariable Long id,@RequestParam(required = false) Integer size,@RequestParam(required = false,defaultValue = "index") String type) throws ResourceNotFoundException, NoValidPictureException {
58+
log.debug("REST request to get image thumb : " + id);
59+
60+
try {
61+
BufferedImage image = null;
62+
if(type.equals("index")) {
63+
image = storeImageService.readIndexImage(id);
64+
} else {
65+
image = storeImageService.readSearchImage(id);
66+
}
67+
68+
if(size!=null) {
69+
SizeUtils sizeCompute = new SizeUtils(image.getWidth(),image.getHeight(),size,size);
70+
sizeCompute = sizeCompute.computeThumbSize();
71+
//resize image
72+
image = resizePicture(image,sizeCompute.getWidth(),sizeCompute.getHeight());
73+
}
74+
75+
76+
final HttpHeaders headers = new HttpHeaders();
77+
headers.setContentType(MediaType.IMAGE_PNG);
78+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
79+
ImageIO.write( image, "png", baos );
80+
baos.flush();
81+
byte[] imageInByte = baos.toByteArray();
82+
baos.close();
83+
return new ResponseEntity<>(imageInByte, headers, HttpStatus.OK);
84+
} catch(IOException e) {
85+
throw new NoValidPictureException("Image with "+id + "cannot be read");
86+
}
87+
}
88+
89+
private static BufferedImage resizePicture(BufferedImage image, int targetWidth, int targetHeight) {
90+
int type = image.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : image.getType();
91+
BufferedImage resizedImage = new BufferedImage(targetWidth, targetHeight, type);
92+
Graphics2D g = resizedImage.createGraphics();
93+
g.setComposite(AlphaComposite.Src);
94+
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
95+
g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_SPEED);
96+
g.drawImage(image, 0, 0, targetWidth, targetHeight, null);
97+
g.dispose();
98+
return resizedImage;
99+
100+
}
101+
}

src/main/webapp/views/main.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ <h1 translate="main.title">Welcome to CBIRest!</h1>
6969
<div class="container" ng-show="results" class="col-sm-10">
7070
<div class="col-md-3"/>
7171
<div class="col-md-6">
72-
<img class="img-responsive img-thumbnail" ng-if="results" ng-src="/api/images/{{results.id}}/thumb.png?size=256&type=search">
72+
<img class="img-responsive img-thumbnail" ng-if="results" ng-src="/thumb/{{results.id}}.png?size=256&type=search">
7373
</div>
7474
<div class="col-md-3"/>
7575
</div>
@@ -81,7 +81,7 @@ <h1 translate="main.title">Welcome to CBIRest!</h1>
8181
<table class="table table-striped table-bordered">
8282
<tr ng-repeat="item in results.data">
8383
<td data-title="'id'">{{item.id}}</td>
84-
<td data-title="'thumb'"><img ng-if="results" class="img-responsive img-thumbnail" ng-src="/api/images/{{item.id}}/thumb.png?size=128"></td>
84+
<td data-title="'thumb'"><img ng-if="results" class="img-responsive img-thumbnail" ng-src="/thumb/{{item.id}}.png?size=128"></td>
8585
<td data-title="'similarities'">{{item.similarities}}</td>
8686
<td data-title="'properties'">{{item.properties}}</td>
8787
</tr>

0 commit comments

Comments
 (0)