TileController.java
/*
* Copyright 2024 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.gringlobal.api.v2.impl;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import org.gringlobal.service.AccessionService;
import org.gringlobal.service.ShortFilterService;
import org.gringlobal.service.filter.AccessionFilter;
import org.gringlobal.util.ColorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import lombok.extern.slf4j.Slf4j;
/**
* This is not an API controller, it's accessible to anonymous users.
*/
@Controller
@Slf4j
public class TileController {
@Autowired
private AccessionService accessionService;
@Autowired
private ShortFilterService shortFilterService;
@Autowired
private ThreadPoolTaskExecutor taskExecutor;
@RequestMapping(value = "/tile/a/{zoom:\\d+}/{x:\\d+}/{y:\\d+}", produces = MediaType.IMAGE_PNG_VALUE)
public ResponseEntity<?> tile(@RequestParam(value = "f", required = true) String filterCode,
@RequestParam(value = "color", required = false) String color,
@PathVariable("x") int x, @PathVariable("y") int y, @PathVariable("zoom") int zoom) throws IOException, ExecutionException, InterruptedException {
return taskExecutor.submit(() -> {
try {
AccessionFilter appliedFilters = shortFilterService.filterByCode(filterCode, AccessionFilter.class);
byte[] image = accessionService.getTile(appliedFilters, zoom, x, y);;
image = ColorUtil.changeColor("#578218", color, image); // #578218 is used by resources/tileserver/accessionDotX.png
// HTTP maxAge
int maxAge;
if (zoom < 8) {
maxAge = 60 * 60; // 1 hr
} else {
maxAge = 60 * 5; // 5 min
}
return ResponseEntity.status(HttpStatus.OK)
.header(HttpHeaders.CACHE_CONTROL, "max-age=" + maxAge + ", s-maxage=" + maxAge + ", public, no-transform")
.contentType(MediaType.IMAGE_PNG)
.header(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN, "*")
.contentLength(image.length)
.body(image);
} catch (Exception e) {
log.warn("Exception in tile request processing", e);
return ResponseEntity.badRequest().build();
}
}).get();
}
}