req.getPath() + (req.getQueryString() == null ? "" : "?" + req.getQueryString()));
switch (req.getMethod()) {
case PUT:
try {
final RawBinary created = binaryStore.create(binaryPath, req.getContentType(), req.getInputStream());
if (created != null) {
return Responses.created();
}
else {
return new StatusResponse(409,
"Conflict: binary already exists. To replace it, DELETE it first.");
}
}
catch (IOException e) {
Throwables.propagate(e);
}
case DELETE:
if (binaryStore.delete(binaryPath)) {
return Responses.deleted();
}
else {
return Responses.notFound("No artifact to be deleted at that location.");
}
case GET:
final List<RawBinary> binaries = binaryStore.getForPath(binaryPath);
// Do we have a single result whose path matches the request exactly?
if (isSuccesfulRequestForSingleArtifact(binaryPath, binaries)) {
final RawBinary binary = binaries.get(0);
return Responses
.streamResponse(binary.getInputStream(), binary.getContentType(), binary.getModifiedDate().toDate());
}
StringBuilder html = new StringBuilder();
html.append("<html><body>");
// missing HTML encoding
html.append("<h1>").append(HtmlEscapers.htmlEscaper().escape(binaryPath)).append("</h1>");
html.append("<ul>");
for (RawBinary binary : binaries) {
html.append(
new Formatter()
.format("<li><a href=\"%s\">%s</a></li>", stripLeadingSlash(binary.getPath()), binary.getPath()));
}
html.append("</ul>");
html.append("</body></html>");
return Responses.html(html.toString());