Skip to content

Commit

Permalink
Merge pull request #300 from nobodyiam/assign-role-result
Browse files Browse the repository at this point in the history
return users assigned and adjust rest template timeout
  • Loading branch information
lepdou committed Jun 27, 2016
2 parents 7891200 + a0a6a43 commit a474d5c
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 26 deletions.
Expand Up @@ -41,6 +41,11 @@ public String getValue(String key) {
return serverConfig == null ? null : serverConfig.getValue();
}

public String getValue(String key, String defaultValue) {
String value = getValue(key);
return value == null ? defaultValue : value;
}

String getDataCenter() {
return Foundation.server().getDataCenter();
}
Expand Down

This file was deleted.

Expand Up @@ -5,7 +5,6 @@
import org.springframework.beans.factory.annotation.Autowired;
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.portal.service.ServiceLocator;

Expand Down
@@ -1,8 +1,8 @@
package com.ctrip.framework.apollo.common.auth;
package com.ctrip.framework.apollo.portal.api;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collection;
import com.google.common.io.BaseEncoding;

import com.ctrip.framework.apollo.portal.service.ServerConfigService;

import org.apache.http.Header;
import org.apache.http.auth.AuthScope;
Expand All @@ -19,13 +19,18 @@
import org.springframework.stereotype.Component;
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
public class RestTemplateFactory implements FactoryBean<RestTemplate>, InitializingBean {
@Autowired
private HttpMessageConverters httpMessageConverters;

@Autowired
private ServerConfigService serverConfigService;

private RestTemplate restTemplate;

public RestTemplate getObject() {
Expand Down Expand Up @@ -54,7 +59,24 @@ public void afterPropertiesSet() throws UnsupportedEncodingException {
.setDefaultHeaders(defaultHeaders).build();

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);
}


}
Expand Up @@ -12,6 +12,8 @@
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
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.web.client.RestTemplate;

Expand All @@ -31,13 +33,32 @@ public class CtripUserService implements UserService {

public CtripUserService(ServerConfigService serverConfigService) {
this.serverConfigService = serverConfigService;
this.restTemplate = new RestTemplate();
this.restTemplate = new RestTemplate(clientHttpRequestFactory());
this.searchUserMatchFields =
Lists.newArrayList("empcode", "empaccount", "displayname", "c_name", "pinyin");
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
public List<UserInfo> searchUsers(String keyword, int offset, int limit) {
UserServiceRequest request = assembleSearchUserRequest(keyword, offset, limit);
Expand Down
Expand Up @@ -87,9 +87,12 @@ public Role createRoleWithPermissions(Role role, Set<Long> permissionIds) {

/**
* Assign role to users
*
* @return the users assigned roles
*/
@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);
Preconditions.checkState(role != null, "Role %s doesn't exist!", roleName);

Expand All @@ -110,6 +113,7 @@ public void assignRoleToUsers(String roleName, Set<String> userIds, String opera
});

userRoleRepository.save(toCreate);
return toAssignUserIds;
}

/**
Expand Down
Expand Up @@ -28,4 +28,9 @@ public String getValue(String key) {
return serverConfig == null ? null : serverConfig.getValue();
}

public String getValue(String key, String defaultValue) {
String value = getValue(key);
return value == null ? defaultValue : value;
}

}

0 comments on commit a474d5c

Please sign in to comment.