Adds an abstract layer to allow easily handle API change
Project: http://git-wip-us.apache.org/repos/asf/struts/repo Commit: http://git-wip-us.apache.org/repos/asf/struts/commit/2776b34b Tree: http://git-wip-us.apache.org/repos/asf/struts/tree/2776b34b Diff: http://git-wip-us.apache.org/repos/asf/struts/diff/2776b34b Branch: refs/heads/support-2-3 Commit: 2776b34b44808f2a4ce2c6fee3c2f3a586bc55b0 Parents: 287e3bc Author: Lukasz Lenart <lukaszlen...@apache.org> Authored: Wed Aug 2 14:58:15 2017 +0200 Committer: Lukasz Lenart <lukaszlen...@apache.org> Committed: Thu Aug 24 08:53:57 2017 +0200 ---------------------------------------------------------------------- .../handler/AbstractContentTypeHandler.java | 44 ++++++++++++++++++++ .../rest/handler/FormUrlEncodedHandler.java | 18 +++++--- .../struts2/rest/handler/HtmlHandler.java | 8 ++-- .../struts2/rest/handler/JacksonLibHandler.java | 20 ++++----- .../struts2/rest/handler/JsonLibHandler.java | 7 ++-- .../rest/handler/MultipartFormDataHandler.java | 18 +++++--- .../struts2/rest/handler/XStreamHandler.java | 7 ++-- 7 files changed, 91 insertions(+), 31 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/struts/blob/2776b34b/plugins/rest/src/main/java/org/apache/struts2/rest/handler/AbstractContentTypeHandler.java ---------------------------------------------------------------------- diff --git a/plugins/rest/src/main/java/org/apache/struts2/rest/handler/AbstractContentTypeHandler.java b/plugins/rest/src/main/java/org/apache/struts2/rest/handler/AbstractContentTypeHandler.java new file mode 100644 index 0000000..ae9b3b8 --- /dev/null +++ b/plugins/rest/src/main/java/org/apache/struts2/rest/handler/AbstractContentTypeHandler.java @@ -0,0 +1,44 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.struts2.rest.handler; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.io.IOException; +import java.io.Reader; +import java.io.Writer; + +abstract public class AbstractContentTypeHandler implements ContentTypeHandler { + + private static final Logger LOG = LogManager.getLogger(AbstractContentTypeHandler.class); + + @Override + public void toObject(Reader in, Object target) throws IOException { + LOG.warn("This method is deprecated!"); + } + + @Override + public String fromObject(Object obj, String resultCode, Writer stream) throws IOException { + LOG.warn("This method is deprecated!"); + return null; + } + +} http://git-wip-us.apache.org/repos/asf/struts/blob/2776b34b/plugins/rest/src/main/java/org/apache/struts2/rest/handler/FormUrlEncodedHandler.java ---------------------------------------------------------------------- diff --git a/plugins/rest/src/main/java/org/apache/struts2/rest/handler/FormUrlEncodedHandler.java b/plugins/rest/src/main/java/org/apache/struts2/rest/handler/FormUrlEncodedHandler.java index 17ea005..30168c7 100644 --- a/plugins/rest/src/main/java/org/apache/struts2/rest/handler/FormUrlEncodedHandler.java +++ b/plugins/rest/src/main/java/org/apache/struts2/rest/handler/FormUrlEncodedHandler.java @@ -20,6 +20,8 @@ */ package org.apache.struts2.rest.handler; +import com.opensymphony.xwork2.ActionInvocation; + import java.io.Writer; import java.io.IOException; import java.io.Reader; @@ -34,21 +36,25 @@ import java.io.Reader; * {@link http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4} * */ -public class FormUrlEncodedHandler implements ContentTypeHandler { +public class FormUrlEncodedHandler extends AbstractContentTypeHandler { public static final String CONTENT_TYPE = "application/x-www-form-urlencoded"; - public String fromObject(Object obj, String resultCode, Writer out) throws IOException { + public String fromObject(ActionInvocation invocation, Object obj, String resultCode, Writer out) throws IOException { throw new IOException("Conversion from Object to '"+getContentType()+"' is not supported"); } - /** No transformation is required as the framework handles this data */ - public void toObject(Reader in, Object target) { + /** + * No transformation is required as the framework handles this data + * + * @param in The input stream, usually the body of the request + * @param target The target, usually the action class + */ + public void toObject(ActionInvocation invocation, Reader in, Object target) { } /** - * The extension is not used by this handler - * @return + * @return The extension is not used by this handler */ public String getExtension() { return null; http://git-wip-us.apache.org/repos/asf/struts/blob/2776b34b/plugins/rest/src/main/java/org/apache/struts2/rest/handler/HtmlHandler.java ---------------------------------------------------------------------- diff --git a/plugins/rest/src/main/java/org/apache/struts2/rest/handler/HtmlHandler.java b/plugins/rest/src/main/java/org/apache/struts2/rest/handler/HtmlHandler.java index 32161fc..94709e1 100644 --- a/plugins/rest/src/main/java/org/apache/struts2/rest/handler/HtmlHandler.java +++ b/plugins/rest/src/main/java/org/apache/struts2/rest/handler/HtmlHandler.java @@ -21,6 +21,8 @@ package org.apache.struts2.rest.handler; +import com.opensymphony.xwork2.ActionInvocation; + import java.io.IOException; import java.io.Reader; import java.io.Writer; @@ -28,13 +30,13 @@ import java.io.Writer; /** * Handles HTML content, usually just a simple passthrough to the framework */ -public class HtmlHandler implements ContentTypeHandler { +public class HtmlHandler extends AbstractContentTypeHandler { - public String fromObject(Object obj, String resultCode, Writer out) throws IOException { + public String fromObject(ActionInvocation invocation, Object obj, String resultCode, Writer out) throws IOException { return resultCode; } - public void toObject(Reader in, Object target) { + public void toObject(ActionInvocation invocation, Reader in, Object target) { } public String getExtension() { http://git-wip-us.apache.org/repos/asf/struts/blob/2776b34b/plugins/rest/src/main/java/org/apache/struts2/rest/handler/JacksonLibHandler.java ---------------------------------------------------------------------- diff --git a/plugins/rest/src/main/java/org/apache/struts2/rest/handler/JacksonLibHandler.java b/plugins/rest/src/main/java/org/apache/struts2/rest/handler/JacksonLibHandler.java index 95822d1..dd9dee3 100644 --- a/plugins/rest/src/main/java/org/apache/struts2/rest/handler/JacksonLibHandler.java +++ b/plugins/rest/src/main/java/org/apache/struts2/rest/handler/JacksonLibHandler.java @@ -21,11 +21,12 @@ package org.apache.struts2.rest.handler; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.ObjectReader; +import com.fasterxml.jackson.databind.SerializationFeature; +import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.inject.Inject; import org.apache.struts2.StrutsConstants; -import org.codehaus.jackson.map.ObjectMapper; -import org.codehaus.jackson.map.ObjectReader; -import org.codehaus.jackson.map.SerializationConfig.Feature; import java.io.IOException; import java.io.Reader; @@ -34,21 +35,20 @@ import java.io.Writer; /** * Handles JSON content using jackson-lib */ -public class JacksonLibHandler implements ContentTypeHandler { +public class JacksonLibHandler extends AbstractContentTypeHandler { private static final String DEFAULT_CONTENT_TYPE = "application/json"; private String defaultEncoding = "ISO-8859-1"; private ObjectMapper mapper = new ObjectMapper(); - public void toObject(Reader in, Object target) throws IOException { - - mapper.configure(Feature.WRITE_NULL_MAP_VALUES, false); + public void toObject(ActionInvocation invocation, Reader in, Object target) throws IOException { + mapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false); ObjectReader or = mapper.readerForUpdating(target); - or.readValue(in); //, new TypeReference<clazz>); + or.readValue(in); } - public String fromObject(Object obj, String resultCode, Writer stream) throws IOException { - mapper.configure(Feature.WRITE_NULL_MAP_VALUES, false); + public String fromObject(ActionInvocation invocation, Object obj, String resultCode, Writer stream) throws IOException { + mapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false); mapper.writeValue(stream, obj); return null; } http://git-wip-us.apache.org/repos/asf/struts/blob/2776b34b/plugins/rest/src/main/java/org/apache/struts2/rest/handler/JsonLibHandler.java ---------------------------------------------------------------------- diff --git a/plugins/rest/src/main/java/org/apache/struts2/rest/handler/JsonLibHandler.java b/plugins/rest/src/main/java/org/apache/struts2/rest/handler/JsonLibHandler.java index 5bc0749..4bd96c7 100644 --- a/plugins/rest/src/main/java/org/apache/struts2/rest/handler/JsonLibHandler.java +++ b/plugins/rest/src/main/java/org/apache/struts2/rest/handler/JsonLibHandler.java @@ -26,6 +26,7 @@ import java.io.Reader; import java.io.Writer; import java.util.Collection; +import com.opensymphony.xwork2.ActionInvocation; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import net.sf.json.JsonConfig; @@ -37,12 +38,12 @@ import com.opensymphony.xwork2.inject.Inject; /** * Handles JSON content using json-lib */ -public class JsonLibHandler implements ContentTypeHandler { +public class JsonLibHandler extends AbstractContentTypeHandler { private static final String DEFAULT_CONTENT_TYPE = "application/json"; private String defaultEncoding = "ISO-8859-1"; - public void toObject(Reader in, Object target) throws IOException { + public void toObject(ActionInvocation invocation, Reader in, Object target) throws IOException { StringBuilder sb = new StringBuilder(); char[] buffer = new char[1024]; int len = 0; @@ -63,7 +64,7 @@ public class JsonLibHandler implements ContentTypeHandler { } } - public String fromObject(Object obj, String resultCode, Writer stream) throws IOException { + public String fromObject(ActionInvocation invocation, Object obj, String resultCode, Writer stream) throws IOException { if (obj != null) { if (isArray(obj)) { JSONArray jsonArray = JSONArray.fromObject(obj); http://git-wip-us.apache.org/repos/asf/struts/blob/2776b34b/plugins/rest/src/main/java/org/apache/struts2/rest/handler/MultipartFormDataHandler.java ---------------------------------------------------------------------- diff --git a/plugins/rest/src/main/java/org/apache/struts2/rest/handler/MultipartFormDataHandler.java b/plugins/rest/src/main/java/org/apache/struts2/rest/handler/MultipartFormDataHandler.java index a49cc2b..356b0e6 100644 --- a/plugins/rest/src/main/java/org/apache/struts2/rest/handler/MultipartFormDataHandler.java +++ b/plugins/rest/src/main/java/org/apache/struts2/rest/handler/MultipartFormDataHandler.java @@ -20,6 +20,8 @@ */ package org.apache.struts2.rest.handler; +import com.opensymphony.xwork2.ActionInvocation; + import java.io.Writer; import java.io.IOException; import java.io.Reader; @@ -35,21 +37,25 @@ import java.io.Reader; * {@link http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4} * */ -public class MultipartFormDataHandler implements ContentTypeHandler { +public class MultipartFormDataHandler extends AbstractContentTypeHandler { public static final String CONTENT_TYPE = "multipart/form-data"; - public String fromObject(Object obj, String resultCode, Writer out) throws IOException { + public String fromObject(ActionInvocation invocation, Object obj, String resultCode, Writer out) throws IOException { throw new IOException("Conversion from Object to '"+getContentType()+"' is not supported"); } - /** No transformation is required as the framework handles this data */ - public void toObject(Reader in, Object target) { + /** + * No transformation is required as the framework handles this data + * + * @param in The input stream, usually the body of the request + * @param target The target, usually the action class + */ + public void toObject(ActionInvocation invocation, Reader in, Object target) { } /** - * The extension is not used by this handler - * @return + * @return The extension is not used by this handler */ public String getExtension() { return null; http://git-wip-us.apache.org/repos/asf/struts/blob/2776b34b/plugins/rest/src/main/java/org/apache/struts2/rest/handler/XStreamHandler.java ---------------------------------------------------------------------- diff --git a/plugins/rest/src/main/java/org/apache/struts2/rest/handler/XStreamHandler.java b/plugins/rest/src/main/java/org/apache/struts2/rest/handler/XStreamHandler.java index 94be7af..5650702 100644 --- a/plugins/rest/src/main/java/org/apache/struts2/rest/handler/XStreamHandler.java +++ b/plugins/rest/src/main/java/org/apache/struts2/rest/handler/XStreamHandler.java @@ -21,6 +21,7 @@ package org.apache.struts2.rest.handler; +import com.opensymphony.xwork2.ActionInvocation; import com.thoughtworks.xstream.XStream; import java.io.IOException; @@ -30,9 +31,9 @@ import java.io.Writer; /** * Handles XML content */ -public class XStreamHandler implements ContentTypeHandler { +public class XStreamHandler extends AbstractContentTypeHandler { - public String fromObject(Object obj, String resultCode, Writer out) throws IOException { + public String fromObject(ActionInvocation invocation, Object obj, String resultCode, Writer out) throws IOException { if (obj != null) { XStream xstream = createXStream(); xstream.toXML(obj, out); @@ -40,7 +41,7 @@ public class XStreamHandler implements ContentTypeHandler { return null; } - public void toObject(Reader in, Object target) { + public void toObject(ActionInvocation invocation, Reader in, Object target) { XStream xstream = createXStream(); xstream.fromXML(in, target); }