mirror of
https://github.com/apache/struts.git
synced 2026-08-07 15:46:57 +00:00
Adds an abstract layer to allow easily handle API change
This commit is contained in:
+44
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
+12
-6
@@ -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;
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
+12
-6
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user