-
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.
- Loading branch information
1 parent
34c49e0
commit b4bf296
Showing
9 changed files
with
428 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() { | ||
} | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
...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,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; | ||
} | ||
} |
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); | ||
} | ||
} | ||
} |
119 changes: 119 additions & 0 deletions
119
...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,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; | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.