From 4b79f6c1d5d00a08f061139b96539577f9c540f3 Mon Sep 17 00:00:00 2001 From: Patrick Gotthard Date: Sun, 13 Apr 2014 11:30:08 +0200 Subject: [PATCH] Updating code to use generics --- .../propono/atom/client/ClientEntry.java | 4 +- .../propono/atom/client/ClientMediaEntry.java | 2 +- .../propono/atom/client/OAuthStrategy.java | 4 +- .../propono/atom/server/AtomRequest.java | 32 +++++++---- .../propono/atom/server/AtomRequestImpl.java | 2 +- .../blogclient/metaweblog/MetaWeblogBlog.java | 53 +++++++++++++------ .../metaweblog/MetaWeblogConnection.java | 7 +-- .../metaweblog/MetaWeblogEntry.java | 2 +- .../metaweblog/MetaWeblogResource.java | 8 +-- .../blogclient/SimpleBlogClientTest.java | 8 +-- 10 files changed, 77 insertions(+), 45 deletions(-) diff --git a/src/main/java/org/rometools/propono/atom/client/ClientEntry.java b/src/main/java/org/rometools/propono/atom/client/ClientEntry.java index 6bd544f..f8357f0 100644 --- a/src/main/java/org/rometools/propono/atom/client/ClientEntry.java +++ b/src/main/java/org/rometools/propono/atom/client/ClientEntry.java @@ -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(); diff --git a/src/main/java/org/rometools/propono/atom/client/ClientMediaEntry.java b/src/main/java/org/rometools/propono/atom/client/ClientMediaEntry.java index dfb49bf..0578e31 100644 --- a/src/main/java/org/rometools/propono/atom/client/ClientMediaEntry.java +++ b/src/main/java/org/rometools/propono/atom/client/ClientMediaEntry.java @@ -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"); diff --git a/src/main/java/org/rometools/propono/atom/client/OAuthStrategy.java b/src/main/java/org/rometools/propono/atom/client/OAuthStrategy.java index e631367..94d2e50 100644 --- a/src/main/java/org/rometools/propono/atom/client/OAuthStrategy.java +++ b/src/main/java/org/rometools/propono/atom/client/OAuthStrategy.java @@ -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 parameters = new ParameterParser().parse(qstring, '&'); + originalqlist = parameters; } else { originalqlist = new ArrayList(); } diff --git a/src/main/java/org/rometools/propono/atom/server/AtomRequest.java b/src/main/java/org/rometools/propono/atom/server/AtomRequest.java index 33b20dc..6bd9ddc 100644 --- a/src/main/java/org/rometools/propono/atom/server/AtomRequest.java +++ b/src/main/java/org/rometools/propono/atom/server/AtomRequest.java @@ -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 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); diff --git a/src/main/java/org/rometools/propono/atom/server/AtomRequestImpl.java b/src/main/java/org/rometools/propono/atom/server/AtomRequestImpl.java index 2186965..07e26e1 100644 --- a/src/main/java/org/rometools/propono/atom/server/AtomRequestImpl.java +++ b/src/main/java/org/rometools/propono/atom/server/AtomRequestImpl.java @@ -99,7 +99,7 @@ public class AtomRequestImpl implements AtomRequest { } @Override - public Map getParameterMap() { + public Map getParameterMap() { return wrapped.getParameterMap(); } diff --git a/src/main/java/org/rometools/propono/blogclient/metaweblog/MetaWeblogBlog.java b/src/main/java/org/rometools/propono/blogclient/metaweblog/MetaWeblogBlog.java index 88aa639..a409b6f 100644 --- a/src/main/java/org/rometools/propono/blogclient/metaweblog/MetaWeblogBlog.java +++ b/src/main/java/org/rometools/propono/blogclient/metaweblog/MetaWeblogBlog.java @@ -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 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 result = (Map) 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 getCategories() throws BlogClientException { final ArrayList ret = new ArrayList(); - 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 catsmap = (Map) result; + + final Set keys = catsmap.keySet(); + for (final String key : keys) { + @SuppressWarnings("unchecked") + final Map catmap = (Map) 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 catmap = (Map) 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 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 result = (Map) 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 { + 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 entryHash = (Map) results.get(pos++); return new MetaWeblogEntry(MetaWeblogBlog.this, entryHash); } diff --git a/src/main/java/org/rometools/propono/blogclient/metaweblog/MetaWeblogConnection.java b/src/main/java/org/rometools/propono/blogclient/metaweblog/MetaWeblogConnection.java index 4badeec..1df6064 100644 --- a/src/main/java/org/rometools/propono/blogclient/metaweblog/MetaWeblogConnection.java +++ b/src/main/java/org/rometools/propono/blogclient/metaweblog/MetaWeblogConnection.java @@ -78,9 +78,10 @@ public class MetaWeblogConnection implements BlogConnection { final Map blogMap = new HashMap(); final Object[] results = (Object[]) getXmlRpcClient().execute("blogger.getUsersBlogs", new Object[] { appkey, userName, password }); for (final Object result : results) { - final Map blog = (Map) result; - final String blogid = blog.get("blogid"); - final String name = blog.get("blogName"); + @SuppressWarnings("unchecked") + final Map blog = (Map) 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; diff --git a/src/main/java/org/rometools/propono/blogclient/metaweblog/MetaWeblogEntry.java b/src/main/java/org/rometools/propono/blogclient/metaweblog/MetaWeblogEntry.java index 46ef3f6..4fde98d 100644 --- a/src/main/java/org/rometools/propono/blogclient/metaweblog/MetaWeblogEntry.java +++ b/src/main/java/org/rometools/propono/blogclient/metaweblog/MetaWeblogEntry.java @@ -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 entryMap) { super(blog); id = (String) entryMap.get("postid"); diff --git a/src/main/java/org/rometools/propono/blogclient/metaweblog/MetaWeblogResource.java b/src/main/java/org/rometools/propono/blogclient/metaweblog/MetaWeblogResource.java index 6ff97d2..95aaafe 100644 --- a/src/main/java/org/rometools/propono/blogclient/metaweblog/MetaWeblogResource.java +++ b/src/main/java/org/rometools/propono/blogclient/metaweblog/MetaWeblogResource.java @@ -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()); this.blog = blog; this.name = name; this.contentType = contentType; diff --git a/src/test/java/org/rometools/propono/blogclient/SimpleBlogClientTest.java b/src/test/java/org/rometools/propono/blogclient/SimpleBlogClientTest.java index 787165c..9b9ebec 100644 --- a/src/test/java/org/rometools/propono/blogclient/SimpleBlogClientTest.java +++ b/src/test/java/org/rometools/propono/blogclient/SimpleBlogClientTest.java @@ -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 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); } + }