You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
71 lines
2.7 KiB
71 lines
2.7 KiB
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);
|
|
}
|
|
}
|
|
|
|
}
|