View Javadoc
1   package net.sumaris.server.http.rest;
2   
3   /*-
4    * #%L
5    * SUMARiS:: Server
6    * %%
7    * Copyright (C) 2018 SUMARiS Consortium
8    * %%
9    * This program is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU General Public License as
11   * published by the Free Software Foundation, either version 3 of the
12   * License, or (at your option) any later version.
13   * 
14   * This program is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Public
20   * License along with this program.  If not, see
21   * <http://www.gnu.org/licenses/gpl-3.0.html>.
22   * #L%
23   */
24  
25  
26  import net.sumaris.core.service.administration.DepartmentService;
27  import net.sumaris.core.service.administration.PersonService;
28  import net.sumaris.core.service.technical.SoftwareService;
29  import net.sumaris.core.vo.data.ImageAttachmentVO;
30  import net.sumaris.core.vo.technical.SoftwareVO;
31  import net.sumaris.server.config.SumarisServerConfiguration;
32  import net.sumaris.server.config.SumarisServerConfigurationOption;
33  import net.sumaris.server.service.administration.ImageService;
34  import org.apache.commons.codec.binary.Base64;
35  import org.apache.commons.collections4.MapUtils;
36  import org.apache.commons.io.IOUtils;
37  import org.apache.commons.lang3.StringUtils;
38  import org.slf4j.Logger;
39  import org.slf4j.LoggerFactory;
40  import org.springframework.beans.factory.annotation.Autowired;
41  import org.springframework.context.ResourceLoaderAware;
42  import org.springframework.core.io.Resource;
43  import org.springframework.core.io.ResourceLoader;
44  import org.springframework.http.MediaType;
45  import org.springframework.http.ResponseEntity;
46  import org.springframework.web.bind.annotation.*;
47  import org.springframework.web.servlet.view.RedirectView;
48  
49  import java.io.ByteArrayOutputStream;
50  import java.io.IOException;
51  import java.io.InputStream;
52  
53  @RestController
54  public class ImageRestController implements ResourceLoaderAware {
55  
56      /* Logger */
57      private static final Logger log = LoggerFactory.getLogger(ImageRestController.class);
58  
59      @Autowired
60      private PersonService personService;
61  
62      @Autowired
63      private DepartmentService departmentService;
64  
65      @Autowired
66      private ImageService imageService;
67  
68      @Autowired
69      private SoftwareService softwareService;
70  
71      @Autowired
72      private SumarisServerConfiguration config;
73  
74      private ResourceLoader resourceLoader;
75  
76      public void setResourceLoader(ResourceLoader resourceLoader) {
77          this.resourceLoader = resourceLoader;
78      }
79  
80      @ResponseBody
81      @RequestMapping(value = RestPaths.PERSON_AVATAR_PATH, method = RequestMethod.GET,
82              produces = {MediaType.IMAGE_JPEG_VALUE, MediaType.IMAGE_PNG_VALUE})
83      public ResponseEntity<byte[]> getPersonAvatar(@PathVariable(name="pubkey") String pubkey) {
84          ImageAttachmentVO image = personService.getAvatarByPubkey(pubkey);
85          if (image == null) {
86              return ResponseEntity.notFound().build();
87          }
88  
89          byte[] bytes = Base64.decodeBase64(image.getContent());
90          return ResponseEntity.ok()
91                  .contentLength(bytes.length)
92                  .contentType(MediaType.parseMediaType(image.getContentType()))
93                  .body(bytes);
94      }
95  
96      @ResponseBody
97      @RequestMapping(value = RestPaths.DEPARTMENT_LOGO_PATH, method = RequestMethod.GET,
98              produces = {MediaType.IMAGE_JPEG_VALUE, MediaType.IMAGE_PNG_VALUE})
99      public ResponseEntity<byte[]> getDepartmentLogo(@PathVariable(name="label") String label) {
100         ImageAttachmentVO image = departmentService.getLogoByLabel(label);
101         if (image == null) {
102             return ResponseEntity.notFound().build();
103         }
104 
105         byte[] bytes = Base64.decodeBase64(image.getContent());
106         return ResponseEntity.ok()
107                 .contentLength(bytes.length)
108                 .contentType(MediaType.parseMediaType(image.getContentType()))
109                 .body(bytes);
110     }
111 
112     @ResponseBody
113     @RequestMapping(value = RestPaths.IMAGE_PATH, method = RequestMethod.GET,
114             produces = {MediaType.IMAGE_JPEG_VALUE, MediaType.IMAGE_PNG_VALUE})
115     public ResponseEntity<byte[]> getImage(@PathVariable(name="id") String id) {
116         ImageAttachmentVO image = imageService.get(Integer.parseInt(id));
117         if (image == null) {
118             return ResponseEntity.notFound().build();
119         }
120 
121         byte[] bytes = Base64.decodeBase64(image.getContent());
122         return ResponseEntity.ok()
123                 .contentLength(bytes.length)
124                 .contentType(MediaType.parseMediaType(image.getContentType()))
125                 .body(bytes);
126     }
127 
128     @ResponseBody
129     @RequestMapping(value = RestPaths.FAVICON, method = RequestMethod.GET,
130             produces = {MediaType.IMAGE_JPEG_VALUE, MediaType.IMAGE_PNG_VALUE})
131     public Object getFavicon() {
132 
133         SoftwareVO software = softwareService.getDefault();
134         if (software == null) return ResponseEntity.notFound().build();
135 
136         String favicon = MapUtils.getString(software.getProperties(), SumarisServerConfigurationOption.SITE_FAVICON.getKey());
137         if (StringUtils.isBlank(favicon)) {
138             return ResponseEntity.notFound().build();
139         }
140 
141         if (favicon.startsWith(ImageService.URI_IMAGE_SUFFIX)) {
142             String imageId = favicon.substring(ImageService.URI_IMAGE_SUFFIX.length());
143             return getImage(imageId);
144         }
145 
146         // Redirect to the URL
147         if (favicon.startsWith("http")) {
148             return new RedirectView(favicon);
149         }
150 
151         // Try to read as a local resource
152         try {
153             Resource faviconResource = resourceLoader.getResource(favicon);
154             InputStream in = faviconResource.getInputStream();
155             ByteArrayOutputStream bos = new ByteArrayOutputStream();
156 
157             IOUtils.copy(in, bos);
158             bos.close();
159             in.close();
160 
161             return ResponseEntity.ok()
162                     .contentLength(faviconResource.contentLength())
163                     .body(bos.toByteArray());
164         } catch(IOException e) {
165             // Not a local resource: continue
166         }
167 
168         // Redirect as a relative URL
169         return new RedirectView(config.getServerUrl() + (favicon.startsWith("/") ? "" : "/") + favicon);
170 
171     }
172 }