|
@@ -19,16 +19,23 @@
|
|
|
package org.hswebframework.web.authorization.oauth2.client.simple.provider;
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.alibaba.fastjson.parser.Feature;
|
|
|
import org.hswebframework.web.authorization.Authentication;
|
|
|
import org.hswebframework.web.authorization.builder.AuthenticationBuilderFactory;
|
|
|
import org.hswebframework.web.authorization.oauth2.client.exception.OAuth2RequestException;
|
|
|
import org.hswebframework.web.authorization.oauth2.client.request.definition.ResponseConvertForProviderDefinition;
|
|
|
import org.hswebframework.web.authorization.oauth2.client.response.OAuth2Response;
|
|
|
+import org.hswebframework.web.controller.message.ResponseMessage;
|
|
|
import org.hswebframework.web.oauth2.core.ErrorType;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
+import java.util.Collection;
|
|
|
import java.util.List;
|
|
|
+import java.util.function.Function;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* @author zhouhao
|
|
@@ -37,30 +44,97 @@ public class HswebResponseConvertSupport implements ResponseConvertForProviderDe
|
|
|
|
|
|
private AuthenticationBuilderFactory authenticationBuilderFactory;
|
|
|
|
|
|
+ private static int responseMessageFieldSize = 4;
|
|
|
+
|
|
|
+ Function<Object,Authentication> autzParser=obj-> convertAuthentication(JSON.toJSONString(obj));
|
|
|
+
|
|
|
+
|
|
|
public HswebResponseConvertSupport(AuthenticationBuilderFactory authenticationBuilderFactory) {
|
|
|
this.authenticationBuilderFactory = authenticationBuilderFactory;
|
|
|
}
|
|
|
|
|
|
+ public Object tryConvertToObject(String json, Class type) {
|
|
|
+ if (json.startsWith("{")) {
|
|
|
+ JSONObject message = JSON.parseObject(json, Feature.DisableFieldSmartMatch);
|
|
|
+ //判断是否响应的为ResponseMessage
|
|
|
+ if(message.size()<=responseMessageFieldSize
|
|
|
+ &&message.get("status")!=null&&message.get("timestamp")!=null){
|
|
|
+
|
|
|
+ Object data = message.get("result");
|
|
|
+ if(data==null){
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ //返回的是对象
|
|
|
+ if (data instanceof JSONObject) {
|
|
|
+ if (type == Authentication.class) {
|
|
|
+ return autzParser.apply(data);
|
|
|
+ }
|
|
|
+ return ((JSONObject) data).toJavaObject(type);
|
|
|
+ }
|
|
|
+ //返回的是集合
|
|
|
+ if (data instanceof JSONArray) {
|
|
|
+ if (type == Authentication.class) {
|
|
|
+ return ((JSONArray) data).stream().map(autzParser).collect(Collectors.toList());
|
|
|
+ }
|
|
|
+ return ((JSONArray) data).toJavaList(type);
|
|
|
+ }
|
|
|
+ return data;
|
|
|
+ }
|
|
|
+ return message.toJavaObject(type);
|
|
|
+ } else if (json.startsWith("[")) {
|
|
|
+ if (type == Authentication.class) {
|
|
|
+ return (JSON.parseArray(json)).stream().map(autzParser).collect(Collectors.toList());
|
|
|
+ }
|
|
|
+ return JSON.parseArray(json, type);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ protected <T> T convertAuthentication(String json) {
|
|
|
+ if (authenticationBuilderFactory != null) {
|
|
|
+ return (T) authenticationBuilderFactory.create().json(json).build();
|
|
|
+ } else {
|
|
|
+ throw new UnsupportedOperationException("authenticationBuilderFactory not ready");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
@Override
|
|
|
public <T> T convert(OAuth2Response response, Class<T> type) {
|
|
|
String json = response.asString();
|
|
|
- if (response.status() != 200) {
|
|
|
- throw new OAuth2RequestException(ErrorType.OTHER, response);
|
|
|
+
|
|
|
+ Object data = tryConvertToObject(json, type);
|
|
|
+ if(null==data)return null;
|
|
|
+ if (type.isInstance(data)) {
|
|
|
+ //success
|
|
|
+ return ((T) data);
|
|
|
}
|
|
|
- if (type == Authentication.class) {
|
|
|
- if (authenticationBuilderFactory != null) {
|
|
|
- return (T) authenticationBuilderFactory.create().json(json).build();
|
|
|
- } else {
|
|
|
- throw new UnsupportedOperationException("authenticationBuilderFactory not ready");
|
|
|
- }
|
|
|
+
|
|
|
+ if (data instanceof ResponseMessage) {
|
|
|
+ //maybe error
|
|
|
+ throw new OAuth2RequestException(((ResponseMessage) data).getMessage(),ErrorType.SERVICE_ERROR, response);
|
|
|
}
|
|
|
- return JSON.parseObject(json, type);
|
|
|
+
|
|
|
+ throw new OAuth2RequestException(ErrorType.PARSE_RESPONSE_ERROR, response);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
+ @SuppressWarnings("all")
|
|
|
public <T> List<T> convertList(OAuth2Response response, Class<T> type) {
|
|
|
String json = response.asString();
|
|
|
- return JSON.parseArray(json, type);
|
|
|
+
|
|
|
+ Object data = tryConvertToObject(json, type);
|
|
|
+ if(null==data)return null;
|
|
|
+ if (data instanceof List) {
|
|
|
+ //success
|
|
|
+ return ((List) data);
|
|
|
+ }
|
|
|
+ if (data instanceof ResponseMessage) {
|
|
|
+ //maybe error
|
|
|
+ throw new OAuth2RequestException(((ResponseMessage) data).getMessage(),ErrorType.SERVICE_ERROR, response);
|
|
|
+ }
|
|
|
+
|
|
|
+ throw new OAuth2RequestException(ErrorType.PARSE_RESPONSE_ERROR, response);
|
|
|
}
|
|
|
|
|
|
@Override
|