Example usage for com.liferay.portal.kernel.util HttpUtil getIpAddress

List of usage examples for com.liferay.portal.kernel.util HttpUtil getIpAddress

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.util HttpUtil getIpAddress.

Prototype

public static String getIpAddress(String url) 

Source Link

Usage

From source file:com.liferay.blogs.web.internal.portlet.action.TrackbackMVCActionCommand.java

License:Open Source License

protected void validate(ActionRequest actionRequest, String remoteIP, String url) throws Exception {

    if (!isCommentsEnabled(actionRequest)) {
        throw new TrackbackValidationException("Comments are disabled");
    }/*www .  j av a  2s .c  o  m*/

    if (Validator.isNull(url)) {
        throw new TrackbackValidationException("Trackback requires a valid permanent URL");
    }

    String trackbackIP = HttpUtil.getIpAddress(url);

    if (!remoteIP.equals(trackbackIP)) {
        throw new TrackbackValidationException("Remote IP does not match the trackback URL's IP");
    }
}

From source file:com.liferay.portlet.blogs.action.TrackbackAction.java

License:Open Source License

protected void addTrackback(ActionRequest actionRequest, ActionResponse actionResponse) throws Exception {

    ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);

    String title = ParamUtil.getString(actionRequest, "title");
    String excerpt = ParamUtil.getString(actionRequest, "excerpt");
    String url = ParamUtil.getString(actionRequest, "url");
    String blogName = ParamUtil.getString(actionRequest, "blog_name");

    if (!isCommentsEnabled(actionRequest)) {
        sendError(actionRequest, actionResponse, "Comments have been disabled for this blog entry.");

        return;//www .ja v a2s  . c  o  m
    }

    if (Validator.isNull(url)) {
        sendError(actionRequest, actionResponse, "Trackback requires a valid permanent URL.");

        return;
    }

    HttpServletRequest request = PortalUtil.getHttpServletRequest(actionRequest);

    String remoteIp = request.getRemoteAddr();

    String trackbackIp = HttpUtil.getIpAddress(url);

    if (!remoteIp.equals(trackbackIp)) {
        sendError(actionRequest, actionResponse,
                "Remote IP " + remoteIp + " does not match trackback URL's IP " + trackbackIp + ".");

        return;
    }

    try {
        ActionUtil.getEntry(actionRequest);
    } catch (PrincipalException pe) {
        sendError(actionRequest, actionResponse,
                "Blog entry must have guest view permissions to enable " + "trackbacks.");

        return;
    }

    BlogsEntry entry = (BlogsEntry) actionRequest.getAttribute(WebKeys.BLOGS_ENTRY);

    if (!entry.isAllowTrackbacks()) {
        sendError(actionRequest, actionResponse, "Trackbacks are not enabled on this blog entry.");

        return;
    }

    long userId = UserLocalServiceUtil.getDefaultUserId(themeDisplay.getCompanyId());
    long groupId = entry.getGroupId();
    String className = BlogsEntry.class.getName();
    long classPK = entry.getEntryId();

    MBMessageDisplay messageDisplay = MBMessageLocalServiceUtil.getDiscussionMessageDisplay(userId, groupId,
            className, classPK, WorkflowConstants.STATUS_APPROVED);

    MBThread thread = messageDisplay.getThread();

    long threadId = thread.getThreadId();
    long parentMessageId = thread.getRootMessageId();
    String body = "[...] " + excerpt + " [...] [url=" + url + "]" + themeDisplay.translate("read-more")
            + "[/url]";

    ServiceContext serviceContext = ServiceContextFactory.getInstance(MBMessage.class.getName(), actionRequest);

    MBMessage message = MBMessageLocalServiceUtil.addDiscussionMessage(userId, blogName, groupId, className,
            classPK, threadId, parentMessageId, title, body, serviceContext);

    String entryURL = PortalUtil.getLayoutFullURL(themeDisplay) + Portal.FRIENDLY_URL_SEPARATOR + "blogs/"
            + entry.getUrlTitle();

    LinkbackConsumerUtil.addNewTrackback(message.getMessageId(), url, entryURL);

    sendSuccess(actionRequest, actionResponse);
}