Skip to content
This repository was archived by the owner on Nov 17, 2020. It is now read-only.

Commit 3af121d

Browse files
author
Libor Krzyzanek
committed
redesigned how posts are pushed to DCP. It uses shared httpclient and also uses infinispan cache for managing what to push. #ORG-3011
1 parent 756542e commit 3af121d

9 files changed

+494
-245
lines changed

.openshift/config/standalone.xml

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
<system-properties>
3838
<property name="org.apache.coyote.http11.Http11Protocol.COMPRESSION" value="on"/>
3939
<property name="borg.updatefeed.workers.count" value="5" />
40+
<property name="borg.sync.dcp.workers.count" value="5" />
4041
</system-properties>
4142

4243
<management>
@@ -224,6 +225,7 @@
224225
<subsystem xmlns="urn:jboss:domain:infinispan:1.4">
225226
<cache-container name="borg">
226227
<local-cache name="sync-feeds"/>
228+
<local-cache name="sync-feeds-to-dcp" />
227229
</cache-container>
228230
<cache-container name="cluster" aliases="ha-partition"
229231
default-cache="default">

pom.xml

+3-1
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,9 @@
226226
<dependency>
227227
<groupId>org.apache.httpcomponents</groupId>
228228
<artifactId>httpclient</artifactId>
229-
<version>4.2.1</version>
229+
<!-- EAP 6.4 -->
230+
<!-- docs: http://hc.apache.org/httpcomponents-client-4.3.x/index.html -->
231+
<version>4.3.6</version>
230232
</dependency>
231233

232234
<dependency>

src/main/java/org/jboss/planet/service/FeedsService.java

+1-6
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import javax.persistence.NoResultException;
2121

2222
import org.apache.http.client.ClientProtocolException;
23-
import org.apache.http.impl.client.DefaultHttpClient;
2423
import org.jboss.planet.model.FeedGroup;
2524
import org.jboss.planet.model.FeedsSecurityRole;
2625
import org.jboss.planet.model.Post;
@@ -138,15 +137,11 @@ public void deleteFeed(String feedName) throws ClientProtocolException, IOExcept
138137
// Check permissions before deleting anything on Sync server.
139138
securityService.checkPermission(f, CRUDOperationType.DELETE);
140139

141-
DefaultHttpClient httpClient = jbossSyncService.createHttpClient();
142-
143140
List<Post> posts = f.getPosts();
144141
for (Post post : posts) {
145-
jbossSyncService.deletePost(post.getTitleAsId(), httpClient);
142+
jbossSyncService.deletePost(post.getTitleAsId());
146143
}
147144

148-
jbossSyncService.shutdownHttpClient(httpClient);
149-
150145
delete(f.getId());
151146
}
152147

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
/*
2+
* JBoss, Home of Professional Open Source
3+
* Copyright 2013 Red Hat Inc. and/or its affiliates and other contributors
4+
* as indicated by the @authors tag. All rights reserved.
5+
*/
6+
package org.jboss.planet.service;
7+
8+
import java.io.IOException;
9+
import java.net.URI;
10+
import java.net.URISyntaxException;
11+
import java.util.logging.Level;
12+
import java.util.logging.Logger;
13+
14+
import javax.annotation.PostConstruct;
15+
import javax.annotation.PreDestroy;
16+
import javax.ejb.Lock;
17+
import javax.ejb.LockType;
18+
import javax.ejb.Singleton;
19+
import javax.inject.Inject;
20+
import javax.inject.Named;
21+
22+
import org.apache.http.Consts;
23+
import org.apache.http.HttpHost;
24+
import org.apache.http.auth.AuthScope;
25+
import org.apache.http.auth.UsernamePasswordCredentials;
26+
import org.apache.http.client.AuthCache;
27+
import org.apache.http.client.CredentialsProvider;
28+
import org.apache.http.client.HttpResponseException;
29+
import org.apache.http.client.config.CookieSpecs;
30+
import org.apache.http.client.config.RequestConfig;
31+
import org.apache.http.client.methods.HttpDelete;
32+
import org.apache.http.client.methods.HttpPost;
33+
import org.apache.http.client.protocol.HttpClientContext;
34+
import org.apache.http.client.utils.URIUtils;
35+
import org.apache.http.entity.ContentType;
36+
import org.apache.http.entity.EntityTemplate;
37+
import org.apache.http.impl.auth.BasicScheme;
38+
import org.apache.http.impl.client.BasicAuthCache;
39+
import org.apache.http.impl.client.BasicCredentialsProvider;
40+
import org.apache.http.impl.client.BasicResponseHandler;
41+
import org.apache.http.impl.client.CloseableHttpClient;
42+
import org.apache.http.impl.client.HttpClients;
43+
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
44+
import org.apache.http.protocol.BasicHttpContext;
45+
import org.apache.http.protocol.HttpContext;
46+
import org.codehaus.jackson.JsonEncoding;
47+
import org.codehaus.jackson.JsonGenerationException;
48+
import org.codehaus.jackson.map.JsonMappingException;
49+
import org.jboss.planet.model.Configuration;
50+
import org.jboss.planet.model.Post;
51+
import org.jboss.planet.util.PostToDCPContentProducer;
52+
53+
/**
54+
* Adapter for JBoss back-end service.<br/>
55+
* Documentation: http://docs.jbossorg.apiary.io/
56+
*
57+
* @author Libor Krzyzanek
58+
*/
59+
@Singleton
60+
@Named
61+
@Lock(LockType.READ)
62+
public class JBossSyncAdapter {
63+
64+
@Inject
65+
private Logger log;
66+
67+
@Inject
68+
private ConfigurationService configurationService;
69+
70+
public static final String SYNC_REST_API = "/rest/content/";
71+
72+
private CloseableHttpClient httpClient = null;
73+
74+
private HttpContext localContext = null;
75+
76+
@PostConstruct
77+
public void init() {
78+
//https://hc.apache.org/httpcomponents-client-4.3.x/httpclient/examples/org/apache/http/examples/client/ClientMultiThreadedExecution.java
79+
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
80+
cm.setMaxTotal(50);
81+
cm.setDefaultMaxPerRoute(10);
82+
83+
Configuration cfg = configurationService.getConfiguration();
84+
HttpHost syncHost;
85+
try {
86+
syncHost = URIUtils.extractHost(new URI(cfg.getSyncServer()));
87+
} catch (URISyntaxException e) {
88+
throw new RuntimeException("Invalid rest api url" + configurationService.getConfiguration(), e);
89+
}
90+
91+
// Don't use cookies
92+
RequestConfig globalConfig = RequestConfig.custom()
93+
.setCookieSpec(CookieSpecs.IGNORE_COOKIES)
94+
.build();
95+
96+
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
97+
credentialsProvider.setCredentials(
98+
new AuthScope(syncHost.getHostName(), AuthScope.ANY_PORT),
99+
new UsernamePasswordCredentials(cfg.getSyncUsername(), cfg.getSyncPassword()));
100+
101+
this.localContext = createPreemptiveAuthContext(syncHost);
102+
103+
this.httpClient = HttpClients.custom()
104+
.useSystemProperties()
105+
.setDefaultRequestConfig(globalConfig)
106+
.setConnectionManager(cm)
107+
.setDefaultCredentialsProvider(credentialsProvider)
108+
.build();
109+
log.log(Level.INFO, "New http client for DCP sync created.");
110+
}
111+
112+
protected HttpContext createPreemptiveAuthContext(HttpHost targetHost) {
113+
// Create AuthCache instance
114+
AuthCache authCache = new BasicAuthCache();
115+
// Generate BASIC scheme object and add it to the local auth cache
116+
BasicScheme basicAuth = new BasicScheme();
117+
authCache.put(targetHost, basicAuth);
118+
119+
// Add AuthCache to the execution context
120+
BasicHttpContext localcontext = new BasicHttpContext();
121+
localcontext.setAttribute(HttpClientContext.AUTH_CACHE, authCache);
122+
123+
return localcontext;
124+
}
125+
126+
@PreDestroy
127+
public void close() {
128+
if (httpClient != null) {
129+
try {
130+
httpClient.close();
131+
} catch (IOException e) {
132+
log.log(Level.SEVERE, "Cannot close httpclient", e);
133+
}
134+
}
135+
}
136+
137+
/**
138+
* Push content to JBoss back-end
139+
*
140+
* @param p
141+
*/
142+
public void pushPostToDcp(final Post p) throws JsonGenerationException, JsonMappingException,
143+
IOException, HttpResponseException {
144+
Configuration c = configurationService.getConfiguration();
145+
String syncApiURL = c.getSyncServer() + SYNC_REST_API + c.getSyncContentType() + "/" + p.getTitleAsId();
146+
147+
HttpPost httpPost = new HttpPost(syncApiURL);
148+
149+
PostToDCPContentProducer producer = new PostToDCPContentProducer(p, JsonEncoding.UTF8);
150+
151+
EntityTemplate entityTemplate = new EntityTemplate(producer);
152+
153+
ContentType httpContentType = ContentType.create(producer.getMimeType(), Consts.UTF_8);
154+
entityTemplate.setContentType(httpContentType.toString());
155+
httpPost.setEntity(entityTemplate);
156+
157+
httpClient.execute(httpPost, new BasicResponseHandler(), localContext);
158+
}
159+
160+
/**
161+
* Delete post from JBoss back-end
162+
*
163+
* @param postTitleAsId
164+
* @throws IOException
165+
*/
166+
public void deletePostInDcp(String postTitleAsId) throws IOException {
167+
log.log(Level.INFO, "Delete post {0} from DCP", postTitleAsId);
168+
169+
Configuration c = configurationService.getConfiguration();
170+
// Documentation: http://docs.jbossorg.apiary.io/
171+
String syncApiURL = c.getSyncServer() + SYNC_REST_API + c.getSyncContentType() + "/" + postTitleAsId
172+
+ "?ignore_missing=true";
173+
174+
HttpDelete httpDelete = new HttpDelete(syncApiURL);
175+
httpClient.execute(httpDelete, new BasicResponseHandler(), localContext);
176+
}
177+
178+
}

0 commit comments

Comments
 (0)