forked from Archives/Athou_commafeed
use java8 optional
This commit is contained in:
@@ -4,6 +4,7 @@ import io.dropwizard.lifecycle.Managed;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Singleton;
|
||||
@@ -20,7 +21,6 @@ import com.commafeed.backend.HttpGetter.NotModifiedException;
|
||||
import com.commafeed.backend.feed.FeedRefreshExecutor.Task;
|
||||
import com.commafeed.backend.model.Feed;
|
||||
import com.commafeed.backend.model.FeedEntry;
|
||||
import com.google.common.base.Optional;
|
||||
|
||||
/**
|
||||
* Calls {@link FeedFetcher} and handles its outcome
|
||||
@@ -84,7 +84,7 @@ public class FeedRefreshWorker implements Managed {
|
||||
int refreshInterval = config.getApplicationSettings().getRefreshIntervalMinutes();
|
||||
Date disabledUntil = DateUtils.addMinutes(new Date(), refreshInterval);
|
||||
try {
|
||||
String url = Optional.fromNullable(feed.getUrlAfterRedirect()).or(feed.getUrl());
|
||||
String url = Optional.ofNullable(feed.getUrlAfterRedirect()).orElse(feed.getUrl());
|
||||
FetchedFeed fetchedFeed = fetcher.fetch(url, false, feed.getLastModifiedHeader(), feed.getEtagHeader(),
|
||||
feed.getLastPublishedDate(), feed.getLastContentHash());
|
||||
// stops here if NotModifiedException or any other exception is thrown
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.commafeed.backend.service;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.inject.Inject;
|
||||
@@ -17,7 +18,6 @@ import lombok.RequiredArgsConstructor;
|
||||
import com.commafeed.CommaFeedConfiguration;
|
||||
import com.commafeed.CommaFeedConfiguration.ApplicationSettings;
|
||||
import com.commafeed.backend.model.User;
|
||||
import com.google.common.base.Optional;
|
||||
|
||||
/**
|
||||
* Mailing service
|
||||
@@ -35,7 +35,7 @@ public class MailService {
|
||||
|
||||
final String username = settings.getSmtpUserName();
|
||||
final String password = settings.getSmtpPassword();
|
||||
final String fromAddress = Optional.fromNullable(settings.getSmtpFromAddress()).or(settings.getSmtpUserName());
|
||||
final String fromAddress = Optional.ofNullable(settings.getSmtpFromAddress()).orElse(settings.getSmtpUserName());
|
||||
|
||||
String dest = user.getEmail();
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.commafeed.backend.service;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Date;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
import javax.inject.Inject;
|
||||
@@ -20,7 +21,6 @@ import com.commafeed.backend.model.User;
|
||||
import com.commafeed.backend.model.UserRole;
|
||||
import com.commafeed.backend.model.UserRole.Role;
|
||||
import com.commafeed.backend.service.internal.PostLoginActivities;
|
||||
import com.google.common.base.Optional;
|
||||
import com.google.common.base.Preconditions;
|
||||
|
||||
@RequiredArgsConstructor(onConstructor = @__({ @Inject }))
|
||||
@@ -41,7 +41,7 @@ public class UserService {
|
||||
*/
|
||||
public Optional<User> login(String nameOrEmail, String password) {
|
||||
if (nameOrEmail == null || password == null) {
|
||||
return Optional.absent();
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
User user = userDAO.findByName(nameOrEmail);
|
||||
@@ -55,7 +55,7 @@ public class UserService {
|
||||
return Optional.of(user);
|
||||
}
|
||||
}
|
||||
return Optional.absent();
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -63,7 +63,7 @@ public class UserService {
|
||||
*/
|
||||
public Optional<User> login(String apiKey) {
|
||||
if (apiKey == null) {
|
||||
return Optional.absent();
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
User user = userDAO.findByApiKey(apiKey);
|
||||
@@ -71,7 +71,7 @@ public class UserService {
|
||||
performPostLoginActivities(user);
|
||||
return Optional.of(user);
|
||||
}
|
||||
return Optional.absent();
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package com.commafeed.frontend.auth;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.ws.rs.WebApplicationException;
|
||||
@@ -18,7 +20,6 @@ import com.commafeed.backend.model.User;
|
||||
import com.commafeed.backend.model.UserRole.Role;
|
||||
import com.commafeed.backend.service.UserService;
|
||||
import com.commafeed.frontend.session.SessionHelper;
|
||||
import com.google.common.base.Optional;
|
||||
|
||||
@RequiredArgsConstructor
|
||||
public class SecurityCheckFactory extends AbstractContainerRequestValueFactory<User> {
|
||||
@@ -82,7 +83,7 @@ public class SecurityCheckFactory extends AbstractContainerRequestValueFactory<U
|
||||
}
|
||||
}
|
||||
}
|
||||
return Optional.absent();
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
private Optional<User> apiKeyLogin() {
|
||||
@@ -90,7 +91,7 @@ public class SecurityCheckFactory extends AbstractContainerRequestValueFactory<U
|
||||
if (apiKey != null && apiKeyAllowed) {
|
||||
return userService.login(apiKey);
|
||||
}
|
||||
return Optional.absent();
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import io.dropwizard.jersey.validation.ValidationErrorMessage;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
import javax.inject.Inject;
|
||||
@@ -56,7 +57,6 @@ import com.commafeed.frontend.model.request.PasswordResetRequest;
|
||||
import com.commafeed.frontend.model.request.ProfileModificationRequest;
|
||||
import com.commafeed.frontend.model.request.RegistrationRequest;
|
||||
import com.commafeed.frontend.session.SessionHelper;
|
||||
import com.google.common.base.Optional;
|
||||
import com.google.common.base.Preconditions;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.wordnik.swagger.annotations.Api;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.commafeed.frontend.servlet;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Optional;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Singleton;
|
||||
@@ -18,7 +19,6 @@ import com.commafeed.backend.dao.UserSettingsDAO;
|
||||
import com.commafeed.backend.model.User;
|
||||
import com.commafeed.backend.model.UserSettings;
|
||||
import com.commafeed.frontend.session.SessionHelper;
|
||||
import com.google.common.base.Optional;
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
@RequiredArgsConstructor(onConstructor = @__({ @Inject }))
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.commafeed.frontend.servlet;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Singleton;
|
||||
@@ -28,7 +29,6 @@ import com.commafeed.backend.model.UserSettings.ReadingOrder;
|
||||
import com.commafeed.backend.service.UserService;
|
||||
import com.commafeed.frontend.resource.CategoryREST;
|
||||
import com.commafeed.frontend.session.SessionHelper;
|
||||
import com.google.common.base.Optional;
|
||||
import com.google.common.collect.Iterables;
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
|
||||
@@ -1,39 +1,40 @@
|
||||
package com.commafeed.frontend.session;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpSession;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
||||
import com.commafeed.backend.model.User;
|
||||
import com.google.common.base.Optional;
|
||||
|
||||
@RequiredArgsConstructor()
|
||||
public class SessionHelper {
|
||||
|
||||
|
||||
private static final String SESSION_KEY_USER = "user";
|
||||
|
||||
|
||||
private final HttpServletRequest request;
|
||||
|
||||
|
||||
public Optional<User> getLoggedInUser() {
|
||||
Optional<HttpSession> session = getSession(false);
|
||||
|
||||
|
||||
if (session.isPresent()) {
|
||||
User user = (User) session.get().getAttribute(SESSION_KEY_USER);
|
||||
return Optional.fromNullable(user);
|
||||
return Optional.ofNullable(user);
|
||||
}
|
||||
|
||||
return Optional.absent();
|
||||
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
|
||||
public void setLoggedInUser(User user) {
|
||||
Optional<HttpSession> session = getSession(true);
|
||||
session.get().setAttribute(SESSION_KEY_USER, user);
|
||||
}
|
||||
|
||||
|
||||
private Optional<HttpSession> getSession(boolean force) {
|
||||
HttpSession session = request.getSession(force);
|
||||
return Optional.fromNullable(session);
|
||||
return Optional.ofNullable(session);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user