diff --git a/.gitignore b/.gitignore
index d911719..c7f1c49 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,5 +1,8 @@
-**/*.iml
-.idea/
-**/target/**/*
-rop-client-js/**/*
-
+**/*.iml
+.idea/
+**/target/**/*
+rop-client-js/**/*
+**/.project
+**/.classpath
+.project
+**/.settings/**/*
diff --git a/pom.xml b/pom.xml
new file mode 100644
index 0000000..211dff1
--- /dev/null
+++ b/pom.xml
@@ -0,0 +1,106 @@
+
+
+ * 每个请求对应一个ClientRequest对象 + *+ * + * @author 陈雄华 + * @version 1.0 + */ +public interface ClientRequest { + + /** + * 设置http请求头信息 + * @param name + * @param value + * @return ClientRequest + */ + ClientRequest setHeader(String name, String value); + + /** + * 添加请求参数,默认需要签名,如果类已经标注了{@link com.rop.annotation.IgnoreSign}则始终不加入签名 + * @param paramName + * @param paramValue + * @return ClientRequest + */ + ClientRequest addParam(String paramName,Object paramValue); + + /** + * 添加请求参数,如果needSign=false表示不参于签名 + * @param paramName + * @param paramValue + * @param needSign + * @return ClientRequest + */ + ClientRequest addParam(String paramName,Object paramValue,boolean needSign); + + /** + * 清除参数列表 + * @return ClientRequest + */ + ClientRequest clearParam(); + + /** + * 使用POST发起请求 + * @param ropResponseClass + * @param methodName + * @param version + * @param
- * 客户端的响应,如果{@link #isSuccessful()}返回true,则调用{@link #getErrorResponse()},反之,则应该
- * 调用{@link #getSuccessResponse(Class)}
- *
- *
- * @author 陈雄华
- * @version 1.0
- */
-public interface CompositeResponse
+ * 客户端的响应,如果{@link #isSuccessful()}返回true,则调用{@link #getErrorResponse()},反之,则应该
+ * 调用{@link #getSuccessResponse(Class)}
+ *
+ *
+ * @author 陈雄华
+ * @version 1.0
+ */
+public interface CompositeResponse- * 功能说明: - *- * - * @author 陈雄华 - * @version 1.0 - */ -public class DefaultCompositeResponse
+ * 功能说明: + *+ * + * @author 陈雄华 + * @version 1.0 + */ +public class DefaultCompositeResponse
+ * 功能说明: + *+ * + * @author 陈雄华 + * @version 1.0 + */ +public class DefaultRopClient implements RopClient { + + protected final Logger logger = LoggerFactory.getLogger(getClass()); + + //服务地址 + private String serverUrl; + + //应用键 + private String appKey; + + private String sessionId; + + private String appKeyName = SystemParameterNames.getAppKey(); + + private String sessionIdName = SystemParameterNames.getSessionId(); + + private String methodName = SystemParameterNames.getMethod(); + + private String versionName = SystemParameterNames.getVersion(); + + private String formatName = SystemParameterNames.getFormat(); + + private String localeName = SystemParameterNames.getLocale(); + + private String signName = SystemParameterNames.getSign(); + + private SignHandler signHandler; + + //报文格式 + private MessageFormat messageFormat = MessageFormat.JSON; + + private Locale locale = Locale.SIMPLIFIED_CHINESE; + + private HttpClient httpClient = new JdkHttpClient(); + + private RopUnmarshaller xmlUnmarshaller; + + private RopUnmarshaller jsonUnmarshaller; + + private RopMarshaller xmlMarshaller; + + private RopMarshaller jsonMarshaller; + + //请求类所有请求参数 + private Map
- * 功能说明: - *- * - * @author 陈雄华 - * @version 1.0 - */ -public interface RopClient { - - /** - * 添加自定义的转换器 - * - * @param ropConverter - */ - void addRopConvertor(RopConverter ropConverter); - - /** - * 设置method系统参数的参数名,下同 - * - * @param paramName - * @return - */ - RopClient setAppKeyParamName(String paramName); - - /** - * 设置sessionId的参数名 - * - * @param paramName - * @return - */ - RopClient setSessionIdParamName(String paramName); - - /** - * 设置method的参数名 - * - * @param paramName - * @return - */ - RopClient setMethodParamName(String paramName); - - /** - * 设置version的参数名 - * - * @param paramName - * @return - */ - RopClient setVersionParamName(String paramName); - - /** - * 设置format的参数名 - * - * @param paramName - * @return - */ - RopClient setFormatParamName(String paramName); - - /** - * 设置locale的参数名 - * - * @param paramName - * @return - */ - RopClient setLocaleParamName(String paramName); - - /** - * 设置sign的参数名 - * - * @param paramName - * @return - */ - RopClient setSignParamName(String paramName); - - /** - * 设置sessionId - * - * @param sessionId - */ - void setSessionId(String sessionId); - - /** - * 创建一个新的服务请求 - * @return - */ - ClientRequest buildClientRequest(); -} - +/* + * Copyright 2012-2017 the original author or authors. + * + * 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 + * 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 com.rop.client; + +import com.rop.converter.RopConverter; +import com.rop.sign.SignHandler; + +/** + *
+ * 功能说明: + *+ * + * @author 陈雄华 + * @version 1.0 + */ +public interface RopClient { + + /** + * 添加自定义的转换器 + * + * @param ropConverter + */ + void addRopConvertor(RopConverter
+ * 功能说明: + *+ * + * @author 陈雄华 + * @version 1.0 + */ +public class DefaultRopClientTest { + + private RopClient ropClient = new DefaultRopClient("http://localhost:8088/router", "00001"); + + @Test + public void testPostWithSession() throws Exception { + ropClient.setSignHandler(new DigestSignHandler("SHA-1", "abcdeabcdeabcdeabcdeabcde")); + ClientRequest request = ropClient.buildClientRequest(); + CompositeResponse
+ * ROP的异常。 + *+ * + * @author 陈雄华 + * @version 1.0 + */ +public class RopException extends RuntimeException { + private static final long serialVersionUID = -5980800274047243722L; + + public RopException() { + } + + public RopException(String message) { + super(message); + } + + public RopException(String message, Throwable cause) { + super(message, cause); + } + + public RopException(Throwable cause) { + super(cause); + } +} + diff --git a/rop-core/src/main/java/com/rop/RopMarshaller.java b/rop-core/src/main/java/com/rop/RopMarshaller.java new file mode 100644 index 0000000..6795b92 --- /dev/null +++ b/rop-core/src/main/java/com/rop/RopMarshaller.java @@ -0,0 +1,32 @@ +/* + * Copyright 2012-2016 the original author or authors. + * + * 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 + * 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 com.rop; + +import java.io.IOException; +import java.io.OutputStream; + +/** + *
+ * 负责将请求方法返回的响应对应流化为相应格式的内容。 + *+ * + * @author 陈雄华 + * @version 1.0 + */ +public interface RopMarshaller { + void marshaller(Object object, OutputStream outputStream) throws IOException; +} + diff --git a/rop-core/src/main/java/com/rop/RopUnmarshaller.java b/rop-core/src/main/java/com/rop/RopUnmarshaller.java new file mode 100644 index 0000000..b949df0 --- /dev/null +++ b/rop-core/src/main/java/com/rop/RopUnmarshaller.java @@ -0,0 +1,38 @@ +/* + * Copyright 2012-2016 the original author or authors. + * + * 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 + * 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 com.rop; + +/** + *
+ * 对响应进行反流化 + *+ * + * @author 陈雄华 + * @version 1.0 + */ +public interface RopUnmarshaller { + + /** + * 将字符串反序列化为相应的对象 + * + * @param content + * @param objectType + * @param
+ * 如果标注在请求类的属性上,则表示该属性无需进行签名,如下所示:
+ * 请求对象({@link com.rop.RopRequest})中不需要签名校验的属性(默认都要签名)。
+ *
+ *
+ * @author 陈雄华
+ * @version 1.0
+ */
+@Target({ElementType.FIELD,ElementType.TYPE})
+@Retention(RetentionPolicy.RUNTIME)
+@Documented
+public @interface IgnoreSign {
+}
diff --git a/rop-core/src/main/java/com/rop/annotation/IgnoreSignType.java b/rop-core/src/main/java/com/rop/annotation/IgnoreSignType.java
new file mode 100644
index 0000000..fc51c32
--- /dev/null
+++ b/rop-core/src/main/java/com/rop/annotation/IgnoreSignType.java
@@ -0,0 +1,38 @@
+/*
+ * Copyright 2012-2016 the original author or authors.
+ *
+ * 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
+ * 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 com.rop.annotation;
+
+/**
+ *
+ * 是否需求进行签名校验.{@link #DEFAULT}是系统预留的,请不要在实际中使用
+ *
+ *
+ * @author 陈雄华
+ * @version 1.0
+ */
+public enum IgnoreSignType {
+
+ YES, NO, DEFAULT;
+
+ public static boolean isIgnoreSign(IgnoreSignType type) {
+ if (NO == type || DEFAULT == type) {
+ return false;
+ } else {
+ return true;
+ }
+ }
+}
+
diff --git a/rop/src/main/java/com/rop/annotation/Temporary.java b/rop-core/src/main/java/com/rop/annotation/Temporary.java
similarity index 50%
rename from rop/src/main/java/com/rop/annotation/Temporary.java
rename to rop-core/src/main/java/com/rop/annotation/Temporary.java
index f5f8cb9..af1d883 100644
--- a/rop/src/main/java/com/rop/annotation/Temporary.java
+++ b/rop-core/src/main/java/com/rop/annotation/Temporary.java
@@ -1,29 +1,40 @@
-/**
- * 版权声明: 版权所有 违者必究 2012
- * 日 期:12-6-30
- */
-package com.rop.annotation;
-
-import java.lang.annotation.*;
-
-/**
- * 默认情况下,请求对象的所有field都会作为请求参数提交,如果希望某个field不作为参数提交,可以打上{@Temporary}注解,如下所示:
- * - *public class MyRopRequest implements RopRequest{ - *- *private String field1;- *- *@Temporary- *private String field2;} - * - * - * @author 陈雄华 - * @version 1.0 - */ -@Target({ElementType.FIELD}) -@Retention(RetentionPolicy.RUNTIME) -@Documented -public @interface Temporary { - -} - +/* + * Copyright 2012-2016 the original author or authors. + * + * 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 + * 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 com.rop.annotation; + +import java.lang.annotation.*; + +/** + * 默认情况下,请求对象的所有field都会作为请求参数提交,如果希望某个field不作为参数提交,可以打上{@Temporary}注解,如下所示: + *+ *public class MyRopRequest implements RopRequest{ + *+ *private String field1;+ *+ *@Temporary+ *private String field2;} + * + * + * @author 陈雄华 + * @version 1.0 + */ +@Target({ElementType.FIELD}) +@Retention(RetentionPolicy.RUNTIME) +@Documented +public @interface Temporary { + +} + diff --git a/rop/src/main/java/com/rop/config/SystemParameterNames.java b/rop-core/src/main/java/com/rop/config/SystemParameterNames.java similarity index 69% rename from rop/src/main/java/com/rop/config/SystemParameterNames.java rename to rop-core/src/main/java/com/rop/config/SystemParameterNames.java index 043bb6c..94178b5 100644 --- a/rop/src/main/java/com/rop/config/SystemParameterNames.java +++ b/rop-core/src/main/java/com/rop/config/SystemParameterNames.java @@ -1,119 +1,143 @@ -/** - * 版权声明: 版权所有 违者必究 2012 - * 日 期:12-6-5 - */ -package com.rop.config; - -/** - *- * 系统级参数的名称 - *- * - * @author 陈雄华 - * @version 1.0 - */ -public class SystemParameterNames { - - private static final String JSONP = "callback"; - - //方法的默认参数名 - private static final String METHOD = "method"; - - //格式化默认参数名 - private static final String FORMAT = "format"; - - //本地化默认参数名 - private static final String LOCALE = "locale"; - - //会话id默认参数名 - private static final String SESSION_ID = "sessionId"; - - //应用键的默认参数名 ; - private static final String APP_KEY = "appKey"; - - //服务版本号的默认参数名 - private static final String VERSION = "v"; - - //签名的默认参数名 - private static final String SIGN = "sign"; - - private static String method = METHOD; - - private static String format = FORMAT; - - private static String locale = LOCALE; - private static String sessionId = SESSION_ID; - - private static String appKey = APP_KEY; - - private static String version = VERSION; - - private static String sign = SIGN; - - private static String jsonp = JSONP; - - public static String getMethod() { - return method; - } - - public static void setMethod(String method) { - SystemParameterNames.method = method; - } - - public static String getFormat() { - return format; - } - - public static void setFormat(String format) { - SystemParameterNames.format = format; - } - - public static String getLocale() { - return locale; - } - - public static void setLocale(String locale) { - SystemParameterNames.locale = locale; - } - - public static String getSessionId() { - return sessionId; - } - - public static void setSessionId(String sessionId) { - SystemParameterNames.sessionId = sessionId; - } - - public static String getAppKey() { - return appKey; - } - - public static void setAppKey(String appKey) { - SystemParameterNames.appKey = appKey; - } - - public static String getVersion() { - return version; - } - - public static void setVersion(String version) { - SystemParameterNames.version = version; - } - - public static String getSign() { - return sign; - } - - public static void setSign(String sign) { - SystemParameterNames.sign = sign; - } - - public static String getJsonp() { - return jsonp; - } - - public static void setJsonp(String jsonp) { - SystemParameterNames.jsonp = jsonp; - } -} - +/* + * Copyright 2012-2016 the original author or authors. + * + * 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 + * 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 com.rop.config; + +import java.util.List; + +/** + *+ * 系统级参数的名称 + *+ * + * @author 陈雄华 + * @version 1.0 + */ +public class SystemParameterNames { + + private static final String JSONP = "callback"; + + //方法的默认参数名 + private static final String METHOD = "method"; + + //格式化默认参数名 + private static final String FORMAT = "format"; + + //本地化默认参数名 + private static final String LOCALE = "locale"; + + //会话id默认参数名 + private static final String SESSION_ID = "sessionId"; + + //应用键的默认参数名 ; + private static final String APP_KEY = "appKey"; + + //服务版本号的默认参数名 + private static final String VERSION = "v"; + + //签名的默认参数名 + private static final String SIGN = "sign"; + + private static String method = METHOD; + + private static String format = FORMAT; + + private static String locale = LOCALE; + private static String sessionId = SESSION_ID; + + private static String appKey = APP_KEY; + + private static String version = VERSION; + + private static String sign = SIGN; + + private static String jsonp = JSONP; + + //默认忽略签名的参数列表 + private static ListignoreSignFieldNames; + + public static String getMethod() { + return method; + } + + public static void setMethod(String method) { + SystemParameterNames.method = method; + } + + public static String getFormat() { + return format; + } + + public static void setFormat(String format) { + SystemParameterNames.format = format; + } + + public static String getLocale() { + return locale; + } + + public static void setLocale(String locale) { + SystemParameterNames.locale = locale; + } + + public static String getSessionId() { + return sessionId; + } + + public static void setSessionId(String sessionId) { + SystemParameterNames.sessionId = sessionId; + } + + public static String getAppKey() { + return appKey; + } + + public static void setAppKey(String appKey) { + SystemParameterNames.appKey = appKey; + } + + public static String getVersion() { + return version; + } + + public static void setVersion(String version) { + SystemParameterNames.version = version; + } + + public static String getSign() { + return sign; + } + + public static void setSign(String sign) { + SystemParameterNames.sign = sign; + } + + public static String getJsonp() { + return jsonp; + } + + public static void setJsonp(String jsonp) { + SystemParameterNames.jsonp = jsonp; + } + + public static List getIgnoreSignFieldNames() { + return ignoreSignFieldNames; + } + + public static void setIgnoreSignFieldNames(List ignoreSignFieldNames) { + SystemParameterNames.ignoreSignFieldNames = ignoreSignFieldNames; + } +} + diff --git a/rop-core/src/main/java/com/rop/converter/IllegalUploadFileFormatException.java b/rop-core/src/main/java/com/rop/converter/IllegalUploadFileFormatException.java new file mode 100644 index 0000000..cc5414c --- /dev/null +++ b/rop-core/src/main/java/com/rop/converter/IllegalUploadFileFormatException.java @@ -0,0 +1,46 @@ +/* + * Copyright 2012-2016 the original author or authors. + * + * 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 + * 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 com.rop.converter; + +/** + * + * 上传文件字符串转换时发生错误 + *+ * + * @author 陈雄华 + * @version 1.0 + */ +public class IllegalUploadFileFormatException extends IllegalArgumentException { + + private static final long serialVersionUID = 1L; + + public IllegalUploadFileFormatException() { + super(); + } + + public IllegalUploadFileFormatException(String s) { + super(s); + } + + public IllegalUploadFileFormatException(String message, Throwable cause) { + super(message, cause); + } + + public IllegalUploadFileFormatException(Throwable cause) { + super(cause); + } +} + diff --git a/rop-core/src/main/java/com/rop/converter/RopConverter.java b/rop-core/src/main/java/com/rop/converter/RopConverter.java new file mode 100644 index 0000000..152364b --- /dev/null +++ b/rop-core/src/main/java/com/rop/converter/RopConverter.java @@ -0,0 +1,54 @@ +/* + * Copyright 2012-2016 the original author or authors. + * + * 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 + * 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 com.rop.converter; + +/** + *+ * 功能说明: + *+ * + * @author 陈雄华 + * @version 1.0 + */ +public interface RopConverter{ + + /** + * 将S类型转换成T类型 + * @param source + * @return T + */ + T convert(S source); + + /** + * 将T类型转换成S类型 + * @param paramValue + * @return S + */ + S unconvert(T paramValue); + + /** + * 获取源类型 + * @return Class+ */ + ClassgetSourceClass(); + + /** + * 获取目标类型 + * @return Class+ */ + Class getTargetClass(); +} + diff --git a/rop/src/main/java/com/rop/request/UploadFile.java b/rop-core/src/main/java/com/rop/converter/UploadFile.java similarity index 52% rename from rop/src/main/java/com/rop/request/UploadFile.java rename to rop-core/src/main/java/com/rop/converter/UploadFile.java index 359e7c5..d2e0a95 100644 --- a/rop/src/main/java/com/rop/request/UploadFile.java +++ b/rop-core/src/main/java/com/rop/converter/UploadFile.java @@ -1,60 +1,72 @@ -/** - * 版权声明: 版权所有 违者必究 2012 - * 日 期:12-8-1 - */ -package com.rop.request; - -import com.rop.annotation.IgnoreSign; -import org.springframework.util.FileCopyUtils; - -import java.io.File; -import java.io.IOException; - -/** - * - * 上传的文件 - *- * - * @author 陈雄华 - * @version 1.0 - */ -@IgnoreSign -public class UploadFile { - - private String fileType; - - private byte[] content; - - /** - * 根据文件内容构造 - * - * @param content - */ - public UploadFile(String fileType, byte[] content) { - this.content = content; - this.fileType = fileType; - } - - /** - * 根据文件构造 - * @param file - */ - public UploadFile(File file) { - try { - this.content = FileCopyUtils.copyToByteArray(file); - this.fileType = file.getName().substring(file.getName().lastIndexOf('.')+1); - } catch (IOException e) { - throw new RuntimeException(e); - } - } - - public String getFileType() { - return fileType; - } - - public byte[] getContent() { - return content; - } -} - - +/* + * Copyright 2012-2016 the original author or authors. + * + * 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 + * 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 com.rop.converter; + +import com.rop.annotation.IgnoreSign; + +import java.io.File; +import java.io.IOException; + +import org.apache.commons.io.FileUtils; + +/** + *+ * 上传的文件 + *+ * + * @author 陈雄华 + * @version 1.0 + */ +@IgnoreSign +public class UploadFile { + + private String fileType; + + private byte[] content; + + /** + * 根据文件内容构造 + * + * @param content + */ + public UploadFile(String fileType, byte[] content) { + this.content = content; + this.fileType = fileType; + } + + /** + * 根据文件构造 + * @param file + */ + public UploadFile(File file) { + try { + this.content = FileUtils.readFileToByteArray(file); + this.fileType = file.getName().substring(file.getName().lastIndexOf('.')+1); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + public String getFileType() { + return fileType; + } + + public byte[] getContent() { + return content; + } +} + + diff --git a/rop-core/src/main/java/com/rop/converter/UploadFileConverter.java b/rop-core/src/main/java/com/rop/converter/UploadFileConverter.java new file mode 100644 index 0000000..cd4ef7f --- /dev/null +++ b/rop-core/src/main/java/com/rop/converter/UploadFileConverter.java @@ -0,0 +1,50 @@ +/* + * Copyright 2012-2016 the original author or authors. + * + * 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 + * 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 com.rop.converter; + +/** + *+ * 将以BASE64位编码字符串转换为字节数组的{@link UploadFile}对象 + *+ * + * @author 陈雄华 + * @version 1.0 + */ +public class UploadFileConverter implements RopConverter{ + + + public UploadFile convert(String source) { + String fileType = UploadFileUtils.getFileType(source); + byte[] contentBytes = UploadFileUtils.decode(source); + return new UploadFile(fileType, contentBytes); + } + + + public String unconvert(UploadFile target) { + return UploadFileUtils.encode(target); + } + + + public Class getSourceClass() { + return String.class; + } + + + public Class getTargetClass() { + return UploadFile.class; + } +} + diff --git a/rop/src/main/java/com/rop/request/UploadFileUtils.java b/rop-core/src/main/java/com/rop/converter/UploadFileUtils.java similarity index 72% rename from rop/src/main/java/com/rop/request/UploadFileUtils.java rename to rop-core/src/main/java/com/rop/converter/UploadFileUtils.java index 9c0466e..c0e0e72 100644 --- a/rop/src/main/java/com/rop/request/UploadFileUtils.java +++ b/rop-core/src/main/java/com/rop/converter/UploadFileUtils.java @@ -1,77 +1,88 @@ -/** - * 版权声明: 版权所有 违者必究 2012 - * 日 期:12-8-3 - */ -package com.rop.request; - -import org.apache.commons.codec.binary.Base64; - -/** - * - * Rop的上传文件编码格式为: - * fileType@BASE64编码的文件内容 - *- * - * @author 陈雄华 - * @version 1.0 - */ -public class UploadFileUtils { - - public static final char SPERATOR = '@'; - - /** - * 获取文件的类型 - * - * @param encodeFile - * @return - */ - public static final String getFileType(String encodeFile) { - int speratorIndex = encodeFile.indexOf(SPERATOR); - if (speratorIndex > -1) { - String fileType = encodeFile.substring(0, speratorIndex); - return fileType.toLowerCase(); - } else { - throw new IllegalUploadFileFormatException("文件格式不对,正确格式为:<文件格式>@<文件内容>"); - } - } - - /** - * 获取文件的字节数组 - * - * @param encodeFile - * @return - */ - public static final byte[] decode(String encodeFile) { - int speratorIndex = encodeFile.indexOf(SPERATOR); - if (speratorIndex > -1) { - String content = encodeFile.substring(speratorIndex + 1); - return Base64.decodeBase64(content); - } else { - throw new IllegalUploadFileFormatException("文件格式不对,正确格式为:<文件格式>@<文件内容>"); - } - } - - /** - * 将文件编码为BASE64的字符串 - * - * @param bytes - * @return - */ - public static final String encode(byte[] bytes) { - return Base64.encodeBase64String(bytes); - } - - /** - * 将文件编码为一个字符串 - * @param uploadFile - * @return - */ - public static final String encode(UploadFile uploadFile){ - StringBuilder sb = new StringBuilder(); - sb.append(uploadFile.getFileType()); - sb.append(SPERATOR); - sb.append(encode(uploadFile.getContent())); - return sb.toString(); - } -} - +/* + * Copyright 2012-2016 the original author or authors. + * + * 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 + * 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 com.rop.converter; + +import org.apache.commons.codec.binary.Base64; + +/** + *+ * Rop的上传文件编码格式为: + * fileType@BASE64编码的文件内容 + *+ * + * @author 陈雄华 + * @version 1.0 + */ +public class UploadFileUtils { + + public static final char SPERATOR = '@'; + + /** + * 获取文件的类型 + * + * @param encodeFile + * @return + */ + public static final String getFileType(String encodeFile) { + int speratorIndex = encodeFile.indexOf(SPERATOR); + if (speratorIndex > -1) { + String fileType = encodeFile.substring(0, speratorIndex); + return fileType.toLowerCase(); + } else { + throw new IllegalUploadFileFormatException("文件格式不对,正确格式为:<文件格式>@<文件内容>"); + } + } + + /** + * 获取文件的字节数组 + * + * @param encodeFile + * @return + */ + public static final byte[] decode(String encodeFile) { + int speratorIndex = encodeFile.indexOf(SPERATOR); + if (speratorIndex > -1) { + String content = encodeFile.substring(speratorIndex + 1); + return Base64.decodeBase64(content); + } else { + throw new IllegalUploadFileFormatException("文件格式不对,正确格式为:<文件格式>@<文件内容>"); + } + } + + /** + * 将文件编码为BASE64的字符串 + * + * @param bytes + * @return + */ + public static final String encode(byte[] bytes) { + return Base64.encodeBase64String(bytes); + } + + /** + * 将文件编码为一个字符串 + * @param uploadFile + * @return + */ + public static final String encode(UploadFile uploadFile){ + StringBuilder sb = new StringBuilder(); + sb.append(uploadFile.getFileType()); + sb.append(SPERATOR); + sb.append(encode(uploadFile.getContent())); + return sb.toString(); + } +} + diff --git a/rop-core/src/main/java/com/rop/marshaller/FastjsonRopMarshaller.java b/rop-core/src/main/java/com/rop/marshaller/FastjsonRopMarshaller.java new file mode 100644 index 0000000..1ed7819 --- /dev/null +++ b/rop-core/src/main/java/com/rop/marshaller/FastjsonRopMarshaller.java @@ -0,0 +1,32 @@ +/* + * Copyright 2012-2016 the original author or authors. + * + * 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 + * 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 com.rop.marshaller; + +import java.io.IOException; +import java.io.OutputStream; + +import com.alibaba.fastjson.JSON; +import com.rop.RopMarshaller; + +/** + * 使用fastjson进行json数据转换 + */ +public class FastjsonRopMarshaller implements RopMarshaller { + + public void marshaller(Object object, OutputStream outputStream) throws IOException { + outputStream.write(JSON.toJSONBytes(object)); + } +} diff --git a/rop-core/src/main/java/com/rop/marshaller/JacksonJsonRopMarshaller.java b/rop-core/src/main/java/com/rop/marshaller/JacksonJsonRopMarshaller.java new file mode 100644 index 0000000..61984c1 --- /dev/null +++ b/rop-core/src/main/java/com/rop/marshaller/JacksonJsonRopMarshaller.java @@ -0,0 +1,55 @@ +/* + * Copyright 2012-2016 the original author or authors. + * + * 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 + * 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 com.rop.marshaller; + +import com.fasterxml.jackson.core.JsonEncoding; +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.rop.RopMarshaller; +import com.rop.utils.ObjectMapperUtils; + +import java.io.IOException; +import java.io.OutputStream; + +/** + *+ * 将响应对象流化成JSON。 {@link ObjectMapper}是线程安全的。 + *+ * + * @author 陈雄华 + * @version 1.0 + */ +public class JacksonJsonRopMarshaller implements RopMarshaller { + + private ObjectMapper objectMapper; + + public void marshaller(Object object, OutputStream outputStream) throws IOException { + JsonGenerator jsonGenerator = getObjectMapper().getFactory().createGenerator(outputStream, JsonEncoding.UTF8); + getObjectMapper().writeValue(jsonGenerator, object); + } + + public void setObjectMapper(ObjectMapper objectMapper){ + this.objectMapper = objectMapper; + } + + private ObjectMapper getObjectMapper() throws IOException { + if (objectMapper == null) { + objectMapper = ObjectMapperUtils.getObjectMapper(); + } + return objectMapper; + } +} + diff --git a/rop/src/main/java/com/rop/marshaller/JaxbXmlRopMarshaller.java b/rop-core/src/main/java/com/rop/marshaller/JaxbXmlRopMarshaller.java similarity index 65% rename from rop/src/main/java/com/rop/marshaller/JaxbXmlRopMarshaller.java rename to rop-core/src/main/java/com/rop/marshaller/JaxbXmlRopMarshaller.java index 9d6db3c..338866b 100644 --- a/rop/src/main/java/com/rop/marshaller/JaxbXmlRopMarshaller.java +++ b/rop-core/src/main/java/com/rop/marshaller/JaxbXmlRopMarshaller.java @@ -1,52 +1,63 @@ -/** - * - * 日 期:12-2-27 - */ -package com.rop.marshaller; - -import com.rop.RopException; -import com.rop.RopMarshaller; - -import javax.xml.bind.JAXBContext; -import javax.xml.bind.JAXBException; -import javax.xml.bind.Marshaller; -import java.io.OutputStream; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -/** - *- * 将对象流化成XML,每个类型对应一个{@link JAXBContext},{@link JAXBContext} 是线程安全的,但是 - * {@link Marshaller}是非线程安全的,因此需要每次创建一个。 - *- * - * @author 陈雄华 - * @version 1.0 - */ -public class JaxbXmlRopMarshaller implements RopMarshaller { - - private static MapjaxbContextHashMap = new ConcurrentHashMap (); - - public void marshaller(Object object, OutputStream outputStream) { - try { - Marshaller m = buildMarshaller(object.getClass()); - m.marshal(object, outputStream); - } catch (JAXBException e) { - throw new RopException(e); - } - } - - - private Marshaller buildMarshaller(Class> objectType) throws JAXBException { - if (!jaxbContextHashMap.containsKey(objectType)) { - JAXBContext context = JAXBContext.newInstance(objectType); - jaxbContextHashMap.put(objectType, context); - } - JAXBContext context = jaxbContextHashMap.get(objectType); - Marshaller marshaller = context.createMarshaller(); - marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); - marshaller.setProperty(Marshaller.JAXB_ENCODING, "utf-8"); - return marshaller; - } -} - +/* + * Copyright 2012-2016 the original author or authors. + * + * 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 + * 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 com.rop.marshaller; + +import com.rop.RopException; +import com.rop.RopMarshaller; + +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; +import javax.xml.bind.Marshaller; +import java.io.OutputStream; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +/** + * + * 将对象流化成XML,每个类型对应一个{@link JAXBContext},{@link JAXBContext} 是线程安全的,但是 + * {@link Marshaller}是非线程安全的,因此需要每次创建一个。 + *+ * + * @author 陈雄华 + * @version 1.0 + */ +public class JaxbXmlRopMarshaller implements RopMarshaller { + + private static Map, JAXBContext> jaxbContextHashMap = new ConcurrentHashMap , JAXBContext>(); + + public void marshaller(Object object, OutputStream outputStream) { + try { + Marshaller m = buildMarshaller(object.getClass()); + m.marshal(object, outputStream); + } catch (JAXBException e) { + throw new RopException(e); + } + } + + + private Marshaller buildMarshaller(Class> objectType) throws JAXBException { + if (!jaxbContextHashMap.containsKey(objectType)) { + JAXBContext context = JAXBContext.newInstance(objectType); + jaxbContextHashMap.put(objectType, context); + } + JAXBContext context = jaxbContextHashMap.get(objectType); + Marshaller marshaller = context.createMarshaller(); + marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); + marshaller.setProperty(Marshaller.JAXB_ENCODING, "utf-8"); + return marshaller; + } +} + diff --git a/rop/src/main/java/com/rop/response/CommonRopResponse.java b/rop-core/src/main/java/com/rop/response/CommonRopResponse.java similarity index 56% rename from rop/src/main/java/com/rop/response/CommonRopResponse.java rename to rop-core/src/main/java/com/rop/response/CommonRopResponse.java index e5256b9..3490347 100644 --- a/rop/src/main/java/com/rop/response/CommonRopResponse.java +++ b/rop-core/src/main/java/com/rop/response/CommonRopResponse.java @@ -1,42 +1,53 @@ -/** - * - * 日 期:12-2-22 - */ -package com.rop.response; - - -import javax.xml.bind.annotation.XmlAccessType; -import javax.xml.bind.annotation.XmlAccessorType; -import javax.xml.bind.annotation.XmlAttribute; -import javax.xml.bind.annotation.XmlRootElement; - -/** - * - * 通用的响应对象 - *- * - * @author 陈雄华 - * @version 1.0 - */ -@XmlAccessorType(XmlAccessType.FIELD) -@XmlRootElement(name = "response") -public class CommonRopResponse{ - - @XmlAttribute - private boolean successful = false; - - public static final CommonRopResponse SUCCESSFUL_RESPONSE = new CommonRopResponse(true); - public static final CommonRopResponse FAILURE_RESPONSE = new CommonRopResponse(false); - - public CommonRopResponse() { - } - - private CommonRopResponse(boolean successful) { - this.successful = successful; - } - - public boolean isSuccessful() { - return successful; - } -} - +/* + * Copyright 2012-2016 the original author or authors. + * + * 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 + * 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 com.rop.response; + + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlRootElement; + +/** + *+ * 通用的响应对象 + *+ * + * @author 陈雄华 + * @version 1.0 + */ +@XmlAccessorType(XmlAccessType.FIELD) +@XmlRootElement(name = "response") +public class CommonRopResponse{ + + @XmlAttribute + private boolean successful = false; + + public static final CommonRopResponse SUCCESSFUL_RESPONSE = new CommonRopResponse(true); + public static final CommonRopResponse FAILURE_RESPONSE = new CommonRopResponse(false); + + public CommonRopResponse() { + } + + private CommonRopResponse(boolean successful) { + this.successful = successful; + } + + public boolean isSuccessful() { + return successful; + } +} + diff --git a/rop/src/main/java/com/rop/response/ErrorResponse.java b/rop-core/src/main/java/com/rop/response/ErrorResponse.java similarity index 74% rename from rop/src/main/java/com/rop/response/ErrorResponse.java rename to rop-core/src/main/java/com/rop/response/ErrorResponse.java index ed46a9a..7771e53 100644 --- a/rop/src/main/java/com/rop/response/ErrorResponse.java +++ b/rop-core/src/main/java/com/rop/response/ErrorResponse.java @@ -1,127 +1,128 @@ -/** - * - * 日 期:12-2-10 - */ -package com.rop.response; - -import com.rop.CommonConstant; -import com.rop.security.MainError; -import com.rop.security.MainErrorType; -import com.rop.security.MainErrors; -import com.rop.security.SubError; - -import javax.xml.bind.annotation.*; -import java.util.ArrayList; -import java.util.List; -import java.util.Locale; - -/** - *- * 功能说明: - *- * - * @author 陈雄华 - * @version 1.0 - */ -@XmlAccessorType(XmlAccessType.FIELD) -@XmlRootElement(name = "error") -public class ErrorResponse{ - - @XmlAttribute - protected String errorToken = CommonConstant.ERROR_TOKEN; - - @XmlAttribute - protected String code; - - @XmlElement - protected String message; - - @XmlElement - protected String solution; - - @XmlElementWrapper(name = "subErrors") - @XmlElement(name = "subError") - protected ListsubErrors; - - public ErrorResponse() { - } - - public ErrorResponse(MainError mainError) { - this.code = mainError.getCode(); - this.message = mainError.getMessage(); - this.solution = mainError.getSolution(); - if (mainError.getSubErrors() != null && mainError.getSubErrors().size() > 0) { - this.subErrors = mainError.getSubErrors(); - } - } - - public String getCode() { - return code; - } - - public void setCode(String code) { - this.code = code; - } - - public String getMessage() { - return message; - } - - public void setMessage(String message) { - this.message = message; - } - - public String getSolution() { - return solution; - } - - public void setSolution(String solution) { - this.solution = solution; - } - - public List getSubErrors() { - return subErrors; - } - - public void setSubErrors(List subErrors) { - this.subErrors = subErrors; - } - - public void addSubError(SubError subError){ - if (subErrors == null) { - subErrors = new ArrayList (); - } - subErrors.add(subError); - } - - protected MainError getInvalidArgumentsError(Locale locale) { - return MainErrors.getError(MainErrorType.INVALID_ARGUMENTS, locale); - } - - protected void setMainError(MainError mainError) { - setCode(mainError.getCode()); - setMessage(mainError.getMessage()); - setSolution(mainError.getSolution()); - } - - public String getErrorToken() { - return errorToken; - } - - /** - * 对服务名进行标准化处理:如book.upload转换为book-upload, - * - * @param method - * @return - */ - protected String transform(String method) { - if(method != null){ - method = method.replace(".", "-"); - return method; - }else{ - return "LACK_METHOD"; - } - } - -} - +/* + * Copyright 2012-2017 the original author or authors. + * + * 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 + * 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 com.rop.response; + +import javax.xml.bind.annotation.*; + +import com.rop.CommonConstant; + +import java.util.ArrayList; +import java.util.List; + +/** + * + * 功能说明: + *+ * + * @author 陈雄华 + * @version 1.0 + */ +@XmlAccessorType(XmlAccessType.FIELD) +@XmlRootElement(name = "error") +public class ErrorResponse{ + + @XmlAttribute + protected String errorToken = CommonConstant.ERROR_TOKEN; + + @XmlAttribute + protected String code; + + @XmlElement + protected String message; + + @XmlElement + protected String solution; + + @XmlElementWrapper(name = "subErrors") + @XmlElement(name = "subError") + protected ListsubErrors; + + public ErrorResponse() { + super(); + } + + public ErrorResponse(MainError mainError) { + this.code = mainError.getCode(); + this.message = mainError.getMessage(); + this.solution = mainError.getSolution(); + if (mainError.getSubErrors() != null && mainError.getSubErrors().size() > 0) { + this.subErrors = mainError.getSubErrors(); + } + } + + public String getCode() { + return code; + } + + public void setCode(String code) { + this.code = code; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public String getSolution() { + return solution; + } + + public void setSolution(String solution) { + this.solution = solution; + } + + public List getSubErrors() { + return subErrors; + } + + public void setSubErrors(List subErrors) { + this.subErrors = subErrors; + } + + public void addSubError(SubError subError){ + if (subErrors == null) { + subErrors = new ArrayList (); + } + subErrors.add(subError); + } + + protected void setMainError(MainError mainError) { + setCode(mainError.getCode()); + setMessage(mainError.getMessage()); + setSolution(mainError.getSolution()); + } + + public String getErrorToken() { + return errorToken; + } + + /** + * 对服务名进行标准化处理:如book.upload转换为book-upload, + * + * @param method + * @return + */ + protected String transform(String method) { + if(method != null){ + return method.replace(".", "-"); + }else{ + return "LACK_METHOD"; + } + } +} \ No newline at end of file diff --git a/rop-core/src/main/java/com/rop/response/MainError.java b/rop-core/src/main/java/com/rop/response/MainError.java new file mode 100644 index 0000000..9d743aa --- /dev/null +++ b/rop-core/src/main/java/com/rop/response/MainError.java @@ -0,0 +1,41 @@ +/* + * Copyright 2012-2016 the original author or authors. + * + * 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 + * 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 com.rop.response; + +import java.util.List; + +/** + * + * 功能说明: + *+ * + * @author 陈雄华 + * @version 1.0 + */ +public interface MainError { + + String getCode(); + + String getMessage(); + + String getSolution(); + + ListgetSubErrors(); + + MainError addSubError(SubError subError); + +} + diff --git a/rop/src/main/java/com/rop/security/MainErrorType.java b/rop-core/src/main/java/com/rop/response/MainErrorType.java similarity index 79% rename from rop/src/main/java/com/rop/security/MainErrorType.java rename to rop-core/src/main/java/com/rop/response/MainErrorType.java index 41b1fe2..0d90ae9 100644 --- a/rop/src/main/java/com/rop/security/MainErrorType.java +++ b/rop-core/src/main/java/com/rop/response/MainErrorType.java @@ -1,82 +1,93 @@ -/** - * - * 日 期:12-2-23 - */ -package com.rop.security; - -import java.util.EnumMap; - -/** - * - * 功能说明: - *- * - * @author 陈雄华 - * @version 1.0 - */ -public enum MainErrorType { - SERVICE_CURRENTLY_UNAVAILABLE, - INSUFFICIENT_ISV_PERMISSIONS, - INSUFFICIENT_USER_PERMISSIONS, - UPLOAD_FAIL, - HTTP_ACTION_NOT_ALLOWED, - INVALID_ENCODING, - FORBIDDEN_REQUEST, - METHOD_OBSOLETED, - BUSINESS_LOGIC_ERROR, - MISSING_SESSION, - INVALID_SESSION, - MISSING_APP_KEY, - INVALID_APP_KEY, - MISSING_SIGNATURE, - INVALID_SIGNATURE, - MISSING_METHOD, - INVALID_METHOD, - MISSING_VERSION, - INVALID_VERSION, - UNSUPPORTED_VERSION, - INVALID_FORMAT, - MISSING_REQUIRED_ARGUMENTS, - INVALID_ARGUMENTS, - EXCEED_USER_INVOKE_LIMITED, - EXCEED_SESSION_INVOKE_LIMITED, - EXCEED_APP_INVOKE_LIMITED, - EXCEED_APP_INVOKE_FREQUENCY_LIMITED; - - private static EnumMaperrorCodeMap = new EnumMap (MainErrorType.class); - - static { - errorCodeMap.put(MainErrorType.SERVICE_CURRENTLY_UNAVAILABLE, "1"); - errorCodeMap.put(MainErrorType.INSUFFICIENT_ISV_PERMISSIONS, "2"); - errorCodeMap.put(MainErrorType.INSUFFICIENT_USER_PERMISSIONS, "3"); - errorCodeMap.put(MainErrorType.UPLOAD_FAIL, "4"); - errorCodeMap.put(MainErrorType.HTTP_ACTION_NOT_ALLOWED, "5"); - errorCodeMap.put(MainErrorType.INVALID_ENCODING, "6"); - errorCodeMap.put(MainErrorType.FORBIDDEN_REQUEST, "7"); - errorCodeMap.put(MainErrorType.METHOD_OBSOLETED, "8"); - errorCodeMap.put(MainErrorType.BUSINESS_LOGIC_ERROR, "9"); - errorCodeMap.put(MainErrorType.MISSING_SESSION, "20"); - errorCodeMap.put(MainErrorType.INVALID_SESSION, "21"); - errorCodeMap.put(MainErrorType.MISSING_APP_KEY, "22"); - errorCodeMap.put(MainErrorType.INVALID_APP_KEY, "23"); - errorCodeMap.put(MainErrorType.MISSING_SIGNATURE, "24"); - errorCodeMap.put(MainErrorType.INVALID_SIGNATURE, "25"); - errorCodeMap.put(MainErrorType.MISSING_METHOD, "26"); - errorCodeMap.put(MainErrorType.INVALID_METHOD, "27"); - errorCodeMap.put(MainErrorType.MISSING_VERSION, "28"); - errorCodeMap.put(MainErrorType.INVALID_VERSION, "29"); - errorCodeMap.put(MainErrorType.UNSUPPORTED_VERSION, "30"); - errorCodeMap.put(MainErrorType.INVALID_FORMAT, "31"); - errorCodeMap.put(MainErrorType.MISSING_REQUIRED_ARGUMENTS, "32"); - errorCodeMap.put(MainErrorType.INVALID_ARGUMENTS, "33"); - errorCodeMap.put(MainErrorType.EXCEED_USER_INVOKE_LIMITED, "34"); - errorCodeMap.put(MainErrorType.EXCEED_SESSION_INVOKE_LIMITED, "35"); - errorCodeMap.put(MainErrorType.EXCEED_APP_INVOKE_LIMITED, "36"); - errorCodeMap.put(MainErrorType.EXCEED_APP_INVOKE_FREQUENCY_LIMITED, "37"); - } - - public String value() { - return errorCodeMap.get(this); - } -} - +/* + * Copyright 2012-2016 the original author or authors. + * + * 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 + * 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 com.rop.response; + +import java.util.EnumMap; + +/** + * + * 功能说明: + *+ * + * @author 陈雄华 + * @version 1.0 + */ +public enum MainErrorType { + SERVICE_CURRENTLY_UNAVAILABLE, + INSUFFICIENT_ISV_PERMISSIONS, + INSUFFICIENT_USER_PERMISSIONS, + UPLOAD_FAIL, + HTTP_ACTION_NOT_ALLOWED, + INVALID_ENCODING, + FORBIDDEN_REQUEST, + METHOD_OBSOLETED, + BUSINESS_LOGIC_ERROR, + MISSING_SESSION, + INVALID_SESSION, + MISSING_APP_KEY, + INVALID_APP_KEY, + MISSING_SIGNATURE, + INVALID_SIGNATURE, + MISSING_METHOD, + INVALID_METHOD, + MISSING_VERSION, + INVALID_VERSION, + UNSUPPORTED_VERSION, + INVALID_FORMAT, + MISSING_REQUIRED_ARGUMENTS, + INVALID_ARGUMENTS, + EXCEED_USER_INVOKE_LIMITED, + EXCEED_SESSION_INVOKE_LIMITED, + EXCEED_APP_INVOKE_LIMITED, + EXCEED_APP_INVOKE_FREQUENCY_LIMITED; + + private static EnumMaperrorCodeMap = new EnumMap (MainErrorType.class); + + static { + errorCodeMap.put(MainErrorType.SERVICE_CURRENTLY_UNAVAILABLE, "1"); + errorCodeMap.put(MainErrorType.INSUFFICIENT_ISV_PERMISSIONS, "2"); + errorCodeMap.put(MainErrorType.INSUFFICIENT_USER_PERMISSIONS, "3"); + errorCodeMap.put(MainErrorType.UPLOAD_FAIL, "4"); + errorCodeMap.put(MainErrorType.HTTP_ACTION_NOT_ALLOWED, "5"); + errorCodeMap.put(MainErrorType.INVALID_ENCODING, "6"); + errorCodeMap.put(MainErrorType.FORBIDDEN_REQUEST, "7"); + errorCodeMap.put(MainErrorType.METHOD_OBSOLETED, "8"); + errorCodeMap.put(MainErrorType.BUSINESS_LOGIC_ERROR, "9"); + errorCodeMap.put(MainErrorType.MISSING_SESSION, "20"); + errorCodeMap.put(MainErrorType.INVALID_SESSION, "21"); + errorCodeMap.put(MainErrorType.MISSING_APP_KEY, "22"); + errorCodeMap.put(MainErrorType.INVALID_APP_KEY, "23"); + errorCodeMap.put(MainErrorType.MISSING_SIGNATURE, "24"); + errorCodeMap.put(MainErrorType.INVALID_SIGNATURE, "25"); + errorCodeMap.put(MainErrorType.MISSING_METHOD, "26"); + errorCodeMap.put(MainErrorType.INVALID_METHOD, "27"); + errorCodeMap.put(MainErrorType.MISSING_VERSION, "28"); + errorCodeMap.put(MainErrorType.INVALID_VERSION, "29"); + errorCodeMap.put(MainErrorType.UNSUPPORTED_VERSION, "30"); + errorCodeMap.put(MainErrorType.INVALID_FORMAT, "31"); + errorCodeMap.put(MainErrorType.MISSING_REQUIRED_ARGUMENTS, "32"); + errorCodeMap.put(MainErrorType.INVALID_ARGUMENTS, "33"); + errorCodeMap.put(MainErrorType.EXCEED_USER_INVOKE_LIMITED, "34"); + errorCodeMap.put(MainErrorType.EXCEED_SESSION_INVOKE_LIMITED, "35"); + errorCodeMap.put(MainErrorType.EXCEED_APP_INVOKE_LIMITED, "36"); + errorCodeMap.put(MainErrorType.EXCEED_APP_INVOKE_FREQUENCY_LIMITED, "37"); + } + + public String value() { + return errorCodeMap.get(this); + } +} + diff --git a/rop/src/main/java/com/rop/security/SubError.java b/rop-core/src/main/java/com/rop/response/SubError.java similarity index 51% rename from rop/src/main/java/com/rop/security/SubError.java rename to rop-core/src/main/java/com/rop/response/SubError.java index d509a30..bb8d574 100644 --- a/rop/src/main/java/com/rop/security/SubError.java +++ b/rop-core/src/main/java/com/rop/response/SubError.java @@ -1,52 +1,63 @@ -/** - * - * 日 期:12-2-12 - */ -package com.rop.security; - - -import javax.xml.bind.annotation.*; - -/** - * - * 功能说明: - *- * - * @author 陈雄华 - * @version 1.0 - */ -@XmlAccessorType(XmlAccessType.FIELD) -@XmlRootElement(name = "subError") -public class SubError { - - @XmlAttribute - private String code; - - @XmlElement - private String message; - - public SubError() { - } - - public SubError(String code, String message) { - this.code = code; - this.message = message; - } - - public String getCode() { - return code; - } - - public void setCode(String code) { - this.code = code; - } - - public String getMessage() { - return message; - } - - public void setMessage(String message) { - this.message = message; - } -} - +/* + * Copyright 2012-2016 the original author or authors. + * + * 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 + * 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 com.rop.response; + + +import javax.xml.bind.annotation.*; + +/** + *+ * 功能说明: + *+ * + * @author 陈雄华 + * @version 1.0 + */ +@XmlAccessorType(XmlAccessType.FIELD) +@XmlRootElement(name = "subError") +public class SubError { + + @XmlAttribute + private String code; + + @XmlElement + private String message; + + public SubError() { + } + + public SubError(String code, String message) { + this.code = code; + this.message = message; + } + + public String getCode() { + return code; + } + + public void setCode(String code) { + this.code = code; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } +} + diff --git a/rop/src/main/java/com/rop/security/SubErrorType.java b/rop-core/src/main/java/com/rop/response/SubErrorType.java similarity index 62% rename from rop/src/main/java/com/rop/security/SubErrorType.java rename to rop-core/src/main/java/com/rop/response/SubErrorType.java index cc9c575..56ce765 100644 --- a/rop/src/main/java/com/rop/security/SubErrorType.java +++ b/rop-core/src/main/java/com/rop/response/SubErrorType.java @@ -1,44 +1,55 @@ -/** - * - * 日 期:12-2-23 - */ -package com.rop.security; - -import java.util.EnumMap; - -/** - *- * 功能说明: - *- * - * @author 陈雄华 - * @version 1.0 - */ -public enum SubErrorType { - ISP_SERVICE_UNAVAILABLE, - ISP_SERVICE_TIMEOUT, - - ISV_NOT_EXIST, - ISV_INVALID_PERMISSION, - ISV_MISSING_PARAMETER, - ISV_INVALID_PARAMETE, - ISV_PARAMETERS_MISMATCH; - - private static EnumMaperrorKeyMap = new EnumMap (SubErrorType.class); - - static { - errorKeyMap.put(SubErrorType.ISP_SERVICE_UNAVAILABLE, "isp.xxx-service-unavailable"); - errorKeyMap.put(SubErrorType.ISP_SERVICE_TIMEOUT, "isp.xxx-service-timeout"); - - errorKeyMap.put(SubErrorType.ISV_NOT_EXIST, "isv.xxx-not-exist:invalid-yyy"); - errorKeyMap.put(SubErrorType.ISV_MISSING_PARAMETER, "isv.missing-parameter:xxx"); - errorKeyMap.put(SubErrorType.ISV_INVALID_PARAMETE, "isv.invalid-paramete:xxx"); - errorKeyMap.put(SubErrorType.ISV_INVALID_PERMISSION, "isv.invalid-permission"); - errorKeyMap.put(SubErrorType.ISV_PARAMETERS_MISMATCH, "isv.parameters-mismatch:xxx-and-yyy"); - } - - public String value() { - return errorKeyMap.get(this); - } -} - +/* + * Copyright 2012-2016 the original author or authors. + * + * 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 + * 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 com.rop.response; + +import java.util.EnumMap; + +/** + * + * 功能说明: + *+ * + * @author 陈雄华 + * @version 1.0 + */ +public enum SubErrorType { + ISP_SERVICE_UNAVAILABLE, + ISP_SERVICE_TIMEOUT, + + ISV_NOT_EXIST, + ISV_INVALID_PERMISSION, + ISV_MISSING_PARAMETER, + ISV_INVALID_PARAMETE, + ISV_PARAMETERS_MISMATCH; + + private static EnumMaperrorKeyMap = new EnumMap (SubErrorType.class); + + static { + errorKeyMap.put(SubErrorType.ISP_SERVICE_UNAVAILABLE, "isp.xxx-service-unavailable"); + errorKeyMap.put(SubErrorType.ISP_SERVICE_TIMEOUT, "isp.xxx-service-timeout"); + + errorKeyMap.put(SubErrorType.ISV_NOT_EXIST, "isv.xxx-not-exist:invalid-yyy"); + errorKeyMap.put(SubErrorType.ISV_MISSING_PARAMETER, "isv.missing-parameter:xxx"); + errorKeyMap.put(SubErrorType.ISV_INVALID_PARAMETE, "isv.invalid-paramete:xxx"); + errorKeyMap.put(SubErrorType.ISV_INVALID_PERMISSION, "isv.invalid-permission"); + errorKeyMap.put(SubErrorType.ISV_PARAMETERS_MISMATCH, "isv.parameters-mismatch:xxx-and-yyy"); + } + + public String value() { + return errorKeyMap.get(this); + } +} + diff --git a/rop-core/src/main/java/com/rop/sign/SignHandler.java b/rop-core/src/main/java/com/rop/sign/SignHandler.java new file mode 100644 index 0000000..418fc6a --- /dev/null +++ b/rop-core/src/main/java/com/rop/sign/SignHandler.java @@ -0,0 +1,57 @@ +/** + * Copyright 2012-2017 the original author or authors. + * + * 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 + * 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 com.rop.sign; + +import java.util.Collection; +import java.util.Map; + +/** + * 数字签名处理接口 + * @author liangruisen + */ +public interface SignHandler { + + /** + * 产生签名 + * @param paramMap + * @return 签名字符串 + */ + String sign(Map paramMap); + + /** + * 产生签名 + * @param paramMap + * @return 签名字符串 + */ + String sign(Map paramMap, Collection ignore); + + /** + * 检查签名是否正确 + * @param sign + * @param paramMap + * @param ignore + * @return boolean + */ + boolean signCheck(String sign, Map paramMap, Collection ignore); + + /** + * 检查签名是否正确 + * @param sign + * @param paramMap + * @return boolean + */ + boolean signCheck(String sign, Map paramMap); +} diff --git a/rop-core/src/main/java/com/rop/unmarshaller/FastjsonRopUnmarshaller.java b/rop-core/src/main/java/com/rop/unmarshaller/FastjsonRopUnmarshaller.java new file mode 100644 index 0000000..cc68cd8 --- /dev/null +++ b/rop-core/src/main/java/com/rop/unmarshaller/FastjsonRopUnmarshaller.java @@ -0,0 +1,29 @@ +/* + * Copyright 2012-2016 the original author or authors. + * + * 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 + * 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 com.rop.unmarshaller; + +import com.alibaba.fastjson.JSON; +import com.rop.RopUnmarshaller; + +/** + * 使用fastjson将json数据转换成java对象 + */ +public class FastjsonRopUnmarshaller implements RopUnmarshaller { + + public T unmarshaller(String content, Class objectType) { + return JSON.parseObject(content, objectType); + } +} diff --git a/rop-core/src/main/java/com/rop/unmarshaller/JacksonJsonRopUnmarshaller.java b/rop-core/src/main/java/com/rop/unmarshaller/JacksonJsonRopUnmarshaller.java new file mode 100644 index 0000000..b79db84 --- /dev/null +++ b/rop-core/src/main/java/com/rop/unmarshaller/JacksonJsonRopUnmarshaller.java @@ -0,0 +1,56 @@ +/* + * Copyright 2012-2016 the original author or authors. + * + * 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 + * 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 com.rop.unmarshaller; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.rop.RopException; +import com.rop.RopUnmarshaller; +import com.rop.utils.ObjectMapperUtils; + +import java.io.IOException; + +/** + * + * 功能说明:使用jackson2.x将json数据转换成java对象 + *+ * + * @author 陈雄华 + * @version 1.0 + */ +public class JacksonJsonRopUnmarshaller implements RopUnmarshaller { + + private ObjectMapper objectMapper; + + publicT unmarshaller(String content, Class objectType) { + try { + return getObjectMapper().readValue(content, objectType); + } catch (IOException e) { + throw new RopException(e); + } + } + + private ObjectMapper getObjectMapper() { + if (objectMapper == null) { + objectMapper = ObjectMapperUtils.getObjectMapper(); + } + return objectMapper; + } + + public void setObjectMapper(ObjectMapper objectMapper){ + this.objectMapper = objectMapper; + } +} + diff --git a/rop/src/main/java/com/rop/client/unmarshaller/JaxbXmlRopUnmarshaller.java b/rop-core/src/main/java/com/rop/unmarshaller/JaxbXmlRopUnmarshaller.java similarity index 55% rename from rop/src/main/java/com/rop/client/unmarshaller/JaxbXmlRopUnmarshaller.java rename to rop-core/src/main/java/com/rop/unmarshaller/JaxbXmlRopUnmarshaller.java index ff5ce92..718f2c9 100644 --- a/rop/src/main/java/com/rop/client/unmarshaller/JaxbXmlRopUnmarshaller.java +++ b/rop-core/src/main/java/com/rop/unmarshaller/JaxbXmlRopUnmarshaller.java @@ -1,55 +1,65 @@ -/** - * 版权声明: 版权所有 违者必究 2012 - * 日 期:12-6-30 - */ -package com.rop.client.unmarshaller; - -import com.rop.RopException; -import com.rop.client.RopUnmarshaller; -import org.xml.sax.InputSource; - -import javax.xml.bind.JAXBContext; -import javax.xml.bind.JAXBException; -import javax.xml.bind.Unmarshaller; -import java.io.StringReader; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -/** - * - * 功能说明: - *- * - * @author 陈雄华 - * @version 1.0 - */ -public class JaxbXmlRopUnmarshaller implements RopUnmarshaller { - - private static MapjaxbContextHashMap = new ConcurrentHashMap (); - - - public T unmarshaller(String content, Class objectType) { - try { - Unmarshaller unmarshaller = buildUnmarshaller(objectType); - StringReader reader = new StringReader(content); - new InputSource(reader); - return (T) unmarshaller.unmarshal(reader); - } catch (JAXBException e) { - throw new RopException(e); - } - - } - - private Unmarshaller buildUnmarshaller(Class> objectType) throws JAXBException { - if (!jaxbContextHashMap.containsKey(objectType)) { - JAXBContext context = JAXBContext.newInstance(objectType); - jaxbContextHashMap.put(objectType, context); - } - JAXBContext context = jaxbContextHashMap.get(objectType); - Unmarshaller unmarshaller = context.createUnmarshaller(); -// unmarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); -// unmarshaller.setProperty(Marshaller.JAXB_ENCODING, "utf-8"); - return unmarshaller; - } -} - +/* + * Copyright 2012-2016 the original author or authors. + * + * 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 + * 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 com.rop.unmarshaller; + +import com.rop.RopException; +import com.rop.RopUnmarshaller; + +import org.xml.sax.InputSource; + +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; +import javax.xml.bind.Unmarshaller; + +import java.io.StringReader; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +/** + * + * 功能说明:使用Jaxb将xml数据转换成java对象 + *+ * + * @author 陈雄华 + * @version 1.0 + */ +public class JaxbXmlRopUnmarshaller implements RopUnmarshaller { + + private static Map, JAXBContext> jaxbContextHashMap = new ConcurrentHashMap , JAXBContext>(); + + + public T unmarshaller(String content, Class objectType) { + try { + Unmarshaller unmarshaller = buildUnmarshaller(objectType); + StringReader reader = new StringReader(content); + new InputSource(reader); + return objectType.cast(unmarshaller.unmarshal(reader)); + } catch (JAXBException e) { + throw new RopException(e); + } + } + + private Unmarshaller buildUnmarshaller(Class> objectType) throws JAXBException { + if (!jaxbContextHashMap.containsKey(objectType)) { + JAXBContext context = JAXBContext.newInstance(objectType); + jaxbContextHashMap.put(objectType, context); + } + JAXBContext context = jaxbContextHashMap.get(objectType); + Unmarshaller unmarshaller = context.createUnmarshaller(); + return unmarshaller; + } +} + diff --git a/rop-core/src/main/java/com/rop/utils/AnnotationUtils.java b/rop-core/src/main/java/com/rop/utils/AnnotationUtils.java new file mode 100644 index 0000000..8789120 --- /dev/null +++ b/rop-core/src/main/java/com/rop/utils/AnnotationUtils.java @@ -0,0 +1,191 @@ +/* + * Copyright 2012-2016 the original author or authors. + * + * 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 + * 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 com.rop.utils; + +import java.lang.annotation.Annotation; +import java.lang.reflect.AnnotatedElement; +import java.lang.reflect.Method; +import java.util.HashSet; +import java.util.Set; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * General utility methods for working with annotations, handling bridge methods + * (which the compiler generates for generic declarations) as well as super methods + * (for optional "annotation inheritance"). Note that none of this is + * provided by the JDK's introspection facilities themselves. + * + * As a general rule for runtime-retained annotations (e.g. for transaction + * control, authorization, or service exposure), always use the lookup methods + * on this class (e.g., {@link #findAnnotation(Method, Class)}, + * {@link #getAnnotation(Method, Class)}, and {@link #getAnnotations(Method)}) + * instead of the plain annotation lookup methods in the JDK. You can still + * explicitly choose between a get lookup on the given class level only + * ({@link #getAnnotation(Method, Class)}) and a find lookup in the entire + * inheritance hierarchy of the given method ({@link #findAnnotation(Method, Class)}). + * + * @author Rob Harrop + * @author Juergen Hoeller + * @author Sam Brannen + * @author Mark Fisher + * @author Chris Beams + * @author Phillip Webb + * @since 2.0 + * @see java.lang.reflect.Method#getAnnotations() + * @see java.lang.reflect.Method#getAnnotation(Class) + */ +public abstract class AnnotationUtils { + + private static transient Logger logger; + + /** + * Find a single {@link Annotation} of {@code annotationType} on the + * supplied {@link Class}, traversing its interfaces, annotations, and + * superclasses if the annotation is not present on the given class + * itself. + *
This method explicitly handles class-level annotations which are not + * declared as {@link java.lang.annotation.Inherited inherited} as well + * as meta-annotations and annotations on interfaces. + *
The algorithm operates as follows: + *
+ *
+ *- Search for the annotation on the given class and return it if found. + *
- Recursively search through all interfaces that the given class declares. + *
- Recursively search through all annotations that the given class declares. + *
- Recursively search through the superclass hierarchy of the given class. + *
Note: in this context, the term recursively means that the search + * process continues by returning to step #1 with the current interface, + * annotation, or superclass as the class to look for annotations on. + * @param clazz the class to look for annotations on + * @param annotationType the type of annotation to look for + * @return the annotation if found, or {@code null} if not found + */ + public static A findAnnotation(Class> clazz, Class annotationType) { + return findAnnotation(clazz, annotationType, new HashSet
()); + } + + /** + * Perform the search algorithm for {@link #findAnnotation(Class, Class)}, + * avoiding endless recursion by tracking which annotations have already + * been visited. + * @param clazz the class to look for annotations on + * @param annotationType the type of annotation to look for + * @param visited the set of annotations that have already been visited + * @return the annotation if found, or {@code null} if not found + */ + private static A findAnnotation(Class> clazz, Class annotationType, Set visited) { + Assert.notNull(clazz, "Class must not be null"); + + if (isAnnotationDeclaredLocally(annotationType, clazz)) { + try { + return clazz.getAnnotation(annotationType); + } + catch (Exception ex) { + // Assuming nested Class values not resolvable within annotation attributes... + logIntrospectionFailure(clazz, ex); + return null; + } + } + + for (Class> ifc : clazz.getInterfaces()) { + A annotation = findAnnotation(ifc, annotationType, visited); + if (annotation != null) { + return annotation; + } + } + + try { + for (Annotation ann : clazz.getDeclaredAnnotations()) { + if (!isInJavaLangAnnotationPackage(ann) && visited.add(ann)) { + A annotation = findAnnotation(ann.annotationType(), annotationType, visited); + if (annotation != null) { + return annotation; + } + } + } + } + catch (Exception ex) { + // Assuming nested Class values not resolvable within annotation attributes... + logIntrospectionFailure(clazz, ex); + return null; + } + + Class> superclass = clazz.getSuperclass(); + if (superclass == null || superclass.equals(Object.class)) { + return null; + } + return findAnnotation(superclass, annotationType, visited); + } + + /** + * Determine whether an annotation for the specified {@code annotationType} is + * declared locally on the supplied {@code clazz}. The supplied {@link Class} + * may represent any type. + * Note: This method does not determine if the annotation is + * {@linkplain java.lang.annotation.Inherited inherited}. For greater clarity + * regarding inherited annotations, consider using + * {@link #isAnnotationInherited(Class, Class)} instead. + * @param annotationType the Class object corresponding to the annotation type + * @param clazz the Class object corresponding to the class on which to check for the annotation + * @return {@code true} if an annotation for the specified {@code annotationType} + * is declared locally on the supplied {@code clazz} + * @see Class#getDeclaredAnnotations() + * @see #isAnnotationInherited(Class, Class) + */ + public static boolean isAnnotationDeclaredLocally(Class extends Annotation> annotationType, Class> clazz) { + Assert.notNull(annotationType, "Annotation type must not be null"); + Assert.notNull(clazz, "Class must not be null"); + boolean declaredLocally = false; + try { + for (Annotation ann : clazz.getDeclaredAnnotations()) { + if (ann.annotationType().equals(annotationType)) { + declaredLocally = true; + break; + } + } + } + catch (Exception ex) { + // Assuming nested Class values not resolvable within annotation attributes... + logIntrospectionFailure(clazz, ex); + } + return declaredLocally; + } + + /** + * Determine if the supplied {@link Annotation} is defined in the core JDK + * {@code java.lang.annotation} package. + * @param annotation the annotation to check (never {@code null}) + * @return {@code true} if the annotation is in the {@code java.lang.annotation} package + */ + public static boolean isInJavaLangAnnotationPackage(Annotation annotation) { + Assert.notNull(annotation, "Annotation must not be null"); + return annotation.annotationType().getName().startsWith("java.lang.annotation"); + } + + private static void logIntrospectionFailure(AnnotatedElement annotatedElement, Exception ex) { + Logger loggerToUse = logger; + if (loggerToUse == null) { + loggerToUse = LoggerFactory.getLogger(AnnotationUtils.class); + logger = loggerToUse; + } + if (loggerToUse.isInfoEnabled()) { + loggerToUse.info("Failed to introspect annotations on [" + annotatedElement + "]: " + ex); + } + } +} + diff --git a/rop-core/src/main/java/com/rop/utils/Assert.java b/rop-core/src/main/java/com/rop/utils/Assert.java new file mode 100644 index 0000000..f79a893 --- /dev/null +++ b/rop-core/src/main/java/com/rop/utils/Assert.java @@ -0,0 +1,145 @@ +/* + * Copyright 2012-2016 the original author or authors. + * + * 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 + * 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 com.rop.utils; + +import java.util.Collection; + +public abstract class Assert { + + /** + * Assert a boolean expression, throwing {@code IllegalArgumentException} + * if the test result is {@code false}. + *
Assert.isTrue(i > 0, "The value must be greater than zero");+ * @param expression a boolean expression + * @param message the exception message to use if the assertion fails + * @throws IllegalArgumentException if expression is {@code false} + */ + public static void isTrue(boolean expression, String message) { + if (!expression) { + throw new IllegalArgumentException(message); + } + } + + /** + * Assert a boolean expression, throwing {@code IllegalArgumentException} + * if the test result is {@code false}. + *Assert.isTrue(i > 0);+ * @param expression a boolean expression + * @throws IllegalArgumentException if expression is {@code false} + */ + public static void isTrue(boolean expression) { + isTrue(expression, "[Assertion failed] - this expression must be true"); + } + + /** + * Assert that an object is {@code null} . + *Assert.isNull(value, "The value must be null");+ * @param object the object to check + * @param message the exception message to use if the assertion fails + * @throws IllegalArgumentException if the object is not {@code null} + */ + public static void isNull(Object object, String message) { + if (object != null) { + throw new IllegalArgumentException(message); + } + } + + /** + * Assert that an object is {@code null} . + *Assert.isNull(value);+ * @param object the object to check + * @throws IllegalArgumentException if the object is not {@code null} + */ + public static void isNull(Object object) { + isNull(object, "[Assertion failed] - the object argument must be null"); + } + + /** + * Assert that an object is not {@code null} . + *Assert.notNull(clazz, "The class must not be null");+ * @param object the object to check + * @param message the exception message to use if the assertion fails + * @throws IllegalArgumentException if the object is {@code null} + */ + public static void notNull(Object object, String message) { + if (object == null) { + throw new IllegalArgumentException(message); + } + } + + /** + * Assert that an object is not {@code null} . + *Assert.notNull(clazz);+ * @param object the object to check + * @throws IllegalArgumentException if the object is {@code null} + */ + public static void notNull(Object object) { + notNull(object, "[Assertion failed] - this argument is required; it must not be null"); + } + + + /** + * Assert that an array has elements; that is, it must not be + * {@code null} and must have at least one element. + *Assert.notEmpty(array, "The array must have elements");+ * @param array the array to check + * @param message the exception message to use if the assertion fails + * @throws IllegalArgumentException if the object array is {@code null} or has no elements + */ + public static void notEmpty(Object[] array, String message) { + if (array == null || array.length <= 0) { + throw new IllegalArgumentException(message); + } + } + + /** + * Assert that an array has elements; that is, it must not be + * {@code null} and must have at least one element. + *Assert.notEmpty(array);+ * @param array the array to check + * @throws IllegalArgumentException if the object array is {@code null} or has no elements + */ + public static void notEmpty(Object[] array) { + notEmpty(array, "[Assertion failed] - this array must not be empty: it must contain at least 1 element"); + } + + /** + * Assert that a collection has elements; that is, it must not be + * {@code null} and must have at least one element. + *Assert.notEmpty(collection, "Collection must have elements");+ * @param collection the collection to check + * @param message the exception message to use if the assertion fails + * @throws IllegalArgumentException if the collection is {@code null} or has no elements + */ + public static void notEmpty(Collection> collection, String message) { + if (collection == null || collection.size() <= 0) { + throw new IllegalArgumentException(message); + } + } + + /** + * Assert that a collection has elements; that is, it must not be + * {@code null} and must have at least one element. + *Assert.notEmpty(collection, "Collection must have elements");+ * @param collection the collection to check + * @throws IllegalArgumentException if the collection is {@code null} or has no elements + */ + public static void notEmpty(Collection> collection) { + notEmpty(collection, + "[Assertion failed] - this collection must not be empty: it must contain at least 1 element"); + } + +} diff --git a/rop-core/src/main/java/com/rop/utils/ObjectMapperUtils.java b/rop-core/src/main/java/com/rop/utils/ObjectMapperUtils.java new file mode 100644 index 0000000..725e011 --- /dev/null +++ b/rop-core/src/main/java/com/rop/utils/ObjectMapperUtils.java @@ -0,0 +1,84 @@ +/* + * Copyright 2012-2016 the original author or authors. + * + * 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 + * 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 com.rop.utils; + +import java.io.IOException; +import java.text.SimpleDateFormat; +import java.util.TimeZone; + +import org.apache.commons.lang.StringEscapeUtils; + +import com.fasterxml.jackson.annotation.JsonInclude.Include; +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.JsonSerializer; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializerProvider; +import com.fasterxml.jackson.databind.module.SimpleModule; +import com.fasterxml.jackson.databind.type.TypeFactory; +import com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector; + +public class ObjectMapperUtils { + + private static ObjectMapper objectMapper; + + public static ObjectMapper getObjectMapper(){ + if(objectMapper == null){ + synchronized (ObjectMapperUtils.class) { + if(objectMapper == null){ + objectMapper = createObjectMapper(); + } + } + } + return objectMapper; + } + + private static ObjectMapper createObjectMapper() { + ObjectMapper objectMapper = new ObjectMapper(); + objectMapper.setAnnotationIntrospector(new JaxbAnnotationIntrospector(TypeFactory.defaultInstance(), true)); + objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:sss")); + objectMapper.setSerializationInclusion(Include.NON_EMPTY); + objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); + // 空值处理为空串 + objectMapper.getSerializerProvider().setNullValueSerializer( + new JsonSerializer