Alfresco basic authentication
@Component
public class RestTemplateFactory implements FactoryBean<RestTemplate>, InitializingBean {
private RestTemplate restTemplate;
public RestTemplateFactory() {
super();
}
// API
@Override
public RestTemplate getObject() {
return restTemplate;
}
@Override
public Class<RestTemplate> getObjectType() {
return RestTemplate.class;
}
@Override
public boolean isSingleton() {
return true;
}
@Override
public void afterPropertiesSet() {
HttpHost host = new HttpHost("localhost", 8080, "http");
final ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactoryBasicAuth(host);
restTemplate = new RestTemplate(requestFactory);
restTemplate.getInterceptors().add(new BasicAuthenticationInterceptor("admin", "admin"));
}
}
----------------------------------------------------------------------------------------------------------------------
public class HttpComponentsClientHttpRequestFactoryBasicAuth extends HttpComponentsClientHttpRequestFactory {
HttpHost host;
public HttpComponentsClientHttpRequestFactoryBasicAuth(HttpHost host) {
super();
this.host = host;
}
protected HttpContext createHttpContext(HttpMethod httpMethod, URI uri) {
return createHttpContext();
}
private HttpContext createHttpContext() {
AuthCache authCache = new BasicAuthCache();
BasicScheme basicAuth = new BasicScheme();
authCache.put(host, basicAuth);
BasicHttpContext localcontext = new BasicHttpContext();
localcontext.setAttribute(HttpClientContext.AUTH_CACHE, authCache);
return localcontext;
}
}
------------------------------------------------------------------------------------------------------------------
@Service
public class SearchServiceImpl implements SearchService {
private static final String ALFRESCO_SERVICE_API_PEOPLE_FILTER = "http://localhost:8080/alfresco/service/api/people?filter=";
@Autowired
private RestTemplateFactory restTemplateFactory;
@Override
public String searchPeople(String userName) {
RestTemplate restTemplate = restTemplateFactory.getObject();
String resp = restTemplate.getForObject(ALFRESCO_SERVICE_API_PEOPLE_FILTER+userName, String.class);
return resp ;
}
public RestTemplateFactory getRestTemplateFactory() {
return restTemplateFactory;
}
public void setRestTemplateFactory(RestTemplateFactory restTemplateFactory) {
this.restTemplateFactory = restTemplateFactory;
}
}
public class RestTemplateFactory implements FactoryBean<RestTemplate>, InitializingBean {
private RestTemplate restTemplate;
public RestTemplateFactory() {
super();
}
// API
@Override
public RestTemplate getObject() {
return restTemplate;
}
@Override
public Class<RestTemplate> getObjectType() {
return RestTemplate.class;
}
@Override
public boolean isSingleton() {
return true;
}
@Override
public void afterPropertiesSet() {
HttpHost host = new HttpHost("localhost", 8080, "http");
final ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactoryBasicAuth(host);
restTemplate = new RestTemplate(requestFactory);
restTemplate.getInterceptors().add(new BasicAuthenticationInterceptor("admin", "admin"));
}
}
----------------------------------------------------------------------------------------------------------------------
public class HttpComponentsClientHttpRequestFactoryBasicAuth extends HttpComponentsClientHttpRequestFactory {
HttpHost host;
public HttpComponentsClientHttpRequestFactoryBasicAuth(HttpHost host) {
super();
this.host = host;
}
protected HttpContext createHttpContext(HttpMethod httpMethod, URI uri) {
return createHttpContext();
}
private HttpContext createHttpContext() {
AuthCache authCache = new BasicAuthCache();
BasicScheme basicAuth = new BasicScheme();
authCache.put(host, basicAuth);
BasicHttpContext localcontext = new BasicHttpContext();
localcontext.setAttribute(HttpClientContext.AUTH_CACHE, authCache);
return localcontext;
}
}
------------------------------------------------------------------------------------------------------------------
@Service
public class SearchServiceImpl implements SearchService {
private static final String ALFRESCO_SERVICE_API_PEOPLE_FILTER = "http://localhost:8080/alfresco/service/api/people?filter=";
@Autowired
private RestTemplateFactory restTemplateFactory;
@Override
public String searchPeople(String userName) {
RestTemplate restTemplate = restTemplateFactory.getObject();
String resp = restTemplate.getForObject(ALFRESCO_SERVICE_API_PEOPLE_FILTER+userName, String.class);
return resp ;
}
public RestTemplateFactory getRestTemplateFactory() {
return restTemplateFactory;
}
public void setRestTemplateFactory(RestTemplateFactory restTemplateFactory) {
this.restTemplateFactory = restTemplateFactory;
}
}
Comments
Post a Comment