Updating code to use generics

This commit is contained in:
Patrick Gotthard 2014-04-13 11:30:08 +02:00
parent 0f30ab14ee
commit 4b79f6c1d5
10 changed files with 77 additions and 45 deletions

View file

@ -144,7 +144,7 @@ public class ClientEntry extends Entry {
final int code = -1; final int code = -1;
try { try {
Atom10Generator.serializeEntry(this, sw); Atom10Generator.serializeEntry(this, sw);
method.setRequestEntity(new StringRequestEntity(sw.toString())); method.setRequestEntity(new StringRequestEntity(sw.toString(), null, null));
method.setRequestHeader("Content-type", "application/atom+xml; charset=utf-8"); method.setRequestHeader("Content-type", "application/atom+xml; charset=utf-8");
getHttpClient().executeMethod(method); getHttpClient().executeMethod(method);
final InputStream is = method.getResponseBodyAsStream(); final InputStream is = method.getResponseBodyAsStream();
@ -208,7 +208,7 @@ public class ClientEntry extends Entry {
int code = -1; int code = -1;
try { try {
Atom10Generator.serializeEntry(this, sw); Atom10Generator.serializeEntry(this, sw);
method.setRequestEntity(new StringRequestEntity(sw.toString())); method.setRequestEntity(new StringRequestEntity(sw.toString(), null, null));
method.setRequestHeader("Content-type", "application/atom+xml; charset=utf-8"); method.setRequestHeader("Content-type", "application/atom+xml; charset=utf-8");
getHttpClient().executeMethod(method); getHttpClient().executeMethod(method);
final InputStream is = method.getResponseBodyAsStream(); final InputStream is = method.getResponseBodyAsStream();

View file

@ -214,7 +214,7 @@ public class ClientMediaEntry extends ClientEntry {
method = new PutMethod(getEditURI()); method = new PutMethod(getEditURI());
final StringWriter sw = new StringWriter(); final StringWriter sw = new StringWriter();
Atom10Generator.serializeEntry(this, sw); Atom10Generator.serializeEntry(this, sw);
method.setRequestEntity(new StringRequestEntity(sw.toString())); method.setRequestEntity(new StringRequestEntity(sw.toString(), null, null));
method.setRequestHeader("Content-type", "application/atom+xml; charset=utf8"); method.setRequestHeader("Content-type", "application/atom+xml; charset=utf8");
} else { } else {
throw new ProponoException("ERROR: media entry has no edit URI or media-link URI"); throw new ProponoException("ERROR: media entry has no edit URI or media-link URI");

View file

@ -116,7 +116,9 @@ public class OAuthStrategy implements AuthStrategy {
if (method.getQueryString() != null) { if (method.getQueryString() != null) {
String qstring = method.getQueryString().trim(); String qstring = method.getQueryString().trim();
qstring = qstring.startsWith("?") ? qstring.substring(1) : qstring; qstring = qstring.startsWith("?") ? qstring.substring(1) : qstring;
originalqlist = new ParameterParser().parse(qstring, '&'); @SuppressWarnings("unchecked")
final List<NameValuePair> parameters = new ParameterParser().parse(qstring, '&');
originalqlist = parameters;
} else { } else {
originalqlist = new ArrayList<NameValuePair>(); originalqlist = new ArrayList<NameValuePair>();
} }

View file

@ -31,7 +31,8 @@ import java.util.Map;
public interface AtomRequest { public interface AtomRequest {
/** /**
* Returns any extra path information associated with the URL the client sent when it made this request. * Returns any extra path information associated with the URL the client sent when it made this
* request.
*/ */
public String getPathInfo(); public String getPathInfo();
@ -41,22 +42,26 @@ public interface AtomRequest {
public String getQueryString(); public String getQueryString();
/** /**
* Returns the login of the user making this request, if the user has been authenticated, or null if the user has not been authenticated. * Returns the login of the user making this request, if the user has been authenticated, or
* null if the user has not been authenticated.
*/ */
public String getRemoteUser(); public String getRemoteUser();
/** /**
* Returns a boolean indicating whether the authenticated user is included in the specified logical "role". * Returns a boolean indicating whether the authenticated user is included in the specified
* logical "role".
*/ */
public boolean isUserInRole(String arg0); public boolean isUserInRole(String arg0);
/** /**
* Returns a java.security.Principal object containing the name of the current authenticated user. * Returns a java.security.Principal object containing the name of the current authenticated
* user.
*/ */
public Principal getUserPrincipal(); public Principal getUserPrincipal();
/** /**
* Returns the part of this request's URL from the protocol name up to the query string in the first line of the HTTP request. * Returns the part of this request's URL from the protocol name up to the query string in the
* first line of the HTTP request.
*/ */
public String getRequestURI(); public String getRequestURI();
@ -66,7 +71,8 @@ public interface AtomRequest {
public StringBuffer getRequestURL(); public StringBuffer getRequestURL();
/** /**
* Returns the length, in bytes, of the request body and made available by the input stream, or -1 if the length is not known. * Returns the length, in bytes, of the request body and made available by the input stream, or
* -1 if the length is not known.
*/ */
public int getContentLength(); public int getContentLength();
@ -76,24 +82,27 @@ public interface AtomRequest {
public String getContentType(); public String getContentType();
/** /**
* Returns the value of a request parameter as a String, or null if the parameter does not exist. * Returns the value of a request parameter as a String, or null if the parameter does not
* exist.
*/ */
public String getParameter(String arg0); public String getParameter(String arg0);
/** /**
* Returns an Enumeration of String objects containing the names of the parameters contained in this request. * Returns an Enumeration of String objects containing the names of the parameters contained in
* this request.
*/ */
public Enumeration getParameterNames(); public Enumeration getParameterNames();
/** /**
* Returns an array of String objects containing all of the values the given request parameter has, or null if the parameter does not exist. * Returns an array of String objects containing all of the values the given request parameter
* has, or null if the parameter does not exist.
*/ */
public String[] getParameterValues(String arg0); public String[] getParameterValues(String arg0);
/** /**
* Returns a java.util.Map of the parameters of this request. * Returns a java.util.Map of the parameters of this request.
*/ */
public Map getParameterMap(); public Map<String, Object> getParameterMap();
/** /**
* Retrieves the body of the request as binary data using a ServletInputStream. * Retrieves the body of the request as binary data using a ServletInputStream.
@ -101,7 +110,8 @@ public interface AtomRequest {
public InputStream getInputStream() throws IOException; public InputStream getInputStream() throws IOException;
/** /**
* Returns the value of the specified request header as a long value that represents a Date object. * Returns the value of the specified request header as a long value that represents a Date
* object.
*/ */
public long getDateHeader(String arg0); public long getDateHeader(String arg0);

View file

@ -99,7 +99,7 @@ public class AtomRequestImpl implements AtomRequest {
} }
@Override @Override
public Map getParameterMap() { public Map<String, Object> getParameterMap() {
return wrapped.getParameterMap(); return wrapped.getParameterMap();
} }

View file

@ -23,6 +23,7 @@ import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
import java.util.TreeMap; import java.util.TreeMap;
import org.apache.xmlrpc.client.XmlRpcClient; import org.apache.xmlrpc.client.XmlRpcClient;
@ -104,7 +105,7 @@ public class MetaWeblogBlog implements Blog {
*/ */
@Override @Override
public BlogEntry newEntry() { public BlogEntry newEntry() {
return new MetaWeblogEntry(this, new HashMap()); return new MetaWeblogEntry(this, new HashMap<String, Object>());
} }
String saveEntry(final BlogEntry entry) throws BlogClientException { String saveEntry(final BlogEntry entry) throws BlogClientException {
@ -118,7 +119,10 @@ public class MetaWeblogBlog implements Blog {
@Override @Override
public BlogEntry getEntry(final String id) throws BlogClientException { public BlogEntry getEntry(final String id) throws BlogClientException {
try { try {
final Map result = (Map) getXmlRpcClient().execute("metaWeblog.getPost", new Object[] { id, userName, password }); final Object[] params = new Object[] { id, userName, password };
final Object response = getXmlRpcClient().execute("metaWeblog.getPost", params);
@SuppressWarnings("unchecked")
final Map<String, Object> result = (Map<String, Object>) response;
return new MetaWeblogEntry(this, result); return new MetaWeblogEntry(this, result);
} catch (final Exception e) { } catch (final Exception e) {
throw new BlogClientException("ERROR: XML-RPC error getting entry", e); throw new BlogClientException("ERROR: XML-RPC error getting entry", e);
@ -177,27 +181,35 @@ public class MetaWeblogBlog implements Blog {
public List<Category> getCategories() throws BlogClientException { public List<Category> getCategories() throws BlogClientException {
final ArrayList<Category> ret = new ArrayList<Category>(); final ArrayList<Category> ret = new ArrayList<Category>();
try {
final Object result = getXmlRpcClient().execute("metaWeblog.getCategories", new Object[] { blogid, userName, password });
if (result != null && result instanceof HashMap) {
// Standard MetaWeblog API style: struct of struts
final Map catsmap = (Map) result;
final Iterator keys = catsmap.keySet().iterator(); try {
while (keys.hasNext()) {
final String key = (String) keys.next(); final Object result = getXmlRpcClient().execute("metaWeblog.getCategories", new Object[] { blogid, userName, password });
final Map catmap = (Map) catsmap.get(key);
if (result != null && result instanceof HashMap) {
// Standard MetaWeblog API style: struct of struts
@SuppressWarnings("unchecked")
final Map<String, Object> catsmap = (Map<String, Object>) result;
final Set<String> keys = catsmap.keySet();
for (final String key : keys) {
@SuppressWarnings("unchecked")
final Map<String, Object> catmap = (Map<String, Object>) catsmap.get(key);
final BlogEntry.Category category = new BlogEntry.Category(key); final BlogEntry.Category category = new BlogEntry.Category(key);
category.setName((String) catmap.get("description")); final String description = (String) catmap.get("description");
category.setName(description);
// catmap.get("htmlUrl"); // catmap.get("htmlUrl");
// catmap.get("rssUrl"); // catmap.get("rssUrl");
ret.add(category); ret.add(category);
} }
} else if (result != null && result instanceof Object[]) { } else if (result != null && result instanceof Object[]) {
// Wordpress style: array of structs // Wordpress style: array of structs
final Object[] resultArray = (Object[]) result; final Object[] array = (Object[]) result;
for (final Object element : resultArray) { for (final Object map : array) {
final Map catmap = (Map) element; @SuppressWarnings("unchecked")
final Map<String, Object> catmap = (Map<String, Object>) map;
final String categoryId = (String) catmap.get("categoryId"); final String categoryId = (String) catmap.get("categoryId");
final String categoryName = (String) catmap.get("categoryName"); final String categoryName = (String) catmap.get("categoryName");
final BlogEntry.Category category = new BlogEntry.Category(categoryId); final BlogEntry.Category category = new BlogEntry.Category(categoryId);
@ -208,7 +220,9 @@ public class MetaWeblogBlog implements Blog {
} catch (final Exception e) { } catch (final Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
return ret; return ret;
} }
private Map<String, Object> createPostStructure(final BlogEntry entry) { private Map<String, Object> createPostStructure(final BlogEntry entry) {
@ -356,7 +370,10 @@ public class MetaWeblogBlog implements Blog {
resmap.put("name", resource.getName()); resmap.put("name", resource.getName());
resmap.put("type", resource.getContent().getType()); resmap.put("type", resource.getContent().getType());
resmap.put("bits", resource.getBytes()); resmap.put("bits", resource.getBytes());
final Map result = (Map) getXmlRpcClient().execute("metaWeblog.newMediaObject", new Object[] { blogid, userName, password, resmap }); final Object[] params = new Object[] { blogid, userName, password, resmap };
final Object response = getXmlRpcClient().execute("metaWeblog.newMediaObject", params);
@SuppressWarnings("unchecked")
final Map<String, Object> result = (Map<String, Object>) response;
final String url = (String) result.get("url"); final String url = (String) result.get("url");
res.getContent().setSrc(url); res.getContent().setSrc(url);
return url; return url;
@ -387,6 +404,7 @@ public class MetaWeblogBlog implements Blog {
* Iterates over MetaWeblog API entries. * Iterates over MetaWeblog API entries.
*/ */
public class EntryIterator implements Iterator<BlogEntry> { public class EntryIterator implements Iterator<BlogEntry> {
private int pos = 0; private int pos = 0;
private boolean eod = false; private boolean eod = false;
private static final int BUFSIZE = 30; private static final int BUFSIZE = 30;
@ -418,7 +436,8 @@ public class MetaWeblogBlog implements Blog {
*/ */
@Override @Override
public BlogEntry next() { public BlogEntry next() {
final Map entryHash = (Map) results.get(pos++); @SuppressWarnings("unchecked")
final Map<String, Object> entryHash = (Map<String, Object>) results.get(pos++);
return new MetaWeblogEntry(MetaWeblogBlog.this, entryHash); return new MetaWeblogEntry(MetaWeblogBlog.this, entryHash);
} }

View file

@ -78,9 +78,10 @@ public class MetaWeblogConnection implements BlogConnection {
final Map<String, MetaWeblogBlog> blogMap = new HashMap<String, MetaWeblogBlog>(); final Map<String, MetaWeblogBlog> blogMap = new HashMap<String, MetaWeblogBlog>();
final Object[] results = (Object[]) getXmlRpcClient().execute("blogger.getUsersBlogs", new Object[] { appkey, userName, password }); final Object[] results = (Object[]) getXmlRpcClient().execute("blogger.getUsersBlogs", new Object[] { appkey, userName, password });
for (final Object result : results) { for (final Object result : results) {
final Map<String, String> blog = (Map<String, String>) result; @SuppressWarnings("unchecked")
final String blogid = blog.get("blogid"); final Map<String, Object> blog = (Map<String, Object>) result;
final String name = blog.get("blogName"); final String blogid = (String) blog.get("blogid");
final String name = (String) blog.get("blogName");
blogMap.put(blogid, new MetaWeblogBlog(blogid, name, url, userName, password)); blogMap.put(blogid, new MetaWeblogBlog(blogid, name, url, userName, password));
} }
return blogMap; return blogMap;

View file

@ -30,7 +30,7 @@ import org.rometools.propono.blogclient.BlogEntry;
*/ */
public class MetaWeblogEntry extends BaseBlogEntry { public class MetaWeblogEntry extends BaseBlogEntry {
MetaWeblogEntry(final MetaWeblogBlog blog, final Map entryMap) { MetaWeblogEntry(final MetaWeblogBlog blog, final Map<String, Object> entryMap) {
super(blog); super(blog);
id = (String) entryMap.get("postid"); id = (String) entryMap.get("postid");

View file

@ -33,7 +33,7 @@ public class MetaWeblogResource extends MetaWeblogEntry implements BlogResource
private byte[] bytes; private byte[] bytes;
MetaWeblogResource(final MetaWeblogBlog blog, final String name, final String contentType, final byte[] bytes) { MetaWeblogResource(final MetaWeblogBlog blog, final String name, final String contentType, final byte[] bytes) {
super(blog, new HashMap()); super(blog, new HashMap<String, Object>());
this.blog = blog; this.blog = blog;
this.name = name; this.name = name;
this.contentType = contentType; this.contentType = contentType;

View file

@ -197,15 +197,15 @@ public class SimpleBlogClientTest extends TestCase {
assertNotNull(token); assertNotNull(token);
} }
for (final Iterator it = blog.getEntries(); it.hasNext();) { for (final Iterator<BlogEntry> it = blog.getEntries(); it.hasNext();) {
final BlogEntry blogEntry = (BlogEntry) it.next(); final BlogEntry blogEntry = it.next();
assertTrue(Atom10Parser.isAbsoluteURI(blogEntry.getToken())); assertTrue(Atom10Parser.isAbsoluteURI(blogEntry.getToken()));
blogEntry.delete(); blogEntry.delete();
} }
} }
public static Test suite() { public static Test suite() {
final TestSuite suite = new TestSuite(SimpleBlogClientTest.class); return new TestSuite(SimpleBlogClientTest.class);
return suite;
} }
} }