Commit a474d5ca by 张乐 Committed by GitHub

Merge pull request #300 from nobodyiam/assign-role-result

return users assigned and adjust rest template timeout
parents 78912008 a0a6a43b
...@@ -41,6 +41,11 @@ public class ServerConfigService { ...@@ -41,6 +41,11 @@ public class ServerConfigService {
return serverConfig == null ? null : serverConfig.getValue(); return serverConfig == null ? null : serverConfig.getValue();
} }
public String getValue(String key, String defaultValue) {
String value = getValue(key);
return value == null ? defaultValue : value;
}
String getDataCenter() { String getDataCenter() {
return Foundation.server().getDataCenter(); return Foundation.server().getDataCenter();
} }
......
package com.ctrip.framework.apollo.common.auth;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
@Target({ElementType.PARAMETER, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@AuthenticationPrincipal
public @interface ActiveUser {
}
...@@ -5,7 +5,6 @@ import javax.annotation.PostConstruct; ...@@ -5,7 +5,6 @@ import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import com.ctrip.framework.apollo.common.auth.RestTemplateFactory;
import com.ctrip.framework.apollo.core.enums.Env; import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.portal.service.ServiceLocator; import com.ctrip.framework.apollo.portal.service.ServiceLocator;
......
package com.ctrip.framework.apollo.common.auth; package com.ctrip.framework.apollo.portal.api;
import java.io.UnsupportedEncodingException; import com.google.common.io.BaseEncoding;
import java.util.ArrayList;
import java.util.Collection; import com.ctrip.framework.apollo.portal.service.ServerConfigService;
import org.apache.http.Header; import org.apache.http.Header;
import org.apache.http.auth.AuthScope; import org.apache.http.auth.AuthScope;
...@@ -19,13 +19,18 @@ import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; ...@@ -19,13 +19,18 @@ import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import com.google.common.io.BaseEncoding; import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collection;
@Component @Component
public class RestTemplateFactory implements FactoryBean<RestTemplate>, InitializingBean { public class RestTemplateFactory implements FactoryBean<RestTemplate>, InitializingBean {
@Autowired @Autowired
private HttpMessageConverters httpMessageConverters; private HttpMessageConverters httpMessageConverters;
@Autowired
private ServerConfigService serverConfigService;
private RestTemplate restTemplate; private RestTemplate restTemplate;
public RestTemplate getObject() { public RestTemplate getObject() {
...@@ -54,7 +59,24 @@ public class RestTemplateFactory implements FactoryBean<RestTemplate>, Initializ ...@@ -54,7 +59,24 @@ public class RestTemplateFactory implements FactoryBean<RestTemplate>, Initializ
.setDefaultHeaders(defaultHeaders).build(); .setDefaultHeaders(defaultHeaders).build();
restTemplate = new RestTemplate(httpMessageConverters.getConverters()); restTemplate = new RestTemplate(httpMessageConverters.getConverters());
restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory(httpClient)); HttpComponentsClientHttpRequestFactory requestFactory =
new HttpComponentsClientHttpRequestFactory(httpClient);
requestFactory.setConnectTimeout(getConnectTimeout());
requestFactory.setReadTimeout(getReadTimeout());
restTemplate.setRequestFactory(requestFactory);
}
private int getConnectTimeout() {
String connectTimeout = serverConfigService.getValue("api.connectTimeout", "3000");
return Integer.parseInt(connectTimeout);
} }
private int getReadTimeout() {
String readTimeout = serverConfigService.getValue("api.readTimeout", "10000");
return Integer.parseInt(readTimeout);
}
} }
...@@ -12,6 +12,8 @@ import org.springframework.core.ParameterizedTypeReference; ...@@ -12,6 +12,8 @@ import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity; import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod; import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
...@@ -31,13 +33,32 @@ public class CtripUserService implements UserService { ...@@ -31,13 +33,32 @@ public class CtripUserService implements UserService {
public CtripUserService(ServerConfigService serverConfigService) { public CtripUserService(ServerConfigService serverConfigService) {
this.serverConfigService = serverConfigService; this.serverConfigService = serverConfigService;
this.restTemplate = new RestTemplate(); this.restTemplate = new RestTemplate(clientHttpRequestFactory());
this.searchUserMatchFields = this.searchUserMatchFields =
Lists.newArrayList("empcode", "empaccount", "displayname", "c_name", "pinyin"); Lists.newArrayList("empcode", "empaccount", "displayname", "c_name", "pinyin");
this.responseType = new ParameterizedTypeReference<Map<String, List<UserServiceResponse>>>() { this.responseType = new ParameterizedTypeReference<Map<String, List<UserServiceResponse>>>() {
}; };
} }
private ClientHttpRequestFactory clientHttpRequestFactory() {
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
factory.setConnectTimeout(getConnectTimeout());
factory.setReadTimeout(getReadTimeout());
return factory;
}
private int getConnectTimeout() {
String connectTimeout = serverConfigService.getValue("api.connectTimeout", "3000");
return Integer.parseInt(connectTimeout);
}
private int getReadTimeout() {
String readTimeout = serverConfigService.getValue("api.readTimeout", "3000");
return Integer.parseInt(readTimeout);
}
@Override @Override
public List<UserInfo> searchUsers(String keyword, int offset, int limit) { public List<UserInfo> searchUsers(String keyword, int offset, int limit) {
UserServiceRequest request = assembleSearchUserRequest(keyword, offset, limit); UserServiceRequest request = assembleSearchUserRequest(keyword, offset, limit);
......
...@@ -87,9 +87,12 @@ public class RolePermissionService implements InitializingBean { ...@@ -87,9 +87,12 @@ public class RolePermissionService implements InitializingBean {
/** /**
* Assign role to users * Assign role to users
*
* @return the users assigned roles
*/ */
@Transactional @Transactional
public void assignRoleToUsers(String roleName, Set<String> userIds, String operatorUserId) { public Set<String> assignRoleToUsers(String roleName, Set<String> userIds,
String operatorUserId) {
Role role = findRoleByRoleName(roleName); Role role = findRoleByRoleName(roleName);
Preconditions.checkState(role != null, "Role %s doesn't exist!", roleName); Preconditions.checkState(role != null, "Role %s doesn't exist!", roleName);
...@@ -110,6 +113,7 @@ public class RolePermissionService implements InitializingBean { ...@@ -110,6 +113,7 @@ public class RolePermissionService implements InitializingBean {
}); });
userRoleRepository.save(toCreate); userRoleRepository.save(toCreate);
return toAssignUserIds;
} }
/** /**
......
...@@ -28,4 +28,9 @@ public class ServerConfigService { ...@@ -28,4 +28,9 @@ public class ServerConfigService {
return serverConfig == null ? null : serverConfig.getValue(); return serverConfig == null ? null : serverConfig.getValue();
} }
public String getValue(String key, String defaultValue) {
String value = getValue(key);
return value == null ? defaultValue : value;
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment