Skip to content

Commit

Permalink
non-json-payload tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jcarranzan committed Jan 17, 2024
1 parent 34c49e0 commit b4bf296
Show file tree
Hide file tree
Showing 9 changed files with 428 additions and 0 deletions.
4 changes: 4 additions & 0 deletions http/http-advanced-reactive/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-reactive-routes</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-reactive-jaxb</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-grpc</artifactId>
Expand Down
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() {
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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;
}
}
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;
}
}
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);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package io.quarkus.ts.http.advanced.reactive;

import static io.quarkus.ts.http.advanced.reactive.MediaTypeResource.APPLICATION_YAML;

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.Produces;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;

import org.eclipse.microprofile.openapi.annotations.parameters.RequestBody;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;

@Path("/city")
public class CityResource {

private static final String YAML_FILE_PATH = "payload.yaml";

private final Logger logger = LoggerFactory.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() {
logger.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(String xml) {

logger.info("Received XML payload: {}", xml);

CityListDTO cityListDTO = serializer.fromXML(xml);

if (cityListDTO == null) {
logger.error("Error deserializing XML");
return Response.status(Response.Status.BAD_REQUEST)
.entity("<error>Invalid XML payload</error>")
.build().toString();
}

List<City> cities = cityListDTO.getCityList();

for (City city : cities) {
logger.info("Saving city {}", city);
}

String responseXML = serializer.toXML(cityListDTO);

return responseXML;
}

@POST
@Path("/cityYaml")
@Produces(MediaType.APPLICATION_JSON)
@Consumes("application/x-yaml")
public Object handleYamlPostRequest(@RequestBody final String yamlPayload) throws IOException {

logger.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 {
String yamlContent = readYamlFile(YAML_FILE_PATH);
return Response.ok(yamlContent).type(APPLICATION_YAML).build();
}

private String 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 {
return new String(Files.readAllBytes(path));
} catch (IOException e) {
logger.error("Error reading YAML file: {}", yamlFilePath, e);
return null;
}
}

}
Loading

0 comments on commit b4bf296

Please sign in to comment.