import net.pterodactylus.sone.data.Sone.SoneStatus;
import net.pterodactylus.sone.data.TemporaryImage;
import net.pterodactylus.sone.database.AlbumBuilder;
+import net.pterodactylus.sone.database.AlbumProvider;
import net.pterodactylus.sone.database.Database;
import net.pterodactylus.sone.database.DatabaseException;
import net.pterodactylus.sone.database.ImageBuilder;
* @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
*/
@Singleton
-public class Core extends AbstractService implements SoneProvider, PostProvider, PostReplyProvider {
+public class Core extends AbstractService implements SoneProvider, PostProvider, PostReplyProvider, AlbumProvider {
/** The logger. */
private static final Logger logger = getLogger(Core.class.getName());
* @return The album with the given ID, or {@code null} if no album with the
* given ID exists
*/
- public Album getAlbum(String albumId) {
- return database.getAlbum(albumId).orNull();
+ public Optional<Album> getAlbum(String albumId) {
+ return database.getAlbum(albumId);
}
public ImageBuilder imageBuilder() {
import net.pterodactylus.util.xml.SimpleXML;
import net.pterodactylus.util.xml.XML;
+import com.google.common.base.Optional;
import org.w3c.dom.Document;
/**
logger.log(Level.WARNING, String.format("Downloaded Sone %s contains invalid album!", sone));
return null;
}
- Album parent = null;
+ Optional<Album> parent = Optional.absent();
if (parentId != null) {
parent = core.getAlbum(parentId);
- if (parent == null) {
+ if (!parent.isPresent()) {
logger.log(Level.WARNING, String.format("Downloaded Sone %s has album with invalid parent!", sone));
return null;
}
.setTitle(title)
.setDescription(description)
.update();
- if (parent != null) {
- parent.addAlbum(album);
+ if (parent.isPresent()) {
+ parent.get().addAlbum(album);
} else {
topLevelAlbums.add(album);
}
import net.pterodactylus.util.template.TemplateContext;
import net.pterodactylus.util.web.Method;
+import com.google.common.base.Optional;
+
/**
* Page that lets the user create a new album.
*
String description = request.getHttpRequest().getPartAsStringFailsafe("description", 256).trim();
Sone currentSone = getCurrentSone(request.getToadletContext());
String parentId = request.getHttpRequest().getPartAsStringFailsafe("parent", IdBuilder.ID_STRING_LENGTH);
- Album parent = webInterface.getCore().getAlbum(parentId);
+ Optional<Album> parent;
if (parentId.equals("")) {
- parent = currentSone.getRootAlbum();
+ parent = Optional.of(currentSone.getRootAlbum());
+ } else {
+ parent = webInterface.getCore().getAlbum(parentId);
+ if (!parent.isPresent()) {
+ throw new RedirectException("noPermission.html");
+ }
}
- Album album = webInterface.getCore().createAlbum(currentSone, parent);
+ Album album = webInterface.getCore().createAlbum(currentSone, parent.get());
try {
album.modify().setTitle(name).setDescription(TextFilter.filter(request.getHttpRequest().getHeader("host"), description)).update();
} catch (AlbumTitleMustNotBeEmpty atmnbe) {
import net.pterodactylus.util.template.TemplateContext;
import net.pterodactylus.util.web.Method;
+import com.google.common.base.Optional;
+
/**
* Page that lets the user delete an {@link Album}.
*
super.processTemplate(request, templateContext);
if (request.getMethod() == Method.POST) {
String albumId = request.getHttpRequest().getPartAsStringFailsafe("album", IdBuilder.ID_STRING_LENGTH);
- Album album = webInterface.getCore().getAlbum(albumId);
- if (album == null) {
+ Optional<Album> album = webInterface.getCore().getAlbum(albumId);
+ if (!album.isPresent()) {
throw new RedirectException("invalid.html");
}
- if (!album.getSone().isLocal()) {
+ if (!album.get().getSone().isLocal()) {
throw new RedirectException("noPermission.html");
}
if (request.getHttpRequest().isPartSet("abortDelete")) {
- throw new RedirectException("imageBrowser.html?album=" + album.getId());
+ throw new RedirectException("imageBrowser.html?album=" + album.get().getId());
}
- Album parentAlbum = album.getParent();
- webInterface.getCore().deleteAlbum(album);
- if (parentAlbum.equals(album.getSone().getRootAlbum())) {
- throw new RedirectException("imageBrowser.html?sone=" + album.getSone().getId());
+ Album parentAlbum = album.get().getParent();
+ webInterface.getCore().deleteAlbum(album.get());
+ if (parentAlbum.equals(album.get().getSone().getRootAlbum())) {
+ throw new RedirectException("imageBrowser.html?sone=" + album.get().getSone().getId());
}
throw new RedirectException("imageBrowser.html?album=" + parentAlbum.getId());
}
String albumId = request.getHttpRequest().getParam("album");
- Album album = webInterface.getCore().getAlbum(albumId);
- if (album == null) {
+ Optional<Album> album = webInterface.getCore().getAlbum(albumId);
+ if (!album.isPresent()) {
throw new RedirectException("invalid.html");
}
templateContext.set("album", album);
import net.pterodactylus.util.template.TemplateContext;
import net.pterodactylus.util.web.Method;
+import com.google.common.base.Optional;
+
/**
* Page that lets the user edit the name and description of an album.
*
super.processTemplate(request, templateContext);
if (request.getMethod() == Method.POST) {
String albumId = request.getHttpRequest().getPartAsStringFailsafe("album", IdBuilder.ID_STRING_LENGTH);
- Album album = webInterface.getCore().getAlbum(albumId);
- if (album == null) {
+ Optional<Album> album = webInterface.getCore().getAlbum(albumId);
+ if (!album.isPresent()) {
throw new RedirectException("invalid.html");
}
- if (!album.getSone().isLocal()) {
+ if (!album.get().getSone().isLocal()) {
throw new RedirectException("noPermission.html");
}
if ("true".equals(request.getHttpRequest().getPartAsStringFailsafe("moveLeft", 4))) {
- album.getParent().moveAlbumUp(album);
+ album.get().getParent().moveAlbumUp(album.get());
webInterface.getCore().touchConfiguration();
- throw new RedirectException("imageBrowser.html?album=" + album.getParent().getId());
+ throw new RedirectException("imageBrowser.html?album=" + album.get().getParent().getId());
} else if ("true".equals(request.getHttpRequest().getPartAsStringFailsafe("moveRight", 4))) {
- album.getParent().moveAlbumDown(album);
+ album.get().getParent().moveAlbumDown(album.get());
webInterface.getCore().touchConfiguration();
- throw new RedirectException("imageBrowser.html?album=" + album.getParent().getId());
+ throw new RedirectException("imageBrowser.html?album=" + album.get().getParent().getId());
}
String albumImageId = request.getHttpRequest().getPartAsStringFailsafe("album-image", IdBuilder.ID_STRING_LENGTH);
if (webInterface.getCore().getImage(albumImageId, false) == null) {
albumImageId = null;
}
- album.modify().setAlbumImage(albumImageId).update();
+ album.get().modify().setAlbumImage(albumImageId).update();
String title = request.getHttpRequest().getPartAsStringFailsafe("title", 100).trim();
String description = request.getHttpRequest().getPartAsStringFailsafe("description", 1000).trim();
try {
- album.modify().setTitle(title).setDescription(TextFilter.filter(request.getHttpRequest().getHeader("host"), description)).update();
+ album.get().modify().setTitle(title).setDescription(TextFilter.filter(request.getHttpRequest().getHeader("host"), description)).update();
} catch (AlbumTitleMustNotBeEmpty atmnbe) {
throw new RedirectException("emptyAlbumTitle.html");
}
webInterface.getCore().touchConfiguration();
- throw new RedirectException("imageBrowser.html?album=" + album.getId());
+ throw new RedirectException("imageBrowser.html?album=" + album.get().getId());
}
}
super.processTemplate(request, templateContext);
String albumId = request.getHttpRequest().getParam("album", null);
if (albumId != null) {
- Album album = webInterface.getCore().getAlbum(albumId);
+ Optional<Album> album = webInterface.getCore().getAlbum(albumId);
templateContext.set("albumRequested", true);
- templateContext.set("album", album);
+ templateContext.set("album", album.orNull());
templateContext.set("page", request.getHttpRequest().getParam("page"));
return;
}
import java.util.logging.Level;
import java.util.logging.Logger;
+import net.pterodactylus.sone.data.Album;
import net.pterodactylus.sone.data.Post;
import net.pterodactylus.sone.data.PostReply;
import net.pterodactylus.sone.data.Profile;
*/
private String getAlbumId(String phrase) {
String albumId = phrase.startsWith("album://") ? phrase.substring(8) : phrase;
- return (webInterface.getCore().getAlbum(albumId) != null) ? albumId : null;
+ return webInterface.getCore().getAlbum(albumId).isPresent() ? albumId : null;
}
/**
import net.pterodactylus.util.template.TemplateContext;
import net.pterodactylus.util.web.Method;
+import com.google.common.base.Optional;
import com.google.common.io.ByteStreams;
import freenet.support.api.Bucket;
if (request.getMethod() == Method.POST) {
Sone currentSone = getCurrentSone(request.getToadletContext());
String parentId = request.getHttpRequest().getPartAsStringFailsafe("parent", IdBuilder.ID_STRING_LENGTH);
- Album parent = webInterface.getCore().getAlbum(parentId);
- if (parent == null) {
+ Optional<Album> parent = webInterface.getCore().getAlbum(parentId);
+ if (!parent.isPresent()) {
throw new RedirectException("noPermission.html");
}
- if (!currentSone.equals(parent.getSone())) {
+ if (!currentSone.equals(parent.get().getSone())) {
throw new RedirectException("noPermission.html");
}
String name = request.getHttpRequest().getPartAsStringFailsafe("title", 200);
}
String mimeType = getMimeType(imageData);
TemporaryImage temporaryImage = webInterface.getCore().createTemporaryImage(mimeType, imageData);
- net.pterodactylus.sone.data.Image image = webInterface.getCore().createImage(currentSone, parent, temporaryImage);
+ net.pterodactylus.sone.data.Image image = webInterface.getCore().createImage(currentSone, parent.get(), temporaryImage);
image.modify().setTitle(name).setDescription(TextFilter.filter(request.getHttpRequest().getHeader("host"), description)).setWidth(uploadedImage.getWidth(null)).setHeight(uploadedImage.getHeight(null)).update();
} catch (IOException ioe1) {
logger.log(Level.WARNING, "Could not read uploaded image!", ioe1);
Closer.close(imageDataInputStream);
Closer.flush(uploadedImage);
}
- throw new RedirectException("imageBrowser.html?album=" + parent.getId());
+ throw new RedirectException("imageBrowser.html?album=" + parent.get().getId());
}
}
import net.pterodactylus.sone.web.WebInterface;
import net.pterodactylus.sone.web.page.FreenetRequest;
+import com.google.common.base.Optional;
+
/**
* Page that stores a user’s album modifications.
*
@Override
protected JsonReturnObject createJsonObject(FreenetRequest request) {
String albumId = request.getHttpRequest().getParam("album");
- Album album = webInterface.getCore().getAlbum(albumId);
- if (album == null) {
+ Optional<Album> album = webInterface.getCore().getAlbum(albumId);
+ if (!album.isPresent()) {
return createErrorJsonObject("invalid-album-id");
}
- if (!album.getSone().isLocal()) {
+ if (!album.get().getSone().isLocal()) {
return createErrorJsonObject("not-authorized");
}
if ("true".equals(request.getHttpRequest().getParam("moveLeft"))) {
- Album swappedAlbum = album.getParent().moveAlbumUp(album);
+ Album swappedAlbum = album.get().getParent().moveAlbumUp(album.get());
webInterface.getCore().touchConfiguration();
- return createSuccessJsonObject().put("sourceAlbumId", album.getId()).put("destinationAlbumId", swappedAlbum.getId());
+ return createSuccessJsonObject().put("sourceAlbumId", album.get().getId()).put("destinationAlbumId", swappedAlbum.getId());
}
if ("true".equals(request.getHttpRequest().getParam("moveRight"))) {
- Album swappedAlbum = album.getParent().moveAlbumDown(album);
+ Album swappedAlbum = album.get().getParent().moveAlbumDown(album.get());
webInterface.getCore().touchConfiguration();
- return createSuccessJsonObject().put("sourceAlbumId", album.getId()).put("destinationAlbumId", swappedAlbum.getId());
+ return createSuccessJsonObject().put("sourceAlbumId", album.get().getId()).put("destinationAlbumId", swappedAlbum.getId());
}
String title = request.getHttpRequest().getParam("title").trim();
String description = request.getHttpRequest().getParam("description").trim();
try {
- album.modify().setTitle(title).setDescription(TextFilter.filter(request.getHttpRequest().getHeader("host"), description)).update();
+ album.get().modify().setTitle(title).setDescription(TextFilter.filter(request.getHttpRequest().getHeader("host"), description)).update();
webInterface.getCore().touchConfiguration();
- return createSuccessJsonObject().put("albumId", album.getId()).put("title", album.getTitle()).put("description", album.getDescription());
+ return createSuccessJsonObject().put("albumId", album.get().getId()).put("title", album.get().getTitle()).put("description", album.get().getDescription());
} catch (IllegalStateException e) {
return createErrorJsonObject("invalid-album-title");
}
@Before
public void setupAlbums() {
- when(core.getAlbum(anyString())).thenAnswer(new Answer<Album>() {
+ when(core.getAlbum(anyString())).thenAnswer(new Answer<Optional<Album>>() {
@Override
- public Album answer(InvocationOnMock invocation)
+ public Optional<Album> answer(InvocationOnMock invocation)
throws Throwable {
- return albums.get(invocation.getArguments()[0]);
+ return Optional.fromNullable(albums.get(invocation.getArguments()[0]));
}
});
}