|
| 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