Skip to content

Commit

Permalink
Fixes #3: Returns HTTP 500 when response caching failes
Browse files Browse the repository at this point in the history
  • Loading branch information
claussni committed Sep 14, 2014
1 parent b1722a2 commit 696ad57
Showing 1 changed file with 25 additions and 21 deletions.
46 changes: 25 additions & 21 deletions src/main/org/purl/sword/server/fedora/FedoraServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@

import javax.xml.rpc.ServiceException;

import java.io.*;
import java.rmi.RemoteException;

import org.apache.axis.client.Stub;
Expand All @@ -88,11 +89,6 @@
import org.purl.sword.server.fedora.api.FedoraAPIA;
import org.purl.sword.server.fedora.api.RepositoryInfo;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.File;

import java.util.Iterator;

import org.apache.log4j.Level;
Expand Down Expand Up @@ -247,23 +243,10 @@ public DepositResponse doDeposit(Deposit pDeposit) throws SWORDAuthenticationExc
}
}
tResponse.setLocation(tLink.getHref());
// and save response for further gets
File tCollectionDir = new File(_props.getEntryStoreLocation(), tCollectionPID.replaceAll(":", "_"));
if (!tCollectionDir.exists()) {
tCollectionDir.mkdir();
}
FileOutputStream tStream = new FileOutputStream(new File(tCollectionDir, tEntry.getId().replaceAll(":", "_") + ".xml"));
Serializer tSerializer = new Serializer(tStream, "UTF-8");
tSerializer.setIndent(3);

Document tDoc = new Document(tEntry.marshall());
tSerializer.write(tDoc);
// and save response for further gets, but don't crash since the everything went fine so far
cacheResponse(tCollectionPID, tEntry);

return tResponse;
} catch (IOException tIOExcpt) {
tIOExcpt.printStackTrace();
LOG.error("Exception occured: " + tIOExcpt);
throw new SWORDException(tIOExcpt.getMessage());
} catch (SWORDException tException) {
tException.printStackTrace();
LOG.error("Exception occured: " + tException);
Expand All @@ -279,7 +262,28 @@ public DepositResponse doDeposit(Deposit pDeposit) throws SWORDAuthenticationExc
}
}

/**
private void cacheResponse(String tCollectionPID, SWORDEntry tEntry) throws SWORDException {
File tCollectionDir = new File(_props.getEntryStoreLocation(), tCollectionPID.replaceAll(":", "_"));
if (!tCollectionDir.exists()) {
if (tCollectionDir.mkdirs()) {
} else {
LOG.warn("Cannot create directory: " + tCollectionDir.toString());
}
}
FileOutputStream tStream = null;
try {
tStream = new FileOutputStream(
new File(tCollectionDir, tEntry.getId().replaceAll(":", "_") + ".xml"));
Serializer tSerializer = new Serializer(tStream, "UTF-8");
tSerializer.setIndent(3);
Document tDoc = new Document(tEntry.marshall());
tSerializer.write(tDoc);
} catch (IOException e) {
LOG.error("Error while caching response: " + e.getMessage());
}
}

/**
* Answer a request for an entry document
*
* @param adr The Atom Document Request object
Expand Down

0 comments on commit 696ad57

Please sign in to comment.