struts-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lukaszlen...@apache.org
Subject [07/14] struts git commit: Adds default converter to handle converstion of internal file representation to File
Date Fri, 25 Nov 2016 18:18:02 GMT
Adds default converter to handle converstion of internal file representation to File


Project: http://git-wip-us.apache.org/repos/asf/struts/repo
Commit: http://git-wip-us.apache.org/repos/asf/struts/commit/3fd90966
Tree: http://git-wip-us.apache.org/repos/asf/struts/tree/3fd90966
Diff: http://git-wip-us.apache.org/repos/asf/struts/diff/3fd90966

Branch: refs/heads/master
Commit: 3fd909668af694214500c7b3b02054a5ea7f4bb1
Parents: e867934
Author: Lukasz Lenart <lukaszlenart@apache.org>
Authored: Tue Nov 22 09:41:13 2016 +0100
Committer: Lukasz Lenart <lukaszlenart@apache.org>
Committed: Tue Nov 22 09:41:13 2016 +0100

----------------------------------------------------------------------
 .../conversion/UploadedFileConverter.java       | 50 ++++++++++++++++++++
 .../xwork-default-conversion.properties         |  1 +
 2 files changed, 51 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/struts/blob/3fd90966/core/src/main/java/org/apache/struts2/conversion/UploadedFileConverter.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/struts2/conversion/UploadedFileConverter.java b/core/src/main/java/org/apache/struts2/conversion/UploadedFileConverter.java
new file mode 100644
index 0000000..15b0d85
--- /dev/null
+++ b/core/src/main/java/org/apache/struts2/conversion/UploadedFileConverter.java
@@ -0,0 +1,50 @@
+/*
+ * 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.conversion;
+
+import com.opensymphony.xwork2.conversion.impl.DefaultTypeConverter;
+import org.apache.struts2.dispatcher.multipart.UploadedFile;
+
+import java.io.File;
+import java.lang.reflect.Array;
+import java.lang.reflect.Member;
+import java.util.Map;
+
+public class UploadedFileConverter extends DefaultTypeConverter {
+
+    @Override
+    public Object convertValue(Map<String, Object> context, Object target, Member member,
String propertyName, Object value, Class toType) {
+        if (File.class.equals(toType)) {
+            if (value.getClass().isArray() && Array.getLength(value) == 1) {
+                Object obj = Array.get(value, 0);
+                if (obj instanceof UploadedFile) {
+                    UploadedFile file = (UploadedFile) obj;
+                    if (file.getContent() instanceof File) {
+                        return file.getContent();
+                    }
+                    return new File(file.getAbsolutePath());
+                }
+            }
+        }
+
+        return super.convertValue(context, target, member, propertyName, value, toType);
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/struts/blob/3fd90966/core/src/main/resources/xwork-default-conversion.properties
----------------------------------------------------------------------
diff --git a/core/src/main/resources/xwork-default-conversion.properties b/core/src/main/resources/xwork-default-conversion.properties
new file mode 100644
index 0000000..43d0fe9
--- /dev/null
+++ b/core/src/main/resources/xwork-default-conversion.properties
@@ -0,0 +1 @@
+java.io.File=org.apache.struts2.conversion.UploadedFileConverter


Mime
View raw message