From: David ‘Bombe’ Roden Date: Mon, 20 Jul 2015 17:28:08 +0000 (+0200) Subject: Merge branch 'feature/fix-coverity-defects' into next X-Git-Tag: 0.9.3^2~15 X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=commitdiff_plain;h=a80191ba660bd732bbd852197cd2d10f619af9d6;hp=0797df88b4485c2c22fcaba6338fe27ce3faa10c Merge branch 'feature/fix-coverity-defects' into next --- diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index e70f963..fe0907f 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -687,8 +687,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, logger.log(Level.WARNING, "Given Identity is null!"); return null; } - final Long latestEdition = tryParse(fromNullable( - identity.getProperty("Sone.LatestEdition")).or("0")); + String property = fromNullable(identity.getProperty("Sone.LatestEdition")).or("0"); + long latestEdition = fromNullable(tryParse(property)).or(0L); Optional existingSone = getSone(identity.getId()); if (existingSone.isPresent() && existingSone.get().isLocal()) { return existingSone.get(); @@ -1064,11 +1064,11 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, } /* load options. */ - sone.getOptions().setAutoFollow(configuration.getBooleanValue(sonePrefix + "/Options/AutoFollow").getValue(null)); - sone.getOptions().setSoneInsertNotificationEnabled(configuration.getBooleanValue(sonePrefix + "/Options/EnableSoneInsertNotifications").getValue(null)); - sone.getOptions().setShowNewSoneNotifications(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewSones").getValue(null)); - sone.getOptions().setShowNewPostNotifications(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewPosts").getValue(null)); - sone.getOptions().setShowNewReplyNotifications(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewReplies").getValue(null)); + sone.getOptions().setAutoFollow(configuration.getBooleanValue(sonePrefix + "/Options/AutoFollow").getValue(false)); + sone.getOptions().setSoneInsertNotificationEnabled(configuration.getBooleanValue(sonePrefix + "/Options/EnableSoneInsertNotifications").getValue(false)); + sone.getOptions().setShowNewSoneNotifications(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewSones").getValue(true)); + sone.getOptions().setShowNewPostNotifications(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewPosts").getValue(true)); + sone.getOptions().setShowNewReplyNotifications(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewReplies").getValue(true)); sone.getOptions().setShowCustomAvatars(ShowCustomAvatars.valueOf(configuration.getStringValue(sonePrefix + "/Options/ShowCustomAvatars").getValue(ShowCustomAvatars.NEVER.name()))); /* if we’re still here, Sone was loaded successfully. */ diff --git a/src/main/java/net/pterodactylus/sone/data/Sone.java b/src/main/java/net/pterodactylus/sone/data/Sone.java index 04c5f39..e64a388 100644 --- a/src/main/java/net/pterodactylus/sone/data/Sone.java +++ b/src/main/java/net/pterodactylus/sone/data/Sone.java @@ -171,17 +171,6 @@ public interface Sone extends Identified, Fingerprintable, Comparable { } }; - public static final Function toSoneXmlUri = - new Function() { - @Nonnull - @Override - public String apply(@Nullable Sone input) { - return input.getRequestUri() - .setMetaString(new String[] { "sone.xml" }) - .toString(); - } - }; - public static final Function> toAllAlbums = new Function>() { @Override public List apply(@Nullable Sone sone) { diff --git a/src/main/java/net/pterodactylus/sone/data/impl/AlbumImpl.java b/src/main/java/net/pterodactylus/sone/data/impl/AlbumImpl.java index c569e0a..3488577 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/AlbumImpl.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/AlbumImpl.java @@ -134,7 +134,7 @@ public class AlbumImpl implements Album { checkArgument(equals(album.getParent()), "album must belong to this album"); int oldIndex = albums.indexOf(album); if (oldIndex <= 0) { - return null; + return album; } albums.remove(oldIndex); albums.add(oldIndex - 1, album); @@ -148,7 +148,7 @@ public class AlbumImpl implements Album { checkArgument(equals(album.getParent()), "album must belong to this album"); int oldIndex = albums.indexOf(album); if ((oldIndex < 0) || (oldIndex >= (albums.size() - 1))) { - return null; + return album; } albums.remove(oldIndex); albums.add(oldIndex + 1, album); @@ -209,7 +209,7 @@ public class AlbumImpl implements Album { checkArgument(image.getAlbum().equals(this), "image must belong to this album"); int oldIndex = imageIds.indexOf(image.getId()); if (oldIndex <= 0) { - return null; + return image; } imageIds.remove(image.getId()); imageIds.add(oldIndex - 1, image.getId()); @@ -224,7 +224,7 @@ public class AlbumImpl implements Album { checkArgument(image.getAlbum().equals(this), "image must belong to this album"); int oldIndex = imageIds.indexOf(image.getId()); if ((oldIndex == -1) || (oldIndex >= (imageIds.size() - 1))) { - return null; + return image; } imageIds.remove(image.getId()); imageIds.add(oldIndex + 1, image.getId()); diff --git a/src/main/java/net/pterodactylus/sone/data/impl/IdOnlySone.java b/src/main/java/net/pterodactylus/sone/data/impl/IdOnlySone.java index 0ef220b..e9a0c57 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/IdOnlySone.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/IdOnlySone.java @@ -18,6 +18,8 @@ import net.pterodactylus.sone.freenet.wot.Identity; import freenet.keys.FreenetURI; +import com.google.common.base.Objects; + /** * {@link Sone} implementation that only stores the ID of a Sone and returns * {@code null}, {@code 0}, or empty collections where appropriate. @@ -240,4 +242,14 @@ public class IdOnlySone implements Sone { return id; } + @Override + public int hashCode() { + return id.hashCode(); + } + + @Override + public boolean equals(Object object) { + return (object != null) && (object.getClass() == getClass()) && Objects.equal(id, ((IdOnlySone) object).id); + } + } diff --git a/src/main/java/net/pterodactylus/sone/freenet/wot/IdentityChangeDetector.java b/src/main/java/net/pterodactylus/sone/freenet/wot/IdentityChangeDetector.java index a8cb62b..2fe10df 100644 --- a/src/main/java/net/pterodactylus/sone/freenet/wot/IdentityChangeDetector.java +++ b/src/main/java/net/pterodactylus/sone/freenet/wot/IdentityChangeDetector.java @@ -142,7 +142,7 @@ public class IdentityChangeDetector { return new Predicate() { @Override public boolean apply(Identity identity) { - return identities.containsKey(identity.getId()); + return (identity != null) && identities.containsKey(identity.getId()); } }; } diff --git a/src/main/java/net/pterodactylus/sone/text/SoneTextParser.java b/src/main/java/net/pterodactylus/sone/text/SoneTextParser.java index b85f100..7319b2b 100644 --- a/src/main/java/net/pterodactylus/sone/text/SoneTextParser.java +++ b/src/main/java/net/pterodactylus/sone/text/SoneTextParser.java @@ -59,41 +59,21 @@ public class SoneTextParser implements Parser { */ private enum LinkType { - /** Link is a KSK. */ - KSK("KSK@"), + KSK("KSK@", true), + CHK("CHK@", true), + SSK("SSK@", true), + USK("USK@", true), + HTTP("http://", false), + HTTPS("https://", false), + SONE("sone://", false), + POST("post://", false); - /** Link is a CHK. */ - CHK("CHK@"), - - /** Link is an SSK. */ - SSK("SSK@"), - - /** Link is a USK. */ - USK("USK@"), - - /** Link is HTTP. */ - HTTP("http://"), - - /** Link is HTTPS. */ - HTTPS("https://"), - - /** Link is a Sone. */ - SONE("sone://"), - - /** Link is a post. */ - POST("post://"); - - /** The scheme identifying this link type. */ private final String scheme; + private final boolean freenetLink; - /** - * Creates a new link type identified by the given scheme. - * - * @param scheme - * The scheme of the link type - */ - private LinkType(String scheme) { + LinkType(String scheme, boolean freenetLink) { this.scheme = scheme; + this.freenetLink = freenetLink; } /** @@ -105,6 +85,10 @@ public class SoneTextParser implements Parser { return scheme; } + public boolean isFreenetLink() { + return freenetLink; + } + } /** The Sone provider. */ @@ -211,7 +195,7 @@ public class SoneTextParser implements Parser { } /* cut off “freenet:” from before keys. */ - if (((linkType == LinkType.KSK) || (linkType == LinkType.CHK) || (linkType == LinkType.SSK) || (linkType == LinkType.USK)) && (next >= 8) && (line.substring(next - 8, next).equals("freenet:"))) { + if (linkType.isFreenetLink() && (next >= 8) && (line.substring(next - 8, next).equals("freenet:"))) { next -= 8; line = line.substring(0, next) + line.substring(next + 8); } diff --git a/src/main/java/net/pterodactylus/sone/web/UploadImagePage.java b/src/main/java/net/pterodactylus/sone/web/UploadImagePage.java index 30a5890..a1e987a 100644 --- a/src/main/java/net/pterodactylus/sone/web/UploadImagePage.java +++ b/src/main/java/net/pterodactylus/sone/web/UploadImagePage.java @@ -17,6 +17,7 @@ package net.pterodactylus.sone.web; +import static com.google.common.base.Optional.fromNullable; import static java.util.logging.Logger.getLogger; import java.awt.Image; @@ -55,8 +56,8 @@ import freenet.support.api.HTTPUploadedFile; */ public class UploadImagePage extends SoneTemplatePage { - /** The logger. */ private static final Logger logger = getLogger(UploadImagePage.class.getName()); + private static final String UNKNOWN_MIME_TYPE = "application/octet-stream"; /** * Creates a new “upload image” page. @@ -154,12 +155,13 @@ public class UploadImagePage extends SoneTemplatePage { ImageInputStream imageInputStream = ImageIO.createImageInputStream(imageDataInputStream); Iterator imageReaders = ImageIO.getImageReaders(imageInputStream); if (imageReaders.hasNext()) { - return imageReaders.next().getOriginatingProvider().getMIMETypes()[0]; + return fromNullable(imageReaders.next().getOriginatingProvider().getMIMETypes()) + .or(new String[] { UNKNOWN_MIME_TYPE })[0]; } } catch (IOException ioe1) { logger.log(Level.FINE, "Could not detect MIME type for image.", ioe1); } - return "application/octet-stream"; + return UNKNOWN_MIME_TYPE; } }