Files
alfresco-community-repo/source/java/org/alfresco/repo/blog/DefaultBlogIntegrationImplementation.java
Tatyana Valkevych 7dd2291753 Merged HEAD-BUG-FIX (5.1/Cloud) to HEAD (5.1/Cloud)
107541: Merged 5.0.N (5.0.3) to HEAD-BUG-FIX (5.1/Cloud) (PARTIAL MERGE)
      107413: Merged DEV to 5.0.N (5.0.3)
         106858 : MNT-13545: JavaDoc : Inconsistencies between the Java doc and the actual code
            - Cleaning of Javadoc,
   107565: MNT-13545 Fix compilation after merge of Javadoc


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@107633 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
2015-07-02 16:13:03 +00:00

227 lines
7.5 KiB
Java

/*
* Copyright (C) 2005-2010 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
package org.alfresco.repo.blog;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
/**
* Default blog integration implementation. Uses various standard XML PRC blogging API to satisfy the
* blog integration implementation interface.
*
* Based on origional contribution by Sudhakar Selvaraj.
*
* @author Roy Wetherall
*/
public abstract class DefaultBlogIntegrationImplementation extends BaseBlogIntegrationImplementation
{
/** Blog actions */
protected static final String ACTION_NEW_POST = "metaWeblog.newPost";
protected static final String ACTION_EDIT_POST = "metaWeblog.editPost";
protected static final String ACTION_GET_POST = "metaWeblog.getPost";
protected static final String ACTION_DELETE_POST = "blogger.deletePost";
/**
* Gets the XML RPC end point URL for the given blog details.
*
* @param blogDetails blog details
* @return String the end point URL
*/
protected abstract String getEndpointURL(BlogDetails blogDetails);
/**
* @see org.alfresco.repo.blog.BlogIntegrationImplementation#newPost(org.alfresco.repo.blog.BlogDetails, java.lang.String, java.lang.String, boolean)
*/
public String newPost(BlogDetails blogDetails, String title, String body, boolean publish)
{
// Create the hash table containing details of the post's content
Hashtable<String, Object> content = new Hashtable<String, Object>();
content.put("title", title);
content.put("description", body);
// Create a list of parameters
List<Object> params = new ArrayList<Object>(5);
params.add(blogDetails.getBlogId());
params.add(blogDetails.getUserName());
params.add(blogDetails.getPassword());
params.add(content);
params.add(publish);
// Create the new post
return (String)execute(getEndpointURL(blogDetails), ACTION_NEW_POST, params);
}
/**
* @see org.alfresco.repo.blog.BlogIntegrationImplementation#updatePost(org.alfresco.repo.blog.BlogDetails, java.lang.String, java.lang.String, java.lang.String, boolean)
*/
public boolean updatePost(BlogDetails blogDetails, String postId, String title, String body, boolean publish)
{
// Create the hash table containing details of the post's content
Hashtable<String, Object> content = new Hashtable<String, Object>();
content.put("title", title);
content.put("description", body);
// Create a list of parameters
List<Object> params = new ArrayList<Object>(5);
params.add(postId);
params.add(blogDetails.getUserName());
params.add(blogDetails.getPassword());
params.add(content);
params.add(publish);
// Create the new post
Object result = execute(getEndpointURL(blogDetails), ACTION_EDIT_POST, params);
if (result.getClass().equals(Boolean.class))
{
return ((Boolean)result).booleanValue();
}
return false;
}
/**
* @see org.alfresco.repo.blog.BlogIntegrationImplementation#getPost(org.alfresco.repo.blog.BlogDetails, java.lang.String)
*/
@SuppressWarnings("unchecked")
public Map<String, Object> getPost(BlogDetails blogDetails, String postId)
{
// Create a list of parameters
List<Object> params = new ArrayList<Object>(3);
params.add(postId);
params.add(blogDetails.getUserName());
params.add(blogDetails.getPassword());
// Get the post details
return (Map<String, Object>)execute(getEndpointURL(blogDetails), ACTION_GET_POST, params);
}
/**
* @see org.alfresco.repo.blog.BlogIntegrationImplementation#deletePost(org.alfresco.repo.blog.BlogDetails, java.lang.String)
*/
public boolean deletePost(BlogDetails blogDetails, String postId)
{
// Create a list of parameters
List<Object> params = new ArrayList<Object>(5);
// Use the blog id for the app key
params.add(blogDetails.getBlogId());
params.add(postId);
params.add(blogDetails.getUserName());
params.add(blogDetails.getPassword());
params.add(true);
// Delete post
Object result = execute(getEndpointURL(blogDetails), ACTION_DELETE_POST, params);
if (result.getClass().equals(Boolean.class))
{
return ((Boolean)result).booleanValue();
}
return false;
}
/**
* Helper method to get the XML RPC client
*
* @param url String
* @return XmlRpcClient
*/
private XmlRpcClient getClient(String url)
{
XmlRpcClient client = null;
try
{
client = new XmlRpcClient();
XmlRpcClientConfigImpl conf = new XmlRpcClientConfigImpl();
conf.setServerURL(new URL(url));
conf.setEncoding("UTF-8");
client.setConfig(conf);
}
catch (MalformedURLException exception)
{
throw new BlogIntegrationRuntimeException("Blog url '" + url + "' is invalid.", exception);
}
return client;
}
/**
* Executes an XML RPC method
*
* @param url String
* @param method String
* @param params List<Object>
* @return Object
*/
protected Object execute(String url, String method, List<Object> params)
{
Object result = null;
try
{
XmlRpcClient client = getClient(url);
result = client.execute(method, params);
}
catch (XmlRpcException exception)
{
throw new BlogIntegrationRuntimeException("Failed to execute blog action '" + method + "' @ url '" + url + "'", exception);
}
return result;
}
/**
* Checks a url for a protocol and adds http if none present
*
* @param url the url
* @return String the checked url
*/
protected String checkForProtocol(String url)
{
if (url.indexOf("://") == -1)
{
url = "http://" + url;
}
return url;
}
/**
* Checks the url for a trailing slash and adds one if none present
*
* @param url the url
* @return String the checked url
*/
protected String checkForTrainlingSlash(String url)
{
if (url.endsWith("/") == false)
{
url = url + "/";
}
return url;
}
}