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;
try {
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");
getHttpClient().executeMethod(method);
final InputStream is = method.getResponseBodyAsStream();
@ -208,7 +208,7 @@ public class ClientEntry extends Entry {
int code = -1;
try {
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");
getHttpClient().executeMethod(method);
final InputStream is = method.getResponseBodyAsStream();

View file

@ -214,7 +214,7 @@ public class ClientMediaEntry extends ClientEntry {
method = new PutMethod(getEditURI());
final StringWriter sw = new StringWriter();
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");
} else {
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) {
String qstring = method.getQueryString().trim();
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 {
originalqlist = new ArrayList<NameValuePair>();
}

View file

@ -31,7 +31,8 @@ import java.util.Map;
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();
@ -41,22 +42,26 @@ public interface AtomRequest {
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();
/**
* 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);
/**
* 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();
/**
* 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();
@ -66,7 +71,8 @@ public interface AtomRequest {
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();
@ -76,24 +82,27 @@ public interface AtomRequest {
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);
/**
* 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();
/**
* 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);
/**
* 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.
@ -101,7 +110,8 @@ public interface AtomRequest {
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);

View file

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

View file

@ -23,6 +23,7 @@ import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import org.apache.xmlrpc.client.XmlRpcClient;
@ -104,7 +105,7 @@ public class MetaWeblogBlog implements Blog {
*/
@Override
public BlogEntry newEntry() {
return new MetaWeblogEntry(this, new HashMap());
return new MetaWeblogEntry(this, new HashMap<String, Object>());
}
String saveEntry(final BlogEntry entry) throws BlogClientException {
@ -118,7 +119,10 @@ public class MetaWeblogBlog implements Blog {
@Override
public BlogEntry getEntry(final String id) throws BlogClientException {
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);
} catch (final Exception 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 {
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();
while (keys.hasNext()) {
final String key = (String) keys.next();
final Map catmap = (Map) catsmap.get(key);
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
@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);
category.setName((String) catmap.get("description"));
final String description = (String) catmap.get("description");
category.setName(description);
// catmap.get("htmlUrl");
// catmap.get("rssUrl");
ret.add(category);
}
} else if (result != null && result instanceof Object[]) {
// Wordpress style: array of structs
final Object[] resultArray = (Object[]) result;
for (final Object element : resultArray) {
final Map catmap = (Map) element;
final Object[] array = (Object[]) result;
for (final Object map : array) {
@SuppressWarnings("unchecked")
final Map<String, Object> catmap = (Map<String, Object>) map;
final String categoryId = (String) catmap.get("categoryId");
final String categoryName = (String) catmap.get("categoryName");
final BlogEntry.Category category = new BlogEntry.Category(categoryId);
@ -208,7 +220,9 @@ public class MetaWeblogBlog implements Blog {
} catch (final Exception e) {
e.printStackTrace();
}
return ret;
}
private Map<String, Object> createPostStructure(final BlogEntry entry) {
@ -356,7 +370,10 @@ public class MetaWeblogBlog implements Blog {
resmap.put("name", resource.getName());
resmap.put("type", resource.getContent().getType());
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");
res.getContent().setSrc(url);
return url;
@ -387,6 +404,7 @@ public class MetaWeblogBlog implements Blog {
* Iterates over MetaWeblog API entries.
*/
public class EntryIterator implements Iterator<BlogEntry> {
private int pos = 0;
private boolean eod = false;
private static final int BUFSIZE = 30;
@ -418,7 +436,8 @@ public class MetaWeblogBlog implements Blog {
*/
@Override
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);
}

View file

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

View file

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

View file

@ -1,10 +1,10 @@
/*
/*
* Copyright 2007 Dave Johnson (Blogapps project)
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
@ -33,7 +33,7 @@ public class MetaWeblogResource extends MetaWeblogEntry implements BlogResource
private 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.name = name;
this.contentType = contentType;

View file

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