001: package org.purl.sword.server;
002:
003: import java.io.IOException;
004: import java.text.ParseException;
005: import java.text.SimpleDateFormat;
006: import java.util.Date;
007: import java.util.TimeZone;
008: import java.util.zip.ZipEntry;
009: import java.util.zip.ZipInputStream;
010:
011: import org.purl.sword.base.Collection;
012: import org.purl.sword.base.Deposit;
013: import org.purl.sword.base.DepositResponse;
014: import org.purl.sword.base.ErrorCodes;
015: import org.purl.sword.base.SWORDAuthenticationException;
016: import org.purl.sword.base.SWORDEntry;
017: import org.purl.sword.base.SWORDException;
018: import org.purl.sword.base.Service;
019: import org.purl.sword.base.ServiceDocument;
020: import org.purl.sword.base.ServiceDocumentRequest;
021: import org.purl.sword.base.ServiceLevel;
022: import org.purl.sword.base.Workspace;
023: import org.w3.atom.Author;
024: import org.w3.atom.Content;
025: import org.w3.atom.Contributor;
026: import org.w3.atom.Generator;
027: import org.w3.atom.InvalidMediaTypeException;
028: import org.w3.atom.Link;
029: import org.w3.atom.Source;
030: import org.w3.atom.Summary;
031: import org.w3.atom.Title;
032:
033: /**
034: * A 'dummy server' which acts as dumb repository which implements the
035: * SWORD ServerInterface. It accepts any type of deposit, and tries to
036: * return appropriate responses.
037: *
038: * It supports authentication: if the username and password match
039: * (case sensitive) it authenticates the user, if not, the authentication
040: * fails.
041: *
042: * @author Stuart Lewis
043: */
044: public class DummyServer implements SWORDServer {
045:
046: /** A counter to count submissions, so the response to a deposito can increment */
047: private static int counter = 0;
048:
049: /**
050: * Provides a dumb but plausible service document - it contains
051: * an anonymous workspace and collection, and one personalised
052: * for the onBehalfOf user.
053: *
054: * @param onBehalfOf The user that the client is acting on behalf of
055: * @throws SWORDAuthenticationException
056: */
057: public ServiceDocument doServiceDocument(ServiceDocumentRequest sdr)
058: throws SWORDAuthenticationException {
059: // Authenticate the user
060: String username = sdr.getUsername();
061: String password = sdr.getPassword();
062: if ((username != null)
063: && (password != null)
064: && (((username.equals("")) && (password.equals(""))) || (!username
065: .equalsIgnoreCase(password)))) {
066: // User not authenticated
067: throw new SWORDAuthenticationException("Bad credentials");
068: }
069:
070: // Create and return a dummy ServiceDocument
071: ServiceDocument document = new ServiceDocument();
072: Service service = new Service(ServiceLevel.ZERO, true, true);
073: document.setService(service);
074:
075: Workspace workspace = new Workspace();
076: workspace.setTitle("Anonymous submitters workspace");
077: Collection collection = new Collection();
078: collection.setTitle("Anonymous submitters collection");
079: collection
080: .setLocation("http://sword.aber.ac.uk/sword/deposit?user=anon");
081: workspace.addCollection(collection);
082: collection = new Collection();
083: collection.setTitle("Anonymous submitters other collection");
084: collection
085: .setLocation("http://sword.aber.ac.uk/sword/deposit?user=anonymous");
086: workspace.addCollection(collection);
087: service.addWorkspace(workspace);
088:
089: if (sdr.getUsername() != null) {
090: workspace = new Workspace();
091: workspace.setTitle("Authenticated workspace for "
092: + username);
093: collection = new Collection();
094: collection.setTitle("Authenticated collection for "
095: + username);
096: collection
097: .setLocation("http://sword.aber.ac.uk/sword/deposit?user="
098: + username);
099: workspace.addCollection(collection);
100: collection = new Collection();
101: collection.setTitle("Second authenticated collection for "
102: + username);
103: collection
104: .setLocation("http://sword.aber.ac.uk/sword/deposit?user="
105: + username + "-2");
106: workspace.addCollection(collection);
107: service.addWorkspace(workspace);
108: }
109:
110: String onBehalfOf = sdr.getOnBehalfOf();
111: if ((onBehalfOf != null) && (!onBehalfOf.equals(""))) {
112: workspace = new Workspace();
113: workspace.setTitle("Personal workspace for " + onBehalfOf);
114: collection = new Collection();
115: collection
116: .setTitle("Personal collection for " + onBehalfOf);
117: collection
118: .setLocation("http://sword.aber.ac.uk/sword/deposit?user="
119: + onBehalfOf);
120: collection.addAccepts("application/zip");
121: collection.addAccepts("application/xml");
122: collection.setAbstract("An abstract goes in here");
123: collection.setCollectionPolicy("A collection policy");
124: collection.setMediation(true);
125: collection.setTreatment("treatment in here too");
126: workspace.addCollection(collection);
127: service.addWorkspace(workspace);
128: }
129:
130: return document;
131: }
132:
133: public DepositResponse doDeposit(Deposit deposit)
134: throws SWORDAuthenticationException, SWORDException {
135: // Authenticate the user
136: String username = deposit.getUsername();
137: String password = deposit.getPassword();
138: if ((username != null)
139: && (password != null)
140: && (((username.equals("")) && (password.equals(""))) || (!username
141: .equalsIgnoreCase(password)))) {
142: // User not authenticated
143: throw new SWORDAuthenticationException("Bad credentials");
144: }
145:
146: // Get the filenames
147: StringBuffer filenames = new StringBuffer(
148: "Deposit file contained: ");
149: if (deposit.getFilename() != null) {
150: filenames.append("(filename = " + deposit.getFilename()
151: + ") ");
152: }
153: if (deposit.getSlug() != null) {
154: filenames.append("(slug = " + deposit.getSlug() + ") ");
155: }
156: try {
157: ZipInputStream zip = new ZipInputStream(deposit.getFile());
158: ZipEntry ze;
159: while ((ze = zip.getNextEntry()) != null) {
160: filenames.append(" " + ze.toString());
161: }
162: } catch (IOException ioe) {
163: throw new SWORDException(
164: "Failed to open deposited zip file", null,
165: ErrorCodes.ERROR_CONTENT);
166: }
167:
168: // Handle the deposit
169: if (!deposit.isNoOp()) {
170: counter++;
171: }
172: DepositResponse dr = new DepositResponse(Deposit.ACCEPTED);
173: SWORDEntry se = new SWORDEntry();
174:
175: Title t = new Title();
176: t.setContent("DummyServer Deposit: #" + counter);
177: se.setTitle(t);
178:
179: se.addCategory("Category");
180:
181: if (deposit.getSlug() != null) {
182: se.setId(deposit.getSlug() + " - ID: " + counter);
183: } else {
184: se.setId("ID: " + counter);
185: }
186:
187: SimpleDateFormat sdf = new SimpleDateFormat(
188: "yyyy-MM-dd'T'HH:mm:ss'Z'");
189: TimeZone utc = TimeZone.getTimeZone("UTC");
190: sdf.setTimeZone(utc);
191: String milliFormat = sdf.format(new Date());
192: try {
193: se.setUpdated(milliFormat);
194: } catch (ParseException e) {
195: // TODO Auto-generated catch block
196: e.printStackTrace();
197: }
198:
199: Summary s = new Summary();
200: s.setContent(filenames.toString());
201: se.setSummary(s);
202: Author a = new Author();
203: if (username != null) {
204: a.setName(username);
205: } else {
206: a.setName("unknown");
207: }
208: se.addAuthors(a);
209:
210: Link em = new Link();
211: em.setRel("edit-media");
212: em
213: .setHref("http://www.myrepository.ac.uk/sdl/workflow/my deposit");
214: se.addLink(em);
215:
216: Link e = new Link();
217: e.setRel("edit");
218: e
219: .setHref("http://www.myrepository.ac.uk/sdl/workflow/my deposit.atom");
220: se.addLink(e);
221:
222: if (deposit.getOnBehalfOf() != null) {
223: Contributor c = new Contributor();
224: c.setName(deposit.getOnBehalfOf());
225: c.setEmail(deposit.getOnBehalfOf() + "@myrepository.ac.uk");
226: se.addContributor(c);
227: }
228:
229: Source source = new Source();
230: Generator generator = new Generator();
231: generator.setContent("org.purl.sword.server.DummyServer");
232: source.setGenerator(generator);
233: se.setSource(source);
234:
235: Content content = new Content();
236: try {
237: content.setType("application/zip");
238: } catch (InvalidMediaTypeException e1) {
239: // TODO Auto-generated catch block
240: e1.printStackTrace();
241: }
242: content
243: .setSource("http://www.myrepository.ac.uk/sdl/uploads/upload-"
244: + counter + ".zip");
245: se.setContent(content);
246:
247: se.setTreatment("Short back and sides");
248:
249: if (deposit.isVerbose()) {
250: se
251: .setVerboseDescription("I've done a lot of hard work to get this far!");
252: }
253:
254: se.setNoOp(deposit.isNoOp());
255:
256: se
257: .setFormatNamespace("http://www.standards-body.com/standardXYZ/v1/");
258:
259: dr.setEntry(se);
260:
261: return dr;
262: }
263: }
|