From: SVN c. m. f. t. SWORD-A. p. <swo...@li...> - 2011-04-24 19:44:46
|
Revision: 266 http://sword-app.svn.sourceforge.net/sword-app/?rev=266&view=rev Author: richard-jones Date: 2011-04-24 19:44:40 +0000 (Sun, 24 Apr 2011) Log Message: ----------- very minor tweaks to ensure that the server is in line with Section 7: Packaging. This mostly involved adding some details about error codes, and a small amount of refactoring Modified Paths: -------------- JavaServer2.0/src/main/java/org/swordapp/server/CollectionAPI.java JavaServer2.0/src/main/java/org/swordapp/server/SWORDWorkspace.java JavaServer2.0/src/main/java/org/swordapp/server/SwordError.java Added Paths: ----------- JavaServer2.0/src/main/java/org/swordapp/server/SwordCollection.java Removed Paths: ------------- JavaServer2.0/src/main/java/org/swordapp/server/SWORDCollection.java Modified: JavaServer2.0/src/main/java/org/swordapp/server/CollectionAPI.java =================================================================== --- JavaServer2.0/src/main/java/org/swordapp/server/CollectionAPI.java 2011-04-24 17:27:47 UTC (rev 265) +++ JavaServer2.0/src/main/java/org/swordapp/server/CollectionAPI.java 2011-04-24 19:44:40 UTC (rev 266) @@ -135,7 +135,6 @@ catch (SwordError se) { this.swordError(req, resp, se); - return; } catch (SwordServerException e) { Deleted: JavaServer2.0/src/main/java/org/swordapp/server/SWORDCollection.java =================================================================== --- JavaServer2.0/src/main/java/org/swordapp/server/SWORDCollection.java 2011-04-24 17:27:47 UTC (rev 265) +++ JavaServer2.0/src/main/java/org/swordapp/server/SWORDCollection.java 2011-04-24 19:44:40 UTC (rev 266) @@ -1,319 +0,0 @@ -package org.swordapp.server; - -import org.apache.abdera.Abdera; -import org.apache.abdera.i18n.iri.IRI; -import org.apache.abdera.model.Collection; -import org.apache.abdera.model.Element; - -import javax.xml.namespace.QName; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -public class SWORDCollection -{ - private Collection collection; - private List<String> multipartAccept = new ArrayList<String>(); - private Abdera abdera; - private String collectionPolicy = null; - private boolean mediation = false; - private String treatment = null; - private List<String> acceptPackaging = new ArrayList<String>(); - private List<IRI> subServices = new ArrayList<IRI>(); - - public SWORDCollection() - { - this.abdera = new Abdera(); - this.collection = this.abdera.getFactory().newCollection(); - } - - public Collection getWrappedCollection() - { - return this.collection; - } - - public Collection getAbderaCollection() - { - Collection abderaCollection = (Collection) this.collection.clone(); - - // ensure that there is an accept field set - List<String> singlePartAccepts = this.getSinglepartAccept(); - if (singlePartAccepts.size() == 0) - { - abderaCollection.addAccepts("*/*"); - } - - // ensure that there is multipart accepts set - List<String> multipartAccepts = this.getMultipartAccept(); - if (multipartAccepts.size() == 0 || this.multipartAccept.size() == 0) - { - this.multipartAccept.add("*/*"); - } - - // add the multipart accepts as elements - for (String mpa : this.multipartAccept) - { - Element element = this.abdera.getFactory().newElement(UriRegistry.APP_ACCEPT); - element.setAttributeValue("alternate", "multipart/related"); - abderaCollection.addExtension(element); - } - - // add the collection Policy - if (this.collectionPolicy != null) - { - abderaCollection.addSimpleExtension(UriRegistry.SWORD_COLLECTION_POLICY, this.collectionPolicy); - } - - // add the mediation - abderaCollection.addSimpleExtension(UriRegistry.SWORD_MEDIATION, (this.mediation ? "true" : "false")); - - // add the treatment - if (this.treatment != null) - { - abderaCollection.addSimpleExtension(UriRegistry.SWORD_TREATMENT, this.treatment); - } - - // add the acceptPackaging - for (String ap : this.acceptPackaging) - { - abderaCollection.addSimpleExtension(UriRegistry.SWORD_ACCEPT_PACKAGING, ap); - } - - // add the sub service document IRI - for (IRI ss : this.subServices) - { - abderaCollection.addSimpleExtension(UriRegistry.SWORD_SERVICE, ss.toString()); - } - - return abderaCollection; - } - - public List<IRI> getSubServices() - { - return subServices; - } - - public void setSubServices(List<IRI> subServices) - { - this.subServices = subServices; - } - - public void addSubService(IRI subService) - { - this.subServices.add(subService); - } - - public void setCollectionPolicy(String collectionPolicy) - { - this.collectionPolicy = collectionPolicy; - } - - public void setMediation(boolean mediation) - { - this.mediation = mediation; - } - - public void setTreatment(String treatment) - { - this.treatment = treatment; - } - - public void setAcceptPackaging(List<String> acceptPackaging) - { - this.acceptPackaging = acceptPackaging; - } - - public void addAcceptPackaging(String acceptPackaging) - { - this.acceptPackaging.add(acceptPackaging); - } - - public String getCollectionPolicy() - { - return collectionPolicy; - } - - public boolean isMediation() - { - return mediation; - } - - public String getTreatment() - { - return treatment; - } - - public List<String> getAcceptPackaging() - { - return acceptPackaging; - } - - public void setTitle(String title) - { - this.collection.setTitle(title); - } - - public void setHref(String href) - { - this.collection.setHref(href); - } - - public void setAccept(String... mediaRanges) - { - this.collection.setAccept(mediaRanges); - } - - public void setAcceptsEntry() - { - this.collection.setAcceptsEntry(); - } - - public void setAcceptsNothing() - { - this.collection.setAcceptsNothing(); - } - - public void addAccepts(String mediaRange) - { - this.collection.addAccepts(mediaRange); - } - - public void addAccepts(String... mediaRanges) - { - this.collection.addAccepts(mediaRanges); - } - - public void addAcceptsEntry() - { - this.collection.addAcceptsEntry(); - } - - public void setMultipartAccept(String... mediaRanges) - { - List<String> mrs = Arrays.asList(mediaRanges); - this.multipartAccept.clear(); - this.multipartAccept.addAll(mrs); - } - - public void addMultipartAccepts(String mediaRange) - { - this.multipartAccept.add(mediaRange); - } - - public void addMultipartAccepts(String... mediaRanges) - { - List<String> mrs = Arrays.asList(mediaRanges); - this.multipartAccept.addAll(mrs); - } - - public List<String> getMultipartAccept() - { - List<String> accepts = new ArrayList<String>(); - List<Element> elements = this.collection.getElements(); - boolean noAccept = false; - for (Element e : elements) - { - String multipartRelated = e.getAttributeValue("alternate"); - QName qn = e.getQName(); - if (qn.getLocalPart().equals("accept") && - qn.getNamespaceURI().equals(UriRegistry.APP_NAMESPACE) && - "multipart-related".equals(multipartRelated)) - { - String content = e.getText(); - if (content == null || "".equals(content)) - { - noAccept = true; - } - if (content != null && !"".equals(content) && !accepts.contains(content)) - { - accepts.add(content); - } - } - } - - // if there are no accept values, and noAccept has not been triggered, then we add the - // default accept type - if (accepts.size() == 0 && !noAccept) - { - accepts.add("application/atom+xml;type=entry"); - } - - // rationalise and return - return this.rationaliseAccepts(accepts); - } - - public List<String> getSinglepartAccept() - { - List<String> accepts = new ArrayList<String>(); - List<Element> elements = this.collection.getElements(); - boolean noAccept = false; - for (Element e : elements) - { - String multipartRelated = e.getAttributeValue("alternate"); - QName qn = e.getQName(); - if (qn.getLocalPart().equals("accept") && - qn.getNamespaceURI().equals(UriRegistry.APP_NAMESPACE) && - !"multipart-related".equals(multipartRelated)) - { - String content = e.getText(); - if (content == null || "".equals(content)) - { - noAccept = true; - } - if (content != null && !"".equals(content) && !accepts.contains(content)) - { - accepts.add(content); - } - } - } - - // if there are no accept values, and noAccept has not been triggered, then we add the - // default accept type - if (accepts.size() == 0 && !noAccept) - { - accepts.add("application/atom+xml;type=entry"); - } - - // rationalise and return - return this.rationaliseAccepts(accepts); - } - - private List<String> rationaliseAccepts(List<String> accepts) - { - List<String> rational = new ArrayList<String>(); - - // first, if "*/*" is there, then we accept anything - if (accepts.contains("*/*")) - { - rational.add("*/*"); - return rational; - } - - // now look to see if we have <x>/* and if so eliminate the unnecessary accepts - List<String> wildcards = new ArrayList<String>(); - for (String a : accepts) - { - if (a.contains("/*")) - { - String wild = a.substring(0, a.indexOf("/")); - wildcards.add(wild); - if (!rational.contains(a)) - { - rational.add(a); - } - } - } - - for (String a : accepts) - { - String type = a.substring(0, a.indexOf("/")); - if (!wildcards.contains(type)) - { - rational.add(a); - } - } - - // by the time we get here we will have only unique and correctly wildcarded accept fields - return rational; - } -} Modified: JavaServer2.0/src/main/java/org/swordapp/server/SWORDWorkspace.java =================================================================== --- JavaServer2.0/src/main/java/org/swordapp/server/SWORDWorkspace.java 2011-04-24 17:27:47 UTC (rev 265) +++ JavaServer2.0/src/main/java/org/swordapp/server/SWORDWorkspace.java 2011-04-24 19:44:40 UTC (rev 266) @@ -4,10 +4,6 @@ import org.apache.abdera.model.Collection; import org.apache.abdera.model.Workspace; -import javax.activation.MimeType; -import java.util.ArrayList; -import java.util.List; - public class SWORDWorkspace { private Workspace workspace; @@ -29,7 +25,7 @@ return workspace; } - public void addCollection(SWORDCollection collection) + public void addCollection(SwordCollection collection) { // FIXME: or should collections be managed internally until getAbderaWorkspace is called Collection abderaCollection = collection.getAbderaCollection(); Copied: JavaServer2.0/src/main/java/org/swordapp/server/SwordCollection.java (from rev 253, JavaServer2.0/src/main/java/org/swordapp/server/SWORDCollection.java) =================================================================== --- JavaServer2.0/src/main/java/org/swordapp/server/SwordCollection.java (rev 0) +++ JavaServer2.0/src/main/java/org/swordapp/server/SwordCollection.java 2011-04-24 19:44:40 UTC (rev 266) @@ -0,0 +1,319 @@ +package org.swordapp.server; + +import org.apache.abdera.Abdera; +import org.apache.abdera.i18n.iri.IRI; +import org.apache.abdera.model.Collection; +import org.apache.abdera.model.Element; + +import javax.xml.namespace.QName; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class SwordCollection +{ + private Collection collection; + private List<String> multipartAccept = new ArrayList<String>(); + private Abdera abdera; + private String collectionPolicy = null; + private boolean mediation = false; + private String treatment = null; + private List<String> acceptPackaging = new ArrayList<String>(); + private List<IRI> subServices = new ArrayList<IRI>(); + + public SwordCollection() + { + this.abdera = new Abdera(); + this.collection = this.abdera.getFactory().newCollection(); + } + + public Collection getWrappedCollection() + { + return this.collection; + } + + public Collection getAbderaCollection() + { + Collection abderaCollection = (Collection) this.collection.clone(); + + // ensure that there is an accept field set + List<String> singlePartAccepts = this.getSinglepartAccept(); + if (singlePartAccepts.size() == 0) + { + abderaCollection.addAccepts("*/*"); + } + + // ensure that there is multipart accepts set + List<String> multipartAccepts = this.getMultipartAccept(); + if (multipartAccepts.size() == 0 || this.multipartAccept.size() == 0) + { + this.multipartAccept.add("*/*"); + } + + // add the multipart accepts as elements + for (String mpa : this.multipartAccept) + { + Element element = this.abdera.getFactory().newElement(UriRegistry.APP_ACCEPT); + element.setAttributeValue("alternate", "multipart/related"); + abderaCollection.addExtension(element); + } + + // add the collection Policy + if (this.collectionPolicy != null) + { + abderaCollection.addSimpleExtension(UriRegistry.SWORD_COLLECTION_POLICY, this.collectionPolicy); + } + + // add the mediation + abderaCollection.addSimpleExtension(UriRegistry.SWORD_MEDIATION, (this.mediation ? "true" : "false")); + + // add the treatment + if (this.treatment != null) + { + abderaCollection.addSimpleExtension(UriRegistry.SWORD_TREATMENT, this.treatment); + } + + // add the acceptPackaging + for (String ap : this.acceptPackaging) + { + abderaCollection.addSimpleExtension(UriRegistry.SWORD_ACCEPT_PACKAGING, ap); + } + + // add the sub service document IRI + for (IRI ss : this.subServices) + { + abderaCollection.addSimpleExtension(UriRegistry.SWORD_SERVICE, ss.toString()); + } + + return abderaCollection; + } + + public List<IRI> getSubServices() + { + return subServices; + } + + public void setSubServices(List<IRI> subServices) + { + this.subServices = subServices; + } + + public void addSubService(IRI subService) + { + this.subServices.add(subService); + } + + public void setCollectionPolicy(String collectionPolicy) + { + this.collectionPolicy = collectionPolicy; + } + + public void setMediation(boolean mediation) + { + this.mediation = mediation; + } + + public void setTreatment(String treatment) + { + this.treatment = treatment; + } + + public void setAcceptPackaging(List<String> acceptPackaging) + { + this.acceptPackaging = acceptPackaging; + } + + public void addAcceptPackaging(String acceptPackaging) + { + this.acceptPackaging.add(acceptPackaging); + } + + public String getCollectionPolicy() + { + return collectionPolicy; + } + + public boolean isMediation() + { + return mediation; + } + + public String getTreatment() + { + return treatment; + } + + public List<String> getAcceptPackaging() + { + return acceptPackaging; + } + + public void setTitle(String title) + { + this.collection.setTitle(title); + } + + public void setHref(String href) + { + this.collection.setHref(href); + } + + public void setAccept(String... mediaRanges) + { + this.collection.setAccept(mediaRanges); + } + + public void setAcceptsEntry() + { + this.collection.setAcceptsEntry(); + } + + public void setAcceptsNothing() + { + this.collection.setAcceptsNothing(); + } + + public void addAccepts(String mediaRange) + { + this.collection.addAccepts(mediaRange); + } + + public void addAccepts(String... mediaRanges) + { + this.collection.addAccepts(mediaRanges); + } + + public void addAcceptsEntry() + { + this.collection.addAcceptsEntry(); + } + + public void setMultipartAccept(String... mediaRanges) + { + List<String> mrs = Arrays.asList(mediaRanges); + this.multipartAccept.clear(); + this.multipartAccept.addAll(mrs); + } + + public void addMultipartAccepts(String mediaRange) + { + this.multipartAccept.add(mediaRange); + } + + public void addMultipartAccepts(String... mediaRanges) + { + List<String> mrs = Arrays.asList(mediaRanges); + this.multipartAccept.addAll(mrs); + } + + public List<String> getMultipartAccept() + { + List<String> accepts = new ArrayList<String>(); + List<Element> elements = this.collection.getElements(); + boolean noAccept = false; + for (Element e : elements) + { + String multipartRelated = e.getAttributeValue("alternate"); + QName qn = e.getQName(); + if (qn.getLocalPart().equals("accept") && + qn.getNamespaceURI().equals(UriRegistry.APP_NAMESPACE) && + "multipart-related".equals(multipartRelated)) + { + String content = e.getText(); + if (content == null || "".equals(content)) + { + noAccept = true; + } + if (content != null && !"".equals(content) && !accepts.contains(content)) + { + accepts.add(content); + } + } + } + + // if there are no accept values, and noAccept has not been triggered, then we add the + // default accept type + if (accepts.size() == 0 && !noAccept) + { + accepts.add("application/atom+xml;type=entry"); + } + + // rationalise and return + return this.rationaliseAccepts(accepts); + } + + public List<String> getSinglepartAccept() + { + List<String> accepts = new ArrayList<String>(); + List<Element> elements = this.collection.getElements(); + boolean noAccept = false; + for (Element e : elements) + { + String multipartRelated = e.getAttributeValue("alternate"); + QName qn = e.getQName(); + if (qn.getLocalPart().equals("accept") && + qn.getNamespaceURI().equals(UriRegistry.APP_NAMESPACE) && + !"multipart-related".equals(multipartRelated)) + { + String content = e.getText(); + if (content == null || "".equals(content)) + { + noAccept = true; + } + if (content != null && !"".equals(content) && !accepts.contains(content)) + { + accepts.add(content); + } + } + } + + // if there are no accept values, and noAccept has not been triggered, then we add the + // default accept type + if (accepts.size() == 0 && !noAccept) + { + accepts.add("application/atom+xml;type=entry"); + } + + // rationalise and return + return this.rationaliseAccepts(accepts); + } + + private List<String> rationaliseAccepts(List<String> accepts) + { + List<String> rational = new ArrayList<String>(); + + // first, if "*/*" is there, then we accept anything + if (accepts.contains("*/*")) + { + rational.add("*/*"); + return rational; + } + + // now look to see if we have <x>/* and if so eliminate the unnecessary accepts + List<String> wildcards = new ArrayList<String>(); + for (String a : accepts) + { + if (a.contains("/*")) + { + String wild = a.substring(0, a.indexOf("/")); + wildcards.add(wild); + if (!rational.contains(a)) + { + rational.add(a); + } + } + } + + for (String a : accepts) + { + String type = a.substring(0, a.indexOf("/")); + if (!wildcards.contains(type)) + { + rational.add(a); + } + } + + // by the time we get here we will have only unique and correctly wildcarded accept fields + return rational; + } +} Modified: JavaServer2.0/src/main/java/org/swordapp/server/SwordError.java =================================================================== --- JavaServer2.0/src/main/java/org/swordapp/server/SwordError.java 2011-04-24 17:27:47 UTC (rev 265) +++ JavaServer2.0/src/main/java/org/swordapp/server/SwordError.java 2011-04-24 19:44:40 UTC (rev 266) @@ -1,23 +1,40 @@ package org.swordapp.server; +import java.util.HashMap; +import java.util.Map; + public class SwordError extends Exception { private String errorUri; + private Map<String, Integer> errorCodes = new HashMap<String, Integer>(); public SwordError() { super(); + this.init(); } public SwordError(String errorUri) { super(errorUri); this.errorUri = errorUri; + this.init(); } public SwordError(String errorUri, String message) { super(message); this.errorUri = errorUri; + this.init(); } + + public void init() + { + this.errorCodes.put(UriRegistry.ERROR_BAD_REQUEST, 400); // bad request + this.errorCodes.put(UriRegistry.ERROR_CHECKSUM_MISMATCH, 412); // precondition failed + this.errorCodes.put(UriRegistry.ERROR_CONTENT, 415); // unsupported media type + this.errorCodes.put(UriRegistry.ERROR_MEDIATION_NOT_ALLOWED, 412); // precondition failed + this.errorCodes.put(UriRegistry.ERROR_METHOD_NOT_ALLOWED, 405); // method not allowed + this.errorCodes.put(UriRegistry.ERROR_TARGET_OWNER_UNKNOWN, 403); // forbidden + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |