Skip to content

Commit

Permalink
feat: image controller (#22)
Browse files Browse the repository at this point in the history
* feat: add images controller

* feat: increase image size

* feat: add RefTypePathParamMapper

* feat: fix test failure keycloak
  • Loading branch information
jsteenke authored Feb 22, 2024
1 parent 777bff1 commit a97bc3f
Show file tree
Hide file tree
Showing 12 changed files with 1,791 additions and 1,113 deletions.
16 changes: 16 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@
<inputSpec>src/main/openapi/onecx-product-store-bff.yaml</inputSpec>
<apiPackage>gen.org.tkit.onecx.product.store.bff.rs.internal</apiPackage>
<modelPackage>gen.org.tkit.onecx.product.store.bff.rs.internal.model</modelPackage>
<typeMappings>File=byte[]</typeMappings>
</configuration>
</execution>
</executions>
Expand Down Expand Up @@ -205,6 +206,21 @@
<skipCache>true</skipCache>
</configuration>
</execution>
<execution>
<id>onecx-image-internal</id>
<phase>generate-resources</phase>
<goals>
<goal>wget</goal>
</goals>
<configuration>
<url>
https://raw.githubusercontent.com/jsteenke/onecx-product-store-svc/main/src/main/openapi/onecx-image-internal-openapi.yaml
</url>
<outputDirectory>target/tmp/openapi</outputDirectory>
<outputFileName>onecx-image-internal.yaml</outputFileName>
<skipCache>true</skipCache>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package org.tkit.onecx.product.store.bff.rs.controllers;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.transaction.Transactional;
import jakarta.validation.ConstraintViolationException;
import jakarta.ws.rs.WebApplicationException;
import jakarta.ws.rs.core.HttpHeaders;
import jakarta.ws.rs.core.Response;

import org.eclipse.microprofile.rest.client.inject.RestClient;
import org.jboss.resteasy.reactive.RestResponse;
import org.jboss.resteasy.reactive.server.ServerExceptionMapper;
import org.tkit.onecx.product.store.bff.rs.mappers.ExceptionMapper;
import org.tkit.onecx.product.store.bff.rs.mappers.ImagesMapper;
import org.tkit.quarkus.log.cdi.LogService;

import gen.org.tkit.onecx.product.store.bff.rs.internal.ImagesInternalApiService;
import gen.org.tkit.onecx.product.store.bff.rs.internal.model.ImageInfoDTO;
import gen.org.tkit.onecx.product.store.bff.rs.internal.model.ProblemDetailResponseDTO;
import gen.org.tkit.onecx.product.store.bff.rs.internal.model.RefTypeDTO;
import gen.org.tkit.onecx.product.store.client.api.ImagesInternalApi;
import gen.org.tkit.onecx.product.store.client.model.ImageInfo;

@ApplicationScoped
@Transactional(value = Transactional.TxType.NOT_SUPPORTED)
@LogService
public class ImagesRestController implements ImagesInternalApiService {

@Inject
@RestClient
ImagesInternalApi imageApi;

@Inject
ImagesMapper imageMapper;

@Inject
ExceptionMapper exceptionMapper;

@Override
public Response getImage(String refId, RefTypeDTO refType) {
try (Response response = imageApi.getImage(refId, imageMapper.map(refType))) {
var contentType = response.getHeaderString(HttpHeaders.CONTENT_TYPE);
var contentLength = response.getHeaderString(HttpHeaders.CONTENT_LENGTH);
var body = response.readEntity(byte[].class);
if (contentType != null && contentLength != null && body != null && body.length != 0) {
return Response.status(response.getStatus())
.header(HttpHeaders.CONTENT_TYPE, contentType)
.header(HttpHeaders.CONTENT_LENGTH, contentLength)
.entity(body)
.build();
} else {
return Response.status(Response.Status.BAD_REQUEST).build();
}
}
}

@Override
public Response updateImage(String refId, RefTypeDTO refType, byte[] body, Integer contentLength) {

try (Response response = imageApi.updateImage(refId, imageMapper.map(refType), body,
contentLength)) {

ImageInfoDTO imageInfoDTO = imageMapper.map(response.readEntity(ImageInfo.class));
return Response.status(response.getStatus()).entity(imageInfoDTO).build();
}
}

@Override
public Response uploadImage(Integer contentLength, String refId, RefTypeDTO refType, byte[] body) {

try (Response response = imageApi.uploadImage(contentLength, refId, imageMapper.map(refType),
body)) {
ImageInfoDTO imageInfoDTO = imageMapper.map(response.readEntity(ImageInfo.class));
return Response.status(response.getStatus()).entity(imageInfoDTO).build();
}
}

@ServerExceptionMapper
public RestResponse<ProblemDetailResponseDTO> constraint(ConstraintViolationException ex) {
return exceptionMapper.constraint(ex);
}

@ServerExceptionMapper
public Response restException(WebApplicationException ex) {
return Response.status(ex.getResponse().getStatus()).build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.tkit.onecx.product.store.bff.rs.mappers;

import org.mapstruct.Mapper;
import org.tkit.quarkus.rs.mappers.OffsetDateTimeMapper;

import gen.org.tkit.onecx.product.store.bff.rs.internal.model.ImageInfoDTO;
import gen.org.tkit.onecx.product.store.bff.rs.internal.model.RefTypeDTO;
import gen.org.tkit.onecx.product.store.client.model.ImageInfo;
import gen.org.tkit.onecx.product.store.client.model.RefType;

@Mapper(uses = { OffsetDateTimeMapper.class })
public interface ImagesMapper {

ImageInfoDTO map(ImageInfo image);

ImageInfo map(ImageInfoDTO image);

RefType map(RefTypeDTO refType);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.tkit.onecx.product.store.bff.rs.mappers;

import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

import jakarta.inject.Singleton;
import jakarta.ws.rs.ext.ParamConverter;
import jakarta.ws.rs.ext.ParamConverterProvider;
import jakarta.ws.rs.ext.Provider;

import gen.org.tkit.onecx.product.store.client.model.RefType;

@Provider
@Singleton
public class RefTypePathParamMapper implements ParamConverterProvider {
public RefTypePathParamMapper() {

}

@Override
public <T> ParamConverter<T> getConverter(Class<T> rawType, Type genericType, Annotation[] annotations) {

return rawType.isAssignableFrom(RefType.class) ? (ParamConverter<T>) new RefTypeParamConverter() : null;
}

public static class RefTypeParamConverter implements ParamConverter<RefType> {
public RefTypeParamConverter() {
}

public RefType fromString(String value) {
return value != null && !value.isBlank() ? RefType.fromString(value) : null;
}

public String toString(RefType value) {
return value == null ? null : value.toString();
}
}

}
Loading

0 comments on commit a97bc3f

Please sign in to comment.