Files
alfresco-community-repo/source/java/org/alfresco/repo/blog/DefaultBlogIntegrationImplementation.java
Raluca Munteanu dc6b2852d0 Merged 5.1.N (5.1.2) to 5.2.N (5.2.1)
125605 rmunteanu: Merged 5.1.1 (5.1.1) to 5.1.N (5.1.2)
      125498 slanglois: MNT-16155 Update source headers - remove svn:eol-style property on Java and JSP source files


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/BRANCHES/DEV/5.2.N/root@125783 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
2016-04-26 13:03:25 +00:00

209 lines
6.9 KiB
Java

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;
}
}