Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix jobdir PUT #283

Merged
merged 2 commits into from
Aug 28, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
package org.archive.crawler.restlet;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;
import java.util.LinkedList;
Expand Down Expand Up @@ -50,7 +52,11 @@ public class EnhDirectoryResource extends DirectoryServerResource {
*/
@Override
public List<Variant> getVariants() {
List<Variant> variants = new LinkedList<>(super.getVariants(Method.GET));
List<Variant> superVariants = super.getVariants();
if (superVariants == null) {
return null; // PUT and DELETE return no content
}
List<Variant> variants = new LinkedList<>(superVariants);
Form f = getRequest().getResourceRef().getQueryAsForm();
String format = f.getFirstValue("format");
if("textedit".equals(format)) {
Expand All @@ -64,7 +70,11 @@ public List<Variant> getVariants() {
} catch (Exception e) {
throw new RuntimeException(e);
}
variants = new LinkedList<>(super.getVariants(Method.GET));
superVariants = super.getVariants();
if (superVariants == null) {
return null;
}
variants = new LinkedList<>(superVariants);
}
// wrap FileRepresentations in EditRepresentations
ListIterator<Variant> iter = variants.listIterator();
Expand Down Expand Up @@ -145,4 +155,28 @@ protected Representation post(Representation entity, Variant variant) throws Res
getResponse().redirectSeeOther(ref);
return new EmptyRepresentation();
}

/*
* XXX: We override Restlet's default PUT behaviour (see FileClientHelper.handleFilePut) as it unhelpfully changes
* the file extension based on the content-type and there's no apparent way to disable that.
*/
@Override
public Representation put(Representation entity) throws ResourceException {
File file = new File(URI.create(getTargetUri()));
if (getTargetUri().endsWith("/") || file.isDirectory()) {
return super.put(entity);
}
boolean created = !file.exists();
try (FileOutputStream out = new FileOutputStream(file)) {
entity.write(out);
} catch (FileNotFoundException e) {
throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND, e);
} catch (IOException e) {
throw new ResourceException(Status.SERVER_ERROR_INTERNAL, e);
}
if (created) {
getResponse().setStatus(Status.SUCCESS_CREATED);
}
return new EmptyRepresentation();
}
}