Search in sources :

Example 16 with InvalidArgumentException

use of com.pratilipi.common.exception.InvalidArgumentException in project pratilipi by Pratilipi.

the class AccessTokenFilter method doFilter.

@Override
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    HttpServletRequest request = (HttpServletRequest) req;
    HttpServletResponse response = (HttpServletResponse) resp;
    String requestUri = request.getRequestURI();
    String userAgent = request.getHeader("user-agent");
    String accessTokenId = request.getParameter(RequestParameter.ACCESS_TOKEN.getName());
    accessTokenId = accessTokenId == null ? null : accessTokenId.trim();
    AccessToken accessToken;
    if (requestUri.equals("/remote_api") || (userAgent != null && userAgent.equals("Amazon CloudFront"))) {
        accessToken = null;
    } else if (autoGenerate) {
        // Used by gamma, default & api modules.
        String accessTokenCookie = getCookieValue(RequestCookie.ACCESS_TOKEN.getName(), request);
        if ((accessTokenId == null || accessTokenId.isEmpty()) && (accessTokenCookie == null || accessTokenCookie.isEmpty())) {
            accessToken = AccessTokenDataUtil.newUserAccessToken(request);
        } else {
            accessToken = accessTokenId != null && !accessTokenId.isEmpty() ? dataAccessor.getAccessToken(accessTokenId) : dataAccessor.getAccessToken(accessTokenCookie);
            if (accessToken == null || accessToken.isExpired()) {
                accessToken = AccessTokenDataUtil.newUserAccessToken(request);
            } else if (accessToken.getExpiry().getTime() < new Date().getTime() + AccessTokenDataUtil.MIN_EXPIRY_MILLIS) {
                accessToken.setExpiry(new Date(new Date().getTime() + AccessTokenDataUtil.MAX_EXPIRY_MILLIS));
                accessToken = dataAccessor.createOrUpdateAccessToken(accessToken);
            }
        }
        if (!accessToken.getId().equals(accessTokenCookie)) {
            accessTokenId = accessToken.getId();
            setCookieValue(RequestCookie.ACCESS_TOKEN.getName(), accessTokenId, 30, response);
        }
    } else if (isWorker) {
        // Used by worker module.
        accessTokenId = dataAccessor.getAppProperty(AppProperty.WORKER_ACCESS_TOKEN_ID).getValue();
        accessToken = dataAccessor.getAccessToken(accessTokenId);
    } else if (requestUri.equals("/user/accesstoken")) {
        // Used by gamma-android & android module.
        accessToken = AccessTokenDataUtil.newUserAccessToken(request);
        dispatchResponse(response, new UserAccessTokenApi.Response(accessToken.getId(), accessToken.getExpiry()));
        return;
    } else {
        // TODO: Consider only header. Ignore accessToken from request param.
        if (accessTokenId == null || accessTokenId.isEmpty()) {
            accessTokenId = request.getHeader(RequestHeader.ACCESS_TOKEN.getName());
            accessTokenId = accessTokenId == null ? null : accessTokenId.trim();
        }
        if (accessTokenId == null || accessTokenId.isEmpty()) {
            dispatchResponse(response, new InvalidArgumentException("Access Token is missing."));
            return;
        } else if ((accessToken = dataAccessor.getAccessToken(accessTokenId)) == null) {
            dispatchResponse(response, new InvalidArgumentException("Access Token is invalid."));
            return;
        } else if (accessToken.isExpired()) {
            dispatchResponse(response, new InsufficientAccessException("Access Token is expired."));
            return;
        } else if (accessToken.getExpiry().getTime() < new Date().getTime() + AccessTokenDataUtil.MIN_EXPIRY_MILLIS) {
            accessToken.setExpiry(new Date(new Date().getTime() + AccessTokenDataUtil.MAX_EXPIRY_MILLIS));
            accessToken = dataAccessor.createOrUpdateAccessToken(accessToken);
        }
    }
    threadLocalAccessToken.set(accessToken);
    chain.doFilter(request, response);
    threadLocalAccessToken.remove();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) ServletResponse(javax.servlet.ServletResponse) InvalidArgumentException(com.pratilipi.common.exception.InvalidArgumentException) DataAccessor(com.pratilipi.data.DataAccessor) AccessToken(com.pratilipi.data.type.AccessToken) HttpServletResponse(javax.servlet.http.HttpServletResponse) InsufficientAccessException(com.pratilipi.common.exception.InsufficientAccessException) Date(java.util.Date)

Example 17 with InvalidArgumentException

use of com.pratilipi.common.exception.InvalidArgumentException in project pratilipi by Pratilipi.

the class UserDataUtil method sendPasswordResetMail.

public static void sendPasswordResetMail(String email, Language language) throws InvalidArgumentException, UnexpectedServerException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    User user = dataAccessor.getUserByEmail(email.toLowerCase());
    if (user == null)
        throw new InvalidArgumentException(GenericRequest.ERR_EMAIL_NOT_REGISTERED);
    String verificationToken = _getNextToken(user.getVerificationToken());
    if (!verificationToken.equals(user.getVerificationToken())) {
        user.setVerificationToken(verificationToken);
        user = dataAccessor.createOrUpdateUser(user);
    }
    Map<String, String> dataModel = new HashMap<>();
    String passwordResetUrl = "/" + "?" + "email=" + user.getEmail() + "&" + "token=" + verificationToken.substring(0, verificationToken.indexOf("|")) + "&" + "passwordReset=" + Boolean.TRUE;
    dataModel.put("passwordResetUrl", passwordResetUrl);
    EmailUtil.sendMail(createUserData(user).getDisplayName(), user.getEmail(), "password-reset", language, dataModel);
}
Also used : User(com.pratilipi.data.type.User) InvalidArgumentException(com.pratilipi.common.exception.InvalidArgumentException) HashMap(java.util.HashMap) DataAccessor(com.pratilipi.data.DataAccessor)

Example 18 with InvalidArgumentException

use of com.pratilipi.common.exception.InvalidArgumentException in project pratilipi by Pratilipi.

the class UserDataUtil method registerUser.

public static UserData registerUser(String firstName, String lastName, String email, String password, UserSignUpSource signUpSource) throws InvalidArgumentException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    User user = dataAccessor.getUserByEmail(email.toLowerCase());
    if (user == null || user.getState() == UserState.DELETED) {
        user = dataAccessor.newUser();
    } else if (user.getState() != UserState.REFERRAL) {
        JsonObject errorMessages = new JsonObject();
        errorMessages.addProperty("email", GenericRequest.ERR_EMAIL_REGISTERED_ALREADY);
        throw new InvalidArgumentException(errorMessages);
    }
    AuditLog auditLog = dataAccessor.newAuditLog(AccessTokenFilter.getAccessToken(), AccessType.USER_ADD, user);
    user.setPassword(PasswordUtil.getSaltedHash(password));
    user.setEmail(email.toLowerCase());
    user.setState(UserState.REGISTERED);
    user.setSignUpDate(new Date());
    user.setSignUpSource(signUpSource);
    user = dataAccessor.createOrUpdateUser(user, auditLog);
    UserData userData = createUserData(user);
    userData.setFirstName(firstName);
    userData.setLastName(lastName);
    return userData;
}
Also used : User(com.pratilipi.data.type.User) InvalidArgumentException(com.pratilipi.common.exception.InvalidArgumentException) UserData(com.pratilipi.data.client.UserData) DataAccessor(com.pratilipi.data.DataAccessor) JsonObject(com.google.gson.JsonObject) AuditLog(com.pratilipi.data.type.AuditLog) Date(java.util.Date)

Example 19 with InvalidArgumentException

use of com.pratilipi.common.exception.InvalidArgumentException in project pratilipi by Pratilipi.

the class UserDataUtil method verifyUserEmail.

public static void verifyUserEmail(String email, String verificationToken) throws InvalidArgumentException {
    DataAccessor dataAccessor = DataAccessorFactory.getDataAccessor();
    User user = dataAccessor.getUserByEmail(email.toLowerCase());
    if (user == null || user.getState() != UserState.REGISTERED)
        return;
    if (!_verifyToken(user, verificationToken))
        throw new InvalidArgumentException(GenericRequest.ERR_VERIFICATION_TOKEN_INVALID_OR_EXPIRED);
    AuditLog auditLog = dataAccessor.newAuditLog(AccessTokenFilter.getAccessToken(), AccessType.USER_UPDATE, user);
    user.setState(UserState.ACTIVE);
    // Note: Do not clear VerificationToken
    user = dataAccessor.createOrUpdateUser(user, auditLog);
}
Also used : User(com.pratilipi.data.type.User) InvalidArgumentException(com.pratilipi.common.exception.InvalidArgumentException) DataAccessor(com.pratilipi.data.DataAccessor) AuditLog(com.pratilipi.data.type.AuditLog)

Example 20 with InvalidArgumentException

use of com.pratilipi.common.exception.InvalidArgumentException in project pratilipi by Pratilipi.

the class PratilipiDocUtil method _setPage.

private static void _setPage(Long pratilipiId, PratilipiContentDoc.Page page, String html) throws InvalidArgumentException, UnexpectedServerException {
    // Deleting Existing Pagelets
    page.deleteAllPagelets();
    // Adding Pagelets from HTML
    if (html != null && !html.trim().isEmpty()) {
        Node body = Jsoup.parse(html).body();
        Node badNode = _validateContent(body);
        if (badNode != null) {
            String errMsg = "";
            while (badNode != body) {
                errMsg = " > " + badNode.nodeName() + errMsg;
                badNode = badNode.parent();
            }
            errMsg = "Invalid node " + errMsg;
            throw new InvalidArgumentException(errMsg);
        }
        for (Node node : body.childNodes()) {
            if (node.nodeName().equals("p")) {
                if (node.childNodeSize() == 1 && node.childNode(0).equals("img")) {
                    JsonObject imgData = _createImageData(pratilipiId, node.childNode(0));
                    if (imgData != null)
                        page.addPagelet(PageletType.IMAGE, imgData);
                } else {
                    page.addPagelet(PageletType.HTML, ((Element) node).html(), _getAlignment(node));
                }
            } else if (node.nodeName().equals("img")) {
                JsonObject imgData = _createImageData(pratilipiId, node);
                if (imgData != null)
                    page.addPagelet(PageletType.IMAGE, imgData);
            } else if (node.nodeName().equals("blockquote")) {
                page.addPagelet(PageletType.BLOCK_QUOTE, ((Element) node).html());
            } else if (node.nodeName().equals("ol")) {
                page.addPagelet(PageletType.LIST_ORDERED, ((Element) node).html());
            } else if (node.nodeName().equals("ul")) {
                page.addPagelet(PageletType.LIST_UNORDERED, ((Element) node).html());
            }
        }
    }
}
Also used : InvalidArgumentException(com.pratilipi.common.exception.InvalidArgumentException) Node(org.jsoup.nodes.Node) TextNode(org.jsoup.nodes.TextNode) Element(org.jsoup.nodes.Element) JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject)

Aggregations

InvalidArgumentException (com.pratilipi.common.exception.InvalidArgumentException)37 JsonObject (com.google.gson.JsonObject)21 DataAccessor (com.pratilipi.data.DataAccessor)19 InsufficientAccessException (com.pratilipi.common.exception.InsufficientAccessException)11 UnexpectedServerException (com.pratilipi.common.exception.UnexpectedServerException)9 User (com.pratilipi.data.type.User)9 Gson (com.google.gson.Gson)7 GenericResponse (com.pratilipi.api.shared.GenericResponse)7 AuditLog (com.pratilipi.data.type.AuditLog)7 Author (com.pratilipi.data.type.Author)6 JsonElement (com.google.gson.JsonElement)5 Post (com.pratilipi.api.annotation.Post)5 UserData (com.pratilipi.data.client.UserData)5 Page (com.pratilipi.data.type.Page)5 Pratilipi (com.pratilipi.data.type.Pratilipi)5 Date (java.util.Date)5 HashMap (java.util.HashMap)5 Language (com.pratilipi.common.type.Language)3 UserPratilipi (com.pratilipi.data.type.UserPratilipi)3 PrintWriter (java.io.PrintWriter)3