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.
Frida Hjelm
aa2394bf66
|
3 years ago | |
---|---|---|
standalone | 3 years ago | |
webapp | 3 years ago | |
.gitignore | 3 years ago | |
README.md | 3 years ago | |
build.sh | 3 years ago | |
pom.xml | 3 years ago |
README.md
Solution to the Java exercise for the backend recruiting process
Running the application
- Make sure you have a working Java development environment. The Maven configuration of the boilerplate requires at least Java 11.
- Clone this repository and don't fork as your code would be visible to other candidates in case you choose to push your implementation to a public repository.
- Compile by executing
./build.sh
. - After successful compilation execute
./standalone/jetty/target/dist/bin/rest
. It launches a REST API and listens on port 8282. - Test the API by fetching data from the status resource by executing
curl http://localhost:8282/status
. - If any of the above fails you need to revisit your development environment or talk to hannes@metasolutions.se (it may be a bug).
Exercise
- Implement a new resource
/echo
that responds with the request body when posting (HTTP POST) to it; it should simply mirror the request back as response. - Add content negotiation to the echo resource and convert the payload: if the request is made with MIME type
text/csv
(i.e.,Content-Type: text/csv
), and the response is expected to be delivered intext/html
(i.e.,Accept: text/html
), then convert from CSV to a simple HTML table. Very basic handling of the CSV payload (use the snippet from the next section) is sufficient, you don't need to use third party libraries to handle advanced CSVs with apostrophes, line breaks, etc.. Only requests withContent-Type: text/csv
andAccept: text/html
need to trigger a conversion, all other requests should behave as described in 1. above.
Bonus for the Linked Data experienced:
- Add support for the output format Turtle (the RDF Turtle serialization format) to the echo resource (triggered by
Accept: text/turtle
). Use appropriate properties from the Dublin Core Terms metadata specification to map the CSV data to Turtle.
CSV snippet
title,description,created
Important document,This is an important document,2022-03-31
Less important document,,2022-03-31
Last document,,
Questions
If anything is unclear or if you get stuck somewhere in the exercise, do not hesitate to contact Hannes for advice.
Result
Document your implementation in Text or Markdown format and send it along with your code as a tar.gz archive to Hannes. You can also send a link to a Git repository.
Solution
The /echo
endpoint will convert any data posted with Content-Type: text/csv
and Accept: text/html
to a simple html table.
All other data will be echoed back unchanged. Empty datasets will return an explanatory message.
RestApplication.java
- New router added for path
/echo
- New router added for path
resources/EchoResource.java
POST
requests are handled depending onContent-Type
. CSV-data is handled by thetablify
method, all others by theecho
method- The
tablify
method:- Checks for
Accept: text/html
to determine if table conversion should be performed. Requests that do not match are treated like otherContent-Type
s. - Assumes the CSV format in the example.
- Returns an explanatory string if POST data is missing or cannot be parsed.
- Checks for
Example use
$ cat test.csv
title,description,created
Important document,This is an important document,2022-03-31
Less important document,,2022-03-31
Last document,,
$ curl -H 'Content-Type: text/csv' -H 'Accept: text/html' -X POST --data-binary @test.csv http://localhost:8282/echo
<table>
<tr>
<td>title</td>
<td>description</td>
<td>created</td>
</tr>
<tr>
<td>Important document</td>
<td>This is an important document</td>
<td>2022-03-31</td>
</tr>
<tr>
<td>Less important document</td>
<td></td>
<td>2022-03-31</td>
</tr>
<tr>
<td>Last document</td>
<td></td>
<td></td>
</tr>
</table>
$ curl -X POST -d "Example string" http://localhost:8282/echo
Example string