Compare commits
3 Commits
01af58adf0
...
master
Author | SHA1 | Date |
---|---|---|
Frida Hjelm | aa2394bf66 | 3 years ago |
Frida Hjelm | 64be5911f5 | 3 years ago |
Frida Hjelm | 51d1eef735 | 3 years ago |
@ -0,0 +1,70 @@
|
||||
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("<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…
Reference in new issue