1
0
Fork 0

Add echo endpoint with csv to html table conversion

master
Frida Hjelm 2 years ago
parent 01af58adf0
commit 51d1eef735

@ -2,7 +2,9 @@ package se.metasolutions.recruit;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.config.Configurator;
import org.restlet.routing.TemplateRoute;
import se.metasolutions.recruit.resources.DefaultResource;
import se.metasolutions.recruit.resources.EchoResource;
import se.metasolutions.recruit.resources.StatusResource;
import org.json.JSONException;
import org.json.JSONObject;
@ -59,6 +61,7 @@ public class RestApplication extends Application {
// global scope
router.attach("/status", StatusResource.class);
router.attach("/", DefaultResource.class);
router.attach("/echo", EchoResource.class);
return router;
}

@ -0,0 +1,75 @@
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);
@Get
public Representation respond() {
return new StringRepresentation("Please POST some content to echo.");
}
@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("<table>\n");
try {
Representation data = new BufferingRepresentation(entity);
String csv = data.getText();
String[] rows = csv.split("\\r?\\n");
for (String row : rows) {
htmlTable.append("<tr>\n");
String[] cells = row.split(",", -1);
for (String cell : cells) {
htmlTable.append("<td>").append(cell).append("</td>\n");
}
htmlTable.append("</tr>\n");
}
} catch (IOException e) {
htmlTable.append("<tr><td>Error while reading CSV data</td></tr>");
} finally {
htmlTable.append("</table>\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);
}
}
}
Loading…
Cancel
Save