System.arraycopy(segments,0,path,0,segments.length-2);
UUID entryId = UUID.fromString(segments[last]);
String feedPath = App.join(path,0,path.length,'/')+'/';
try {
Feed f = app.getFeed(feedPath);
if (f!=null) {
try {
Entry entry = f.findEntry(entryId);
if (entry!=null) {
EntryResource r = new EntryResource(theApp,f,entry,storage);
return r;
}
} catch (SQLException ex) {
getContext().getLogger().log(Level.SEVERE,"Failed to retrieve entry at "+entryId+" from the database.",ex);
return new ErrorResource(theApp.getStatusService().getRepresentation(Status.SERVER_ERROR_INTERNAL,request,response));
}
}
return null;
} catch (AppException ex) {
if (ex.getStatus().getCode()==Status.CLIENT_ERROR_NOT_FOUND.getCode()) {
return null;
} else {
getContext().getLogger().log(Level.SEVERE,"Failed to retrieve feed at "+feedPath+" from the database.",ex);
return new ErrorResource(theApp.getStatusService().getRepresentation(Status.SERVER_ERROR_INTERNAL,request,response));
}
}
} else {
try {
Feed f = app.getFeed(uriPath);
if (uriPath.length()>0 && !uriPath.endsWith("/")) {
uriPath += "/";
}
Reference feedResourceRef = new Reference(request.getRootRef().toString()+resourceBase.toString()+uriPath);
FeedResource r = new FeedResource(theApp,f,feedResourceRef,storage);
return r;
} catch (AppException ex) {
if (ex.getStatus().getCode()==Status.CLIENT_ERROR_NOT_FOUND.getCode()) {
// It is possible we have a media entry with no '.' in the name, so test for a media entry
if (segments.length>0) {
String [] path = new String[segments.length-1];
System.arraycopy(segments,0,path,0,segments.length-1);
String feedPath = App.join(path,0,path.length,'/');
file = segments[segments.length-1];
try {
Feed f = app.getFeed(feedPath);
EntryMedia media = f.findEntryResource(file);
if (media!=null) {
MediaResource r = new MediaResource(theApp,f,media,storage);
return r;
} else {
//getLogger().warning("No media resource "+file);
return null;
}
} catch (AppException nex) {
if (ex.getStatus().getCode()==Status.CLIENT_ERROR_NOT_FOUND.getCode()) {
return null;
} else {
getContext().getLogger().log(Level.SEVERE,"Failed to retrieve feed at "+feedPath+" from the database.",nex);
return new ErrorResource(theApp.getStatusService().getRepresentation(Status.SERVER_ERROR_INTERNAL,request,response));
}
} catch (SQLException nex) {
getContext().getLogger().log(Level.SEVERE,"Failed to retrieve entry media "+file+" from feed at "+feedPath+" from the database.",nex);
return new ErrorResource(theApp.getStatusService().getRepresentation(Status.SERVER_ERROR_INTERNAL,request,response));
}
}
return null;
} else {
getContext().getLogger().log(Level.SEVERE,"Failed to retrieve feed at "+uriPath+" from the database.",ex);
return new ErrorResource(theApp.getStatusService().getRepresentation(Status.SERVER_ERROR_INTERNAL,request,response));
}
}
}
} else {
String [] path = new String[segments.length-1];
System.arraycopy(segments,0,path,0,segments.length-1);
String feedPath = App.join(path,0,path.length,'/');
if (file.startsWith("_entry_.")) {
try {
Feed f = app.getFeed(feedPath);
if (f!=null) {
String entryIdS = file.substring(8);
int end = entryIdS.indexOf(".");
if (end<0) {
return null;
}
if (!entryIdS.substring(end).equals(".atom")) {
return null;
}
entryIdS = entryIdS.substring(0,end);
try {
UUID entryId = UUID.fromString(entryIdS);
Entry entry = f.findEntry(entryId);
if (entry!=null) {
EntryResource r = new EntryResource(theApp,f,entry,storage);
return r;
}
} catch (SQLException ex) {
getContext().getLogger().log(Level.SEVERE,"Failed to retrieve entry at "+entryIdS+" from the database.",ex);
return new ErrorResource(theApp.getStatusService().getRepresentation(Status.SERVER_ERROR_INTERNAL,request,response));
} catch (IllegalArgumentException ex) {
return new ErrorResource(theApp.getStatusService().getRepresentation(Status.CLIENT_ERROR_BAD_REQUEST,request,response));
}
}
return null;
} catch (AppException ex) {
if (ex.getStatus().getCode()==Status.CLIENT_ERROR_NOT_FOUND.getCode()) {
return null;
} else {
getContext().getLogger().log(Level.SEVERE,"Failed to retrieve feed at "+feedPath+" from the database.",ex);
return new ErrorResource(theApp.getStatusService().getRepresentation(Status.SERVER_ERROR_INTERNAL,request,response));
}
}
} else if (file.charAt(0)=='.' && file.endsWith(".atom")) {
return new ErrorResource(theApp.getStatusService().getRepresentation(Status.CLIENT_ERROR_BAD_REQUEST,request,response));
} else {
try {
// The media name is already encoded
file = URLDecoder.decode(file,"UTF-8");
file = URLEncoder.encode(file,"UTF-8");
Feed f = app.getFeed(feedPath);
EntryMedia media = f.findEntryResource(file);
if (media!=null) {
MediaResource r = new MediaResource(theApp,f,media,storage);
return r;
} else {
//getLogger().warning("No media resource "+file);