Skip to content

Commit

Permalink
Merge pull request #283 from internetarchive/jobdir-put-fix
Browse files Browse the repository at this point in the history
Fix jobdir PUT
  • Loading branch information
nlevitt committed Aug 28, 2019
2 parents 2eafb5a + eeddfd7 commit feea47b
Showing 1 changed file with 36 additions and 2 deletions.
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();
}
}

0 comments on commit feea47b

Please sign in to comment.