Example usage for com.liferay.portal.kernel.xmlrpc XmlRpcUtil createFault

List of usage examples for com.liferay.portal.kernel.xmlrpc XmlRpcUtil createFault

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.xmlrpc XmlRpcUtil createFault.

Prototype

public static Fault createFault(int code, String description) 

Source Link

Usage

From source file:com.liferay.blogs.internal.util.PingbackMethodImpl.java

License:Open Source License

@Override
public Response execute(long companyId) {
    try {/*from   ww  w  . j a  v  a 2  s  . c  om*/
        addPingback(companyId);

        return XmlRpcUtil.createSuccess("Pingback accepted");
    } catch (DuplicateCommentException dce) {
        return XmlRpcUtil.createFault(PINGBACK_ALREADY_REGISTERED,
                "Pingback is already registered: " + dce.getMessage());
    } catch (InvalidSourceURIException isurie) {
        return XmlRpcUtil.createFault(SOURCE_URI_INVALID, isurie.getMessage());
    } catch (DisabledPingbackException dpe) {
        return XmlRpcUtil.createFault(XmlRpcConstants.REQUESTED_METHOD_NOT_FOUND, dpe.getMessage());
    } catch (UnavailableSourceURIException usurie) {
        return XmlRpcUtil.createFault(SOURCE_URI_DOES_NOT_EXIST, usurie.getMessage());
    } catch (Exception e) {
        if (_log.isDebugEnabled()) {
            _log.debug(e, e);
        }

        return XmlRpcUtil.createFault(TARGET_URI_INVALID, "Unable to parse target URI");
    }
}

From source file:com.liferay.portlet.blogs.util.PingbackMethodImpl.java

License:Open Source License

public Response execute(long companyId) {
    if (!PropsValues.BLOGS_PINGBACK_ENABLED) {
        return XmlRpcUtil.createFault(XmlRpcConstants.REQUESTED_METHOD_NOT_FOUND, "Pingbacks are disabled");
    }/*from   www .j a  v a2 s .  co m*/

    Response response = validateSource();

    if (response != null) {
        return response;
    }

    try {
        BlogsEntry entry = getBlogsEntry(companyId);

        if (!entry.isAllowPingbacks()) {
            return XmlRpcUtil.createFault(XmlRpcConstants.REQUESTED_METHOD_NOT_FOUND, "Pingbacks are disabled");
        }

        long userId = UserLocalServiceUtil.getDefaultUserId(companyId);
        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 = "[...] " + getExcerpt() + " [...] [url=" + _sourceUri + "]"
                + LanguageUtil.get(LocaleUtil.getDefault(), "read-more") + "[/url]";

        List<MBMessage> messages = MBMessageLocalServiceUtil.getThreadMessages(threadId,
                WorkflowConstants.STATUS_APPROVED);

        for (MBMessage message : messages) {
            if (message.getBody().equals(body)) {
                return XmlRpcUtil.createFault(PINGBACK_ALREADY_REGISTERED, "Pingback previously registered");
            }
        }

        ServiceContext serviceContext = new ServiceContext();

        String pingbackUserName = LanguageUtil.get(LocaleUtil.getDefault(), "pingback");

        serviceContext.setAttribute("pingbackUserName", pingbackUserName);

        StringBundler sb = new StringBundler(5);

        String layoutFullURL = PortalUtil.getLayoutFullURL(groupId, PortletKeys.BLOGS);

        sb.append(layoutFullURL);

        sb.append(Portal.FRIENDLY_URL_SEPARATOR);

        Portlet portlet = PortletLocalServiceUtil.getPortletById(companyId, PortletKeys.BLOGS);

        sb.append(portlet.getFriendlyURLMapping());
        sb.append(StringPool.SLASH);
        sb.append(entry.getUrlTitle());

        serviceContext.setAttribute("redirect", sb.toString());

        serviceContext.setLayoutFullURL(layoutFullURL);

        MBMessageLocalServiceUtil.addDiscussionMessage(userId, StringPool.BLANK, groupId, className, classPK,
                threadId, parentMessageId, StringPool.BLANK, body, serviceContext);

        return XmlRpcUtil.createSuccess("Pingback accepted");
    } catch (Exception e) {
        if (_log.isDebugEnabled()) {
            _log.debug(e, e);
        }

        return XmlRpcUtil.createFault(TARGET_URI_INVALID, "Error parsing target URI");
    }
}

From source file:com.liferay.portlet.blogs.util.PingbackMethodImpl.java

License:Open Source License

protected Response validateSource() {
    Source source = null;//w w w. j av a  2 s  .c o  m

    try {
        String html = HttpUtil.URLtoString(_sourceUri);

        source = new Source(html);
    } catch (Exception e) {
        return XmlRpcUtil.createFault(SOURCE_URI_DOES_NOT_EXIST, "Error accessing source URI");
    }

    List<StartTag> startTags = source.getAllStartTags("a");

    for (StartTag startTag : startTags) {
        String href = GetterUtil.getString(startTag.getAttributeValue("href"));

        if (href.equals(_targetUri)) {
            return null;
        }
    }

    return XmlRpcUtil.createFault(SOURCE_URI_INVALID, "Could not find target URI in source");
}