Code with Finding: |
class NegotiatedFrame {
protected ResourceReference negotiatePut(Request request)
throws ProtocolException, ResourceException
{
// Check for zero or one variant:
ResourceReference variants[] = getVariantResources() ;
HTTPFrame itsframe;
int nb_v;
// zero, don't PUT on a negotiable resource!
if (variants == null || variants.length == 0) {
try {
getResource().delete();
} catch (MultipleLockException ex) {
//will be deleted later...
} finally {
Reply reply = request.makeReply(HTTP.NOT_FOUND);
reply.setContent ("<h1>Document not found</h1>"+
"<p>The document "+request.getURL()+
" has no acceptable variants "+
"(probably deleted).");
throw new HTTPException (reply);
}
}
// negotiate etag
HttpEntityTag etag = request.getETag();
HttpEntityTag etags[] = request.getIfMatch();
// gather the etags
if (etags == null && etag != null) {
etags = new HttpEntityTag[1];
etags[0] = etag;
} else if (etag != null) {
HttpEntityTag t_etags[] = new HttpEntityTag[etags.length+1];
System.arraycopy(etags, 0, t_etags, 0, etags.length);
t_etags[etags.length] = etag;
etags = t_etags;
}
if (etags != null) {
// yeah go for it!
FramedResource resource;
HttpEntityTag frametag;
for (int i = 0 ; i < variants.length ; i++) {
try {
resource = (FramedResource)variants[i].lock() ;
itsframe = (HTTPFrame)resource.getFrame(httpFrameClass);
if (itsframe != null) {
frametag = itsframe.getETag();
// Do we have a winner?
for (int j=0; j<etags.length; j++)
if (frametag.getTag().equals(etags[j].getTag()))
return variants[i];
}
} catch (InvalidResourceException ex) {
//FIXME
} finally {
variants[i].unlock();
}
}
// no matching variants...
Reply reply = request.makeReply(HTTP.NOT_FOUND);
reply.setContent ("<h1>Document not found</h1>"+
"<p>The document "+request.getURL()+
" has no acceptable variants "+
"according to the ETag sent");
throw new HTTPException (reply);
}
// if we are strict, don't go any further, etags
// is the mandatory thing, otherwise PUT on the direct version
if (getPutPolicy()) {
Reply reply = request.makeReply(HTTP.NOT_FOUND);
reply.setContent ("<h1>Document not found</h1>"+
"<p>The document "+request.getURL()+
" has no acceptable variants "+
" for a PUT, as no ETags were sent");
throw new HTTPException (reply);
}
// now filter out variants
nb_v = variants.length;
MimeType type = request.getContentType();
String encodings[] = request.getContentEncoding();
String languages[] = request.getContentLanguage();
ResourceReference rr;
if (type != null || encodings != null || languages != null) {
// the request is not too bad ;)
for (int i = 0 ; i < variants.length ; i++) {
if (variants[i] == null)
continue;
rr = variants[i];
try {
resource = (FramedResource)rr.lock() ;
itsframe = (HTTPFrame)resource.getFrame(httpFrameClass);
if (itsframe == null) {
nb_v--;
variants[i] = null;
continue;
}
// remove the non matching mime types
if (type != null) {
MimeType fmt = itsframe.getContentType();
if (fmt == null || (fmt.match(type) !=
MimeType.MATCH_SPECIFIC_SUBTYPE)) {
nb_v--;
variants[i] = null;
continue;
}
}
// remove the non matching languages
if (languages != null) {
String language = itsframe.getContentLanguage();
nb_v--;
variants[i] = null;
if (language == null) {
continue;
}
for (int j=0; j<languages.length; j++) {
if (language.equals(languages[j])) {
nb_v++;
variants[i] = rr;
break;
}
}
}
// remove the non matching encodings
if (encodings != null) {
String encoding = itsframe.getContentEncoding();
nb_v--;
variants[i] = null;
if (encoding == null) {
continue;
}
for (int j=0; j<encodings.length; j++) {
if (encoding.equals(languages[j])) {
nb_v++;
variants[i] = rr;
break;
}
}
}
} catch (InvalidResourceException ex) {
//FIXME
} finally {
rr.unlock();
}
}
// a winner!
if (nb_v == 1) {
for (int i=0; i< variants.length; i++) {
if (variants[i] != null)
return variants[i];
}
}
// no document matching
if (nb_v <= 0 ) {
Reply reply = request.makeReply(HTTP.NOT_FOUND);
reply.setContent ("<h1>Document not found</h1>"+
"<p>The document "+request.getURL()+
" has no acceptable variants "+
" for a PUT");
throw new HTTPException (reply);
}
}
// now we have multiple choice :(
String name;
Reply reply = request.makeReply(HTTP.MULTIPLE_CHOICE) ;
HtmlGenerator g = new HtmlGenerator ("Multiple choice for "+
resource.getIdentifier()) ;
g.append ("<ul>") ;
for (int i = 0 ; i < variants.length ; i++) {
if (variants[i] != null) {
try {
name = variants[i].lock().getIdentifier();
g.append ("<li>"
+ "<a href=\"" + name + "\">" +name+ "</a>");
} catch (InvalidResourceException ex) {
//FIXME (this should NOT happen :) )
} finally {
variants[i].unlock();
}
}
}
reply.setStream (g) ;
reply.setHeaderValue(reply.H_VARY, getVary());
throw new HTTPException (reply) ;
}
}
|