Skip to content

Instantly share code, notes, and snippets.

@CodingBash
Created January 31, 2026 14:46
Show Gist options
  • Select an option

  • Save CodingBash/35a40b34240293ea3c5db470a80a8b2c to your computer and use it in GitHub Desktop.

Select an option

Save CodingBash/35a40b34240293ea3c5db470a80a8b2c to your computer and use it in GitHub Desktop.
package edu.columbia.biology.processors;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponents;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.S3Object;
import com.amazonaws.services.s3.model.S3ObjectInputStream;
import edu.columbia.biology.RestTemplateFactory;
@Component
public class ContentRetrievalProcessor {
private static final Logger LOGGER = LoggerFactory.getLogger(ContentRetrievalProcessor.class);
@Autowired
private AmazonS3 amazonS3Client;
@Autowired
private RestTemplateFactory restTemplateFactory;
public <T> T retrieveResponseFromUri(UriComponents uriComponents, Class<T> clazz) {
LOGGER.info("Retrieving resource from {}", uriComponents.toUriString());
RestTemplate restTemplate = restTemplateFactory.getObject();
ResponseEntity<T> responseEntity = restTemplate.exchange(uriComponents.toUriString(), HttpMethod.GET,
new HttpEntity<T>(restTemplateFactory.createHeaders()), clazz);
return responseEntity.getBody();
}
public void sendImageToClient(String imageUUID, HttpServletResponse response) throws IOException{
S3Object object = amazonS3Client.getObject("cellx-files-test", imageUUID);
S3ObjectInputStream inputStream = object.getObjectContent();
response.setContentType(MediaType.IMAGE_PNG_VALUE);
response.setHeader("Cache-Control", "public, max-age=1210000");
IOUtils.copy(inputStream, response.getOutputStream());
response.flushBuffer();
}
}
package edu.columbia.biology.controller;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import edu.columbia.biology.processors.CellxPrincipalProcessor;
import edu.columbia.biology.processors.ContentRetrievalProcessor;
import edu.columbia.biology.service.ImageService;
/*
* Test cases
* Access: image/3d12b0a7-7604-5935-a862-84f306b28ff8.png
* Access controlled: image/abb47011-1f4f-5ae4-a4dd-5e8b834527d2.png
*/
@RestController
public class S3ContentDeliveryController {
private static final Logger LOGGER = LoggerFactory.getLogger(S3ContentDeliveryController.class);
@Autowired
private ImageService imageService;
@Autowired
private ContentRetrievalProcessor contentRetrievalProcessor;
@Autowired
private CellxPrincipalProcessor cellxPrincipalProcessor;
@RequestMapping(value = "/s3/image", method = RequestMethod.GET)
public void retrieveS3ImagesObjectWithUUID(@RequestParam("imageUUID") String imageUUID,
Authentication authentication, HttpServletResponse response) throws IOException {
if (imageService.imageIsUserAccessible(authentication, imageUUID)) {
contentRetrievalProcessor.sendImageToClient(imageUUID, response);
} else {
if (authentication != null) {
LOGGER.info("User {} does not have access to image {}", cellxPrincipalProcessor.retrieveUserIdFromPrincipal(authentication),
imageUUID);
} else {
LOGGER.info("Guest user does not have access to image {}", imageUUID);
}
throw new AccessDeniedException("User does not have access to the image resouorce");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment