package se.metasolutions.recruit.resources; import org.restlet.data.MediaType; import org.restlet.representation.BufferingRepresentation; import org.restlet.representation.Representation; import org.restlet.representation.StringRepresentation; import org.restlet.resource.ResourceException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.restlet.resource.Get; import org.restlet.resource.Post; import java.io.IOException; public class EchoResource extends BaseResource { private final static Logger log = LoggerFactory.getLogger(EchoResource.class); @Post() public Representation echo(Representation entity) throws IOException { return getEchoResponse(entity); } @Post("csv") public Representation tablify(Representation entity) throws IOException { Representation response; if (!getRequest().isEntityAvailable()) { response = new StringRepresentation("No POST data available"); } else { String acceptedMediaTypes = getClientInfo().getAcceptedMediaTypes().toString(); if (acceptedMediaTypes.contains("text/html")) { StringBuilder htmlTable = new StringBuilder("\n"); try { Representation data = new BufferingRepresentation(entity); String csv = data.getText(); String[] rows = csv.split("\\r?\\n"); for (String row : rows) { htmlTable.append("\n"); String[] cells = row.split(",", -1); for (String cell : cells) { htmlTable.append("\n"); } htmlTable.append("\n"); } } catch (IOException e) { htmlTable.append(""); } finally { htmlTable.append("
").append(cell).append("
Error while reading CSV data
\n"); } response = new StringRepresentation(htmlTable); response.setMediaType(MediaType.valueOf("text/html")); } else { response = getEchoResponse(entity); } } return response; } Representation getEchoResponse(Representation entity) throws IOException { String responseText = ""; try { BufferingRepresentation data = new BufferingRepresentation(entity); responseText = data.getText(); } catch (IOException e) { responseText = "Error while reading text data"; } finally { return new StringRepresentation(responseText); } } }