-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
formatting pom formatting pom include some tweaks, add jpg, rename path method, fix testGetImage fix format files pass CItyDTO instead of String
- Loading branch information
1 parent
5dce2e5
commit 1758449
Showing
11 changed files
with
510 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
http/http-advanced-reactive/src/main/java/io/quarkus/ts/http/advanced/reactive/City.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package io.quarkus.ts.http.advanced.reactive; | ||
|
||
import jakarta.xml.bind.annotation.XmlElement; | ||
import jakarta.xml.bind.annotation.XmlRootElement; | ||
|
||
@XmlRootElement | ||
public class City { | ||
@XmlElement | ||
private String name; | ||
|
||
@XmlElement | ||
private String country; | ||
|
||
public City(String name, String country) { | ||
this.name = name; | ||
this.country = country; | ||
} | ||
|
||
public City() { | ||
} | ||
|
||
} |
34 changes: 34 additions & 0 deletions
34
...ttp-advanced-reactive/src/main/java/io/quarkus/ts/http/advanced/reactive/CityListDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package io.quarkus.ts.http.advanced.reactive; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import jakarta.xml.bind.annotation.XmlElement; | ||
import jakarta.xml.bind.annotation.XmlRootElement; | ||
|
||
@XmlRootElement | ||
public class CityListDTO { | ||
|
||
@XmlElement(name = "city") | ||
private List<City> cities = new ArrayList<>(); | ||
|
||
public CityListDTO() { | ||
} | ||
|
||
public CityListDTO(List<City> cities) { | ||
this.cities = cities; | ||
} | ||
|
||
public List<City> getCityList() { | ||
return cities; | ||
} | ||
|
||
public void setCities(List<City> cities) { | ||
this.cities = cities; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return cities.toString(); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...advanced-reactive/src/main/java/io/quarkus/ts/http/advanced/reactive/CityListWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package io.quarkus.ts.http.advanced.reactive; | ||
|
||
import java.util.List; | ||
|
||
import jakarta.xml.bind.annotation.XmlElement; | ||
import jakarta.xml.bind.annotation.XmlRootElement; | ||
|
||
@XmlRootElement | ||
public class CityListWrapper { | ||
|
||
private List<City> cities; | ||
|
||
@XmlElement(name = "cities") | ||
private List<City> citiesList; | ||
|
||
public CityListWrapper() { | ||
} | ||
|
||
public CityListWrapper(List<City> cities) { | ||
this.cities = cities; | ||
} | ||
|
||
public List<City> getCities() { | ||
return cities; | ||
} | ||
|
||
public void setCities(List<City> cities) { | ||
this.citiesList = cities; | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
...eactive/src/main/java/io/quarkus/ts/http/advanced/reactive/CityListWrapperSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package io.quarkus.ts.http.advanced.reactive; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.StringWriter; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.xml.bind.JAXBContext; | ||
import jakarta.xml.bind.JAXBException; | ||
import jakarta.xml.bind.Marshaller; | ||
import jakarta.xml.bind.Unmarshaller; | ||
import jakarta.xml.bind.annotation.XmlRootElement; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
@ApplicationScoped | ||
@XmlRootElement(name = "cityListWrapper") | ||
public class CityListWrapperSerializer { | ||
private final Logger logger = LoggerFactory.getLogger(CityListWrapperSerializer.class); | ||
|
||
public CityListDTO fromXML(String xml) { | ||
if (xml == null || xml.isEmpty()) { | ||
return new CityListDTO(); | ||
} | ||
|
||
try { | ||
JAXBContext jaxbContext = JAXBContext.newInstance(CityListDTO.class); | ||
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); | ||
CityListDTO cityListDTO = (CityListDTO) unmarshaller.unmarshal(new ByteArrayInputStream(xml.getBytes())); | ||
if (cityListDTO.getCityList().isEmpty()) { | ||
throw new IllegalArgumentException("The XML payload must contain at least one city record."); | ||
} | ||
return cityListDTO; | ||
} catch (JAXBException e) { | ||
logger.error("Error deserializing XML: {}", e.getCause()); | ||
throw new IllegalArgumentException("Error deserializing XML", e); | ||
} | ||
} | ||
|
||
public String toXML(CityListDTO cityListDTO) { | ||
try { | ||
JAXBContext jaxbContext = JAXBContext.newInstance(CityListDTO.class); | ||
Marshaller marshaller = jaxbContext.createMarshaller(); | ||
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); | ||
StringWriter xmlWriter = new StringWriter(); | ||
marshaller.marshal(cityListDTO, xmlWriter); | ||
return xmlWriter.toString(); | ||
} catch (JAXBException e) { | ||
logger.error("Error serializing XML: {}", e); | ||
throw new IllegalArgumentException("Error serializing XML", e); | ||
} | ||
} | ||
} |
175 changes: 175 additions & 0 deletions
175
...tp-advanced-reactive/src/main/java/io/quarkus/ts/http/advanced/reactive/CityResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
package io.quarkus.ts.http.advanced.reactive; | ||
|
||
import static io.quarkus.ts.http.advanced.reactive.MediaTypeResource.APPLICATION_YAML; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import jakarta.inject.Inject; | ||
import jakarta.ws.rs.Consumes; | ||
import jakarta.ws.rs.GET; | ||
import jakarta.ws.rs.POST; | ||
import jakarta.ws.rs.Path; | ||
import jakarta.ws.rs.PathParam; | ||
import jakarta.ws.rs.Produces; | ||
import jakarta.ws.rs.core.MediaType; | ||
import jakarta.ws.rs.core.Response; | ||
|
||
import org.apache.commons.io.IOUtils; | ||
import org.eclipse.microprofile.openapi.annotations.parameters.RequestBody; | ||
import org.jboss.logging.Logger; | ||
import org.jboss.resteasy.reactive.PartType; | ||
import org.jboss.resteasy.reactive.RestForm; | ||
import org.yaml.snakeyaml.Yaml; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper; | ||
|
||
import io.quarkus.logging.Log; | ||
|
||
@Path("/city") | ||
public class CityResource { | ||
|
||
private static final String YAML_FILE_PATH = "payload.yaml"; | ||
|
||
private static final Logger LOG = Logger.getLogger(CityResource.class); | ||
|
||
private final ObjectMapper objectMapper = new YAMLMapper(); | ||
|
||
@Inject | ||
private CityListWrapperSerializer serializer; | ||
|
||
private List<City> cityList = new ArrayList<>(); | ||
|
||
@GET | ||
@Produces(value = MediaType.APPLICATION_XML) | ||
public CityListWrapper getCities() { | ||
LOG.info("Received request to getCities"); | ||
|
||
if (cityList.isEmpty()) { | ||
cityList.add(new City("San Bernardino", "EEUU")); | ||
cityList.add(new City("Brno", "Czech Republic")); | ||
cityList.add(new City("Zaragoza", "Spain")); | ||
} | ||
return new CityListWrapper(cityList); | ||
} | ||
|
||
@POST | ||
@Consumes(MediaType.APPLICATION_XML) | ||
@Produces(value = MediaType.APPLICATION_XML) | ||
public String createCity(CityListDTO cityListDTO) { | ||
|
||
if (cityListDTO == null) { | ||
LOG.error("Error deserializing XML"); | ||
return Response.status(Response.Status.BAD_REQUEST) | ||
.entity("<error>Invalid XML payload</error>") | ||
.build().toString(); | ||
} | ||
|
||
String responseXML = serializer.toXML(cityListDTO); | ||
|
||
return responseXML; | ||
} | ||
|
||
@POST | ||
@Path("/cities") | ||
@Produces(MediaType.APPLICATION_JSON) | ||
@Consumes("application/x-yaml") | ||
public Object handleConsumedYamlPostRequest(@RequestBody final String yamlPayload) throws IOException { | ||
|
||
LOG.info("Received YAML payload: " + yamlPayload); | ||
|
||
try { | ||
return objectMapper.readValue(yamlPayload, Object.class); | ||
} catch (Exception e) { | ||
return Response.status(Response.Status.BAD_REQUEST) | ||
.entity("Error parsing YAML: " + e.getMessage()) | ||
.build(); | ||
} | ||
} | ||
|
||
@GET | ||
@Path("/getYamlFile") | ||
@Produces(APPLICATION_YAML) | ||
public Response getYamlFile() throws IOException { | ||
try { | ||
CityListDTO cityListDTO = readYamlFile(YAML_FILE_PATH); | ||
|
||
if (cityListDTO != null) { | ||
LOG.info("content ----! " + cityListDTO); | ||
return Response.ok(cityListDTO).build(); | ||
} else { | ||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR) | ||
.entity("Error reading YAML file") | ||
.type("text/plain") | ||
.build(); | ||
} | ||
} catch (IOException e) { | ||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR) | ||
.entity("Error reading YAML file: " + e.getMessage()) | ||
.type("text/plain") | ||
.build(); | ||
} | ||
} | ||
|
||
private CityListDTO readYamlFile(String yamlFilePath) throws IOException { | ||
java.nio.file.Path path = Paths.get(yamlFilePath); | ||
if (!Files.exists(path)) { | ||
throw new IOException("YAML file not found: " + yamlFilePath); | ||
} | ||
|
||
try { | ||
byte[] yamlData = Files.readAllBytes(path); | ||
Yaml yamlParser = new Yaml(); | ||
List<City> cities = yamlParser.loadAs(new ByteArrayInputStream(yamlData), List.class); | ||
return new CityListDTO(cities); | ||
} catch (IOException e) { | ||
LOG.error("Error reading YAML file: {}", yamlFilePath, e); | ||
return null; | ||
} | ||
} | ||
|
||
@GET | ||
@Path("/{imageName}") | ||
@Consumes("image/jpg") | ||
public Response getImage(@PathParam("imageName") String imageName) { | ||
try { | ||
java.nio.file.Path imagePath = Paths.get("wolf_moon_howling.jpg"); | ||
if (Files.exists(imagePath)) { | ||
byte[] imageData = Files.readAllBytes(imagePath); | ||
Log.info("Image retrieval successful for {} " + imageName); | ||
return Response.ok(imageData, determineContentType(imageName)).build(); | ||
} else { | ||
Log.info("Image not found: {} " + imageName); | ||
return Response.status(Response.Status.NOT_FOUND).entity("Image not found").build(); | ||
} | ||
} catch (IOException e) { | ||
Log.error("Error reading image for {}", imageName, e); | ||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("Error reading image").build(); | ||
} | ||
} | ||
|
||
@POST | ||
@Path("/image") | ||
@Consumes(MediaType.MULTIPART_FORM_DATA) | ||
@Produces(MediaType.APPLICATION_OCTET_STREAM) | ||
public byte[] postFormReturnFile(@RestForm("image") @PartType("image/jpg") File image) throws IOException { | ||
return IOUtils.toByteArray(image.toURI()); | ||
} | ||
|
||
private String determineContentType(String imageName) { | ||
if (imageName.endsWith(".png")) { | ||
return "image/png"; | ||
} else if (imageName.endsWith(".jpg") || imageName.endsWith(".jpeg")) { | ||
return "image/jpeg"; | ||
} else { | ||
return "application/octet-stream"; | ||
} | ||
} | ||
|
||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.