com.foreach.imageserver.client.RemoteImageServerClient Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of imageserver-client Show documentation
Show all versions of imageserver-client Show documentation
ImageServer application modules.
package com.foreach.imageserver.client;
import com.foreach.imageserver.dto.*;
import com.foreach.imageserver.logging.LogHelper;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.net.URI;
import java.util.Date;
import java.util.List;
import java.util.Map;
import static org.springframework.util.Assert.notNull;
/**
* Represents a client for a remote ImageServer endpoint.
*/
public class RemoteImageServerClient extends AbstractImageServerClient
{
private static final Logger LOG = LoggerFactory.getLogger( RemoteImageServerClient.class );
private static final class ResponseTypes
{
private ResponseTypes() {
}
private static final ParameterizedTypeReference>> RESOLUTIONS =
new ParameterizedTypeReference>>()
{
};
private static final ParameterizedTypeReference>> MODIFICATIONS =
new ParameterizedTypeReference>>()
{
};
private static final ParameterizedTypeReference> OBJECT =
new ParameterizedTypeReference>()
{
};
private static final ParameterizedTypeReference> IMAGE_INFO =
new ParameterizedTypeReference>()
{
};
}
private final String imageServerAccessToken;
private RestTemplate restTemplate;
public RemoteImageServerClient( String imageServerEndpoint, String imageServerAccessToken ) {
this( imageServerEndpoint, imageServerAccessToken, new RestTemplate() );
}
public RemoteImageServerClient( String imageServerEndpoint, String imageServerAccessToken, RestTemplate restTemplate ) {
super( imageServerEndpoint );
this.imageServerAccessToken = imageServerAccessToken;
this.restTemplate = restTemplate;
}
public void setRestTemplate( RestTemplate restTemplate ) {
notNull( restTemplate, "RestTemplate can not be null" );
this.restTemplate = restTemplate;
}
@Override
public InputStream imageStream( String imageId,
String context,
Integer width,
Integer height,
ImageTypeDto imageType ) {
return imageStream( imageId, context, new ImageResolutionDto( width, height ),
new ImageVariantDto( imageType ) );
}
@Override
public InputStream imageStream( String imageId,
String context,
ImageResolutionDto imageResolution,
ImageVariantDto imageVariant ) {
if ( StringUtils.isBlank( imageId ) || context == null || imageResolution == null || imageVariant == null ) {
LOG.warn(
"Null parameters not allowed - ImageServerClientImpl#imageStream: imageId={}, context={}, imageResolution={}, imageVariant={}",
LogHelper.flatten( imageId, context, imageResolution, imageVariant ) );
}
MultiValueMap queryParams = new LinkedMultiValueMap<>();
queryParams.set( "iid", imageId );
queryParams.set( "context", context );
addQueryParams( queryParams, imageResolution );
addQueryParams( queryParams, imageVariant );
hashBuilder().ifPresent(
hashBuilder -> queryParams.set(
"hash", hashBuilder.calculateHash( context, null, imageResolution, imageVariant )
)
);
return new ByteArrayInputStream( httpGet( ENDPOINT_IMAGE_VIEW, queryParams, byte[].class ) );
}
@Override
public InputStream imageStream( String imageId,
ImageModificationDto imageModificationDto,
ImageVariantDto imageVariant ) {
if ( StringUtils.isBlank( imageId ) || imageModificationDto == null || imageVariant == null ) {
LOG.warn(
"Null parameters not allowed - ImageServerClientImpl#imageStream: imageId={}, imageModificationDto={}, imageResolution={}, imageVariant={}",
LogHelper.flatten( imageId, imageModificationDto, imageVariant ) );
}
MultiValueMap queryParams = new LinkedMultiValueMap<>();
queryParams.set( "token", imageServerAccessToken );
queryParams.set( "iid", imageId );
addQueryParams( queryParams, imageModificationDto );
addQueryParams( queryParams, imageVariant );
return new ByteArrayInputStream( httpGet( ENDPOINT_IMAGE_RENDER, queryParams, byte[].class ) );
}
@Override
public InputStream imageStream( byte[] imageData,
ImageModificationDto imageModificationDto,
ImageVariantDto imageVariant ) {
if ( imageData == null || imageModificationDto == null || imageVariant == null ) {
LOG.warn(
"Null parameters not allowed - ImageServerClientImpl#imageStream: imageData={}, imageModificationDto={}, imageResolution={}, imageVariant={}",
LogHelper.flatten( imageData == null, imageModificationDto, imageVariant ) );
}
MultiValueMap queryParams = new LinkedMultiValueMap<>();
queryParams.set( "token", imageServerAccessToken );
addQueryParams( queryParams, imageModificationDto );
addQueryParams( queryParams, imageVariant );
MultiValueMap bodyParts = getImageDataAsBody( imageData );
return new ByteArrayInputStream( httpPost( ENDPOINT_IMAGE_RENDER, queryParams, bodyParts, byte[].class ) );
}
@Override
public ImageInfoDto loadImage( String imageId, byte[] imageBytes ) {
return loadImage( imageId, imageBytes, false );
}
@Override
public ImageInfoDto loadImage( String imageId, byte[] imageBytes, boolean replaceExisting ) {
return loadImage( imageId, imageBytes, null, replaceExisting );
}
@Override
public ImageInfoDto loadImage( String imageId, byte[] imageBytes, Date imageDate ) {
return loadImage( imageId, imageBytes, imageDate, false );
}
@Override
public ImageInfoDto loadImage( String imageId, byte[] imageBytes, Date imageDate, boolean replaceExisting ) {
if ( StringUtils.isBlank( imageId ) ) {
throw new ImageServerException( "You must specify an imageId when loading an image." );
}
if ( imageBytes == null || imageBytes.length == 0 ) {
throw new ImageServerException( "Unable to load an image with empty byte data." );
}
MultiValueMap queryParams = new LinkedMultiValueMap<>();
queryParams.set( "token", imageServerAccessToken );
queryParams.set( "iid", imageId );
queryParams.set( "replaceExisting", Boolean.toString( replaceExisting ) );
if ( imageDate != null ) {
queryParams.set( "imageTimestamp", Long.toString( imageDate.getTime() ) );
}
MultiValueMap bodyParts = getImageDataAsBody( imageBytes );
try {
return httpPost( ENDPOINT_IMAGE_LOAD, queryParams, bodyParts, ResponseTypes.IMAGE_INFO );
}
catch ( RuntimeException e ) {
LOG.error(
"Loading image caused exception - ImageServerClientImpl#loadImage: imageId={}, imageBytes={}, imageDate={}",
LogHelper.flatten( imageId, imageBytes.length, imageDate ) );
throw e;
}
}
private MultiValueMap getImageDataAsBody( final byte[] imageBytes ) {
MultiValueMap bodyParts = new LinkedMultiValueMap<>();
bodyParts.set( "imageData", new ByteArrayResource( imageBytes )
{
@Override
public String getFilename() {
return "imageData";
}
} );
return bodyParts;
}
@Override
public boolean deleteImage( String imageId ) {
if ( StringUtils.isBlank( imageId ) ) {
throw new ImageServerException( "You must specify an imageId when deleting an image." );
}
MultiValueMap queryParams = new LinkedMultiValueMap<>();
queryParams.set( "token", imageServerAccessToken );
queryParams.set( "iid", imageId );
Object map = httpPost( ENDPOINT_IMAGE_DELETE, queryParams, new LinkedMultiValueMap(),
ResponseTypes.OBJECT );
if ( map instanceof Map ) {
return Boolean.valueOf( "" + ( (Map) map ).get( "deleted" ) );
}
return false;
}
@Override
public boolean imageExists( String imageId ) {
return imageInfo( imageId ).isExisting();
}
@Override
public ImageInfoDto imageInfo( String imageId ) {
MultiValueMap queryParams = new LinkedMultiValueMap<>();
queryParams.set( "token", imageServerAccessToken );
queryParams.set( "iid", imageId );
return httpGet( ENDPOINT_IMAGE_INFO, queryParams, ResponseTypes.IMAGE_INFO );
}
@Override
public List pregenerateResolutions( String imageId ) {
MultiValueMap queryParams = new LinkedMultiValueMap<>();
queryParams.set( "token", imageServerAccessToken );
queryParams.set( "iid", imageId );
return httpGet( ENDPOINT_IMAGE_PREGENERATE, queryParams, ResponseTypes.RESOLUTIONS );
}
@Override
public void registerImageModification( String imageId,
String context,
ImageModificationDto imageModification ) {
MultiValueMap queryParams = new LinkedMultiValueMap<>();
queryParams.set( "token", imageServerAccessToken );
queryParams.set( "iid", imageId );
queryParams.set( "context", context );
addQueryParams( queryParams, imageModification );
httpGet( ENDPOINT_MODIFICATION_REGISTER, queryParams, ResponseTypes.OBJECT );
}
@Override
public void registerImageModifications( String imageId,
String context,
List imageModifications ) {
MultiValueMap queryParams = new LinkedMultiValueMap<>();
queryParams.set( "token", imageServerAccessToken );
queryParams.set( "iid", imageId );
queryParams.set( "context", context );
MultiValueMap bodyParts = new LinkedMultiValueMap<>();
addQueryParams( bodyParts, imageModifications );
httpPost( ENDPOINT_MODIFICATION_REGISTER_LIST, queryParams, bodyParts, ResponseTypes.OBJECT );
}
@Override
public List listAllowedResolutions( String context ) {
MultiValueMap queryParams = new LinkedMultiValueMap<>();
queryParams.set( "token", imageServerAccessToken );
queryParams.set( "context", context );
return httpGet( ENDPOINT_RESOLUTION_LIST, queryParams, ResponseTypes.RESOLUTIONS );
}
@Override
public List listConfigurableResolutions( String context ) {
MultiValueMap queryParams = new LinkedMultiValueMap<>();
queryParams.set( "token", imageServerAccessToken );
queryParams.set( "context", context );
queryParams.set( "configurableOnly", "true" );
return httpGet( ENDPOINT_RESOLUTION_LIST, queryParams, ResponseTypes.RESOLUTIONS );
}
@Override
public List listModifications( String imageId, String context ) {
MultiValueMap queryParams = new LinkedMultiValueMap<>();
queryParams.set( "token", imageServerAccessToken );
queryParams.set( "iid", imageId );
queryParams.set( "context", context );
return httpGet( ENDPOINT_MODIFICATION_LIST, queryParams, ResponseTypes.MODIFICATIONS );
}
protected T httpGet( String path, MultiValueMap queryParams, Class responseType ) {
URI url = buildUri( path, queryParams );
HttpEntity> request = new HttpEntity>( new LinkedMultiValueMap() );
ResponseEntity response = restTemplate.exchange( url, HttpMethod.GET, request, responseType );
return response.getBody();
}
protected T httpGet( String path,
MultiValueMap queryParams,
ParameterizedTypeReference> responseType ) {
URI url = buildUri( path, queryParams );
HttpEntity> request = new HttpEntity>( new LinkedMultiValueMap() );
JsonResponse body;
try {
ResponseEntity> response =
restTemplate.exchange( url, HttpMethod.GET, request, responseType );
body = response.getBody();
}
catch ( Exception e ) {
LOG.error( e.getMessage() );
throw new ImageServerException( e.getMessage() );
}
if ( !body.isSuccess() ) {
throw new ImageServerException( body.getErrorMessage() );
}
return body.getResult();
}
protected T httpPost( String path,
MultiValueMap queryParams,
MultiValueMap bodyParams,
Class responseType ) {
URI url = buildUri( path, queryParams );
HttpEntity> request =
new HttpEntity>( bodyParams );
ResponseEntity response = restTemplate.exchange( url, HttpMethod.POST, request, responseType );
return response.getBody();
}
protected T httpPost( String path,
MultiValueMap queryParams,
MultiValueMap bodyParams,
ParameterizedTypeReference> responseType ) {
URI url = buildUri( path, queryParams );
HttpEntity> request =
new HttpEntity>( bodyParams );
ResponseEntity> response =
restTemplate.exchange( url, HttpMethod.POST, request, responseType );
JsonResponse body = response.getBody();
if ( !body.isSuccess() ) {
throw new ImageServerException( body.getErrorMessage() );
}
return body.getResult();
}
}