Skip to content

Commit cef06d2

Browse files
author
richard-jones
committed
Error handling improvements
* add default title to error documents * support configurable alternate urls to error documents * max upload size now expects bytes not kilobytes git-svn-id: http://sword-app.svn.sourceforge.net/svnroot/sword-app/JavaServer2.0/trunk@506 2bf6ea0f-123d-0410-b71a-f1a21eb24612
1 parent c30eab1 commit cef06d2

5 files changed

Lines changed: 32 additions & 6 deletions

File tree

src/main/java/org/swordapp/server/ErrorDocument.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ public void writeTo(Writer out, SwordConfiguration config)
8585

8686
// write some boiler-plate text into the document
8787
Element title = new Element("atom:title", UriRegistry.ATOM_NAMESPACE);
88+
title.appendChild("ERROR");
8889
Element updates = new Element("atom:updated", UriRegistry.ATOM_NAMESPACE);
8990
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
9091
updates.appendChild(sdf.format(new Date()));
@@ -118,6 +119,20 @@ public void writeTo(Writer out, SwordConfiguration config)
118119
error.appendChild(vd);
119120
}
120121

122+
String alternate = config.getAlternateUrl();
123+
String altContentType = config.getAlternateUrlContentType();
124+
if (alternate != null && !"".equals(alternate))
125+
{
126+
Element altLink = new Element("atom:link", UriRegistry.ATOM_NAMESPACE);
127+
altLink.addAttribute(new Attribute("rel", "alternate"));
128+
if (altContentType != null && !"".equals(altContentType))
129+
{
130+
altLink.addAttribute(new Attribute("type", altContentType));
131+
}
132+
altLink.addAttribute(new Attribute("href", alternate));
133+
error.appendChild(altLink);
134+
}
135+
121136
try
122137
{
123138
// now get it written out

src/main/java/org/swordapp/server/SwordAPIEndpoint.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,12 +164,12 @@ protected void storeAndCheckBinary(Deposit deposit, SwordConfiguration config)
164164
// Set the file to be deposited
165165
deposit.setFile(file);
166166

167-
long fLength = file.length() / 1024; // in kilobytes
167+
long fLength = file.length(); // in bytes
168168
if ((config.getMaxUploadSize() != -1) && (fLength > config.getMaxUploadSize()))
169169
{
170170
String msg = "The uploaded file exceeded the maximum file size this server will accept (the file is " +
171-
fLength + "kB but the server will only accept files as large as " +
172-
config.getMaxUploadSize() + "kB)";
171+
fLength + " bytes but the server will only accept files as large as " +
172+
config.getMaxUploadSize() + " bytes)";
173173
throw new SwordError(UriRegistry.ERROR_MAX_UPLOAD_SIZE_EXCEEDED, msg);
174174
}
175175

src/main/java/org/swordapp/server/SwordConfiguration.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,8 @@ public interface SwordConfiguration
2121
String getTempDirectory();
2222

2323
int getMaxUploadSize();
24+
25+
String getAlternateUrl();
26+
27+
String getAlternateUrlContentType();
2428
}

src/main/java/org/swordapp/server/SwordConfigurationDefault.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,14 @@ public int getMaxUploadSize()
5151
{
5252
return -1;
5353
}
54+
55+
public String getAlternateUrl()
56+
{
57+
return null;
58+
}
59+
60+
public String getAlternateUrlContentType()
61+
{
62+
return null;
63+
}
5464
}

src/main/java/org/swordapp/server/SwordError.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package org.swordapp.server;
22

3-
import java.util.HashMap;
4-
import java.util.Map;
5-
63
public class SwordError extends Exception
74
{
85
private String errorUri;

0 commit comments

Comments
 (0)