use of org.apache.http.protocol.HttpProcessor in project acceptance-test-harness by jenkinsci.
the class MockUpdateCenter method ensureRunning.
public void ensureRunning() {
if (original != null) {
return;
}
// TODO this will likely not work on arbitrary controllers, so perhaps limit to the default WinstoneController
Jenkins jenkins = injector.getInstance(Jenkins.class);
List<String> sites = new UpdateCenter(jenkins).getJson("tree=sites[url]").findValuesAsText("url");
if (sites.size() != 1) {
// TODO ideally it would rather delegate to all of them, but that implies deprecating CachedUpdateCenterMetadataLoader.url and using whatever site(s) Jenkins itself specifies
LOGGER.log(Level.WARNING, "found an unexpected number of update sites: {0}", sites);
return;
}
UpdateCenterMetadata ucm;
try {
ucm = ucmd.get(jenkins);
} catch (IOException x) {
LOGGER.log(Level.WARNING, "cannot load data for mock update center", x);
return;
}
JSONObject all;
try {
all = new JSONObject(ucm.originalJSON);
all.remove("signature");
JSONObject plugins = all.getJSONObject("plugins");
LOGGER.info(() -> "editing JSON with " + plugins.length() + " plugins to reflect " + ucm.plugins.size() + " possible overrides");
for (PluginMetadata meta : ucm.plugins.values()) {
String name = meta.getName();
String version = meta.getVersion();
JSONObject plugin = plugins.optJSONObject(name);
if (plugin == null) {
LOGGER.log(Level.INFO, "adding plugin {0}", name);
plugin = new JSONObject().accumulate("name", name);
plugins.put(name, plugin);
}
plugin.put("url", name + ".hpi");
updating(plugin, "version", version);
updating(plugin, "gav", meta.gav);
updating(plugin, "requiredCore", meta.requiredCore().toString());
updating(plugin, "dependencies", new JSONArray(meta.getDependencies().stream().map(d -> {
try {
return new JSONObject().accumulate("name", d.name).accumulate("version", d.version).accumulate("optional", d.optional);
} catch (JSONException x) {
throw new AssertionError(x);
}
}).collect(Collectors.toList())));
plugin.remove("sha1");
}
} catch (JSONException x) {
LOGGER.log(Level.WARNING, "cannot prepare mock update center", x);
return;
}
HttpProcessor proc = HttpProcessorBuilder.create().add(new ResponseServer("MockUpdateCenter")).add(new ResponseContent()).add(new RequestConnControl()).build();
UriHttpRequestHandlerMapper handlerMapper = new UriHttpRequestHandlerMapper();
String json = "updateCenter.post(\n" + all + "\n);";
handlerMapper.register("/update-center.json", (HttpRequest request, HttpResponse response, HttpContext context) -> {
response.setStatusCode(HttpStatus.SC_OK);
response.setEntity(new StringEntity(json, ContentType.APPLICATION_JSON));
});
handlerMapper.register("*.hpi", (HttpRequest request, HttpResponse response, HttpContext context) -> {
String plugin = request.getRequestLine().getUri().replaceFirst("^/(.+)[.]hpi$", "$1");
PluginMetadata meta = ucm.plugins.get(plugin);
if (meta == null) {
LOGGER.log(Level.WARNING, "no such plugin {0}", plugin);
response.setStatusCode(HttpStatus.SC_NOT_FOUND);
return;
}
File local = meta.resolve(injector, meta.getVersion());
LOGGER.log(Level.INFO, "serving {0}", local);
response.setStatusCode(HttpStatus.SC_OK);
response.setEntity(new FileEntity(local));
});
handlerMapper.register("*", (HttpRequest request, HttpResponse response, HttpContext context) -> {
String location = original.replace("/update-center.json", request.getRequestLine().getUri());
LOGGER.log(Level.INFO, "redirect to {0}", location);
/* TODO for some reason DownloadService.loadJSONHTML does not seem to process the redirect, despite calling setInstanceFollowRedirects(true):
response.setStatusCode(HttpStatus.SC_MOVED_TEMPORARILY);
response.setHeader("Location", location);
*/
HttpURLConnection uc = (HttpURLConnection) new URL(location).openConnection();
uc.setInstanceFollowRedirects(true);
// TODO consider caching these downloads locally like CachedUpdateCenterMetadataLoader does for the main update-center.json
byte[] data = IOUtils.toByteArray(uc);
String contentType = uc.getContentType();
response.setStatusCode(HttpStatus.SC_OK);
response.setEntity(new ByteArrayEntity(data, ContentType.create(contentType)));
});
server = ServerBootstrap.bootstrap().setHttpProcessor(proc).setHandlerMapper(handlerMapper).setExceptionLogger(serverExceptionHandler()).create();
try {
server.start();
} catch (IOException x) {
LOGGER.log(Level.WARNING, "cannot start mock update center", x);
return;
}
original = sites.get(0);
// TODO figure out how to deal with Docker-based controllers which would need to have an IP address for the host
String override = "http://" + server.getInetAddress().getHostAddress() + ":" + server.getLocalPort() + "/update-center.json";
LOGGER.log(Level.INFO, "replacing update site {0} with {1}", new Object[] { original, override });
jenkins.runScript("DownloadService.signatureCheck = false; Jenkins.instance.updateCenter.sites.replaceBy([new UpdateSite(UpdateCenter.ID_DEFAULT, '%s')])", override);
}
use of org.apache.http.protocol.HttpProcessor in project dropwizard by dropwizard.
the class HttpClientBuilderTest method usesHttpProcessor.
@Test
void usesHttpProcessor() throws Exception {
HttpProcessor httpProcessor = mock(HttpProcessor.class);
final ConfiguredCloseableHttpClient client = builder.using(httpProcessor).createClient(apacheBuilder, connectionManager, "test");
assertThat(client).isNotNull();
assertThat(getInaccessibleField(httpClientBuilderClass, "httpprocessor").get(apacheBuilder)).isSameAs(httpProcessor);
}
use of org.apache.http.protocol.HttpProcessor in project acceptance-test-harness by jenkinsci.
the class MockUpdateCenter method ensureRunning.
public void ensureRunning(Jenkins jenkins) {
if (original != null) {
return;
}
JsonNode ucNode = new UpdateCenter(jenkins).getJson("tree=sites[url,id]");
List<String> sites = ucNode.findValuesAsText("url");
List<String> ids = ucNode.findValuesAsText("id");
if (sites.size() != 1) {
// TODO ideally it would rather delegate to all of them, but that implies deprecating CachedUpdateCenterMetadataLoader.url and using whatever site(s) Jenkins itself specifies
LOGGER.log(Level.WARNING, "found an unexpected number of update sites: {0}", sites);
return;
} else if (!"default".equals(ids.get(0))) {
LOGGER.log(Level.WARNING, "the default update site has been replaced by a site with id: {0}. Will not setup the mock update center", ids.get(0));
return;
}
UpdateCenterMetadata ucm;
try {
ucm = ucmd.get(jenkins);
} catch (IOException x) {
throw new Error("cannot load data for mock update center", x);
}
JSONObject all;
try {
all = new JSONObject(ucm.originalJSON);
all.remove("signature");
JSONObject plugins = all.getJSONObject("plugins");
LOGGER.info(() -> "editing JSON with " + plugins.length() + " plugins to reflect " + ucm.plugins.size() + " possible overrides");
for (PluginMetadata meta : ucm.plugins.values()) {
String name = meta.getName();
String version = meta.getVersion();
JSONObject plugin = plugins.optJSONObject(name);
if (plugin == null) {
LOGGER.log(Level.INFO, "adding plugin {0}", name);
plugin = new JSONObject().accumulate("name", name);
plugins.put(name, plugin);
}
plugin.put("url", name + ".hpi");
updating(plugin, "version", version);
updating(plugin, "gav", meta.gav);
updating(plugin, "requiredCore", meta.requiredCore().toString());
updating(plugin, "dependencies", new JSONArray(meta.getDependencies().stream().map(d -> {
try {
return new JSONObject().accumulate("name", d.name).accumulate("version", d.version).accumulate("optional", d.optional);
} catch (JSONException x) {
throw new AssertionError(x);
}
}).collect(Collectors.toList())));
// The fingerprints are not going to match after injecting different binary so we need to fix/recalculate
// - For JUT before 2.168, only sha1 is checked if present, so let's simply remove it
plugin.remove("sha1");
// - For JUT after 2.168, it is enough to recalculate the strongest cypher
if (meta instanceof PluginMetadata.ModifyingMetadata) {
String sha512 = ((PluginMetadata.ModifyingMetadata) meta).getSha512Checksum(injector);
plugin.put("sha512", sha512);
}
}
} catch (JSONException | NoSuchAlgorithmException | IOException x) {
LOGGER.log(Level.WARNING, "cannot prepare mock update center", x);
return;
}
HttpProcessor proc = HttpProcessorBuilder.create().add(new ResponseServer("MockUpdateCenter")).add(new ResponseContent()).add(new RequestConnControl()).build();
UriHttpRequestHandlerMapper handlerMapper = new UriHttpRequestHandlerMapper();
String json = "updateCenter.post(\n" + all + "\n);";
handlerMapper.register("/update-center.json", (HttpRequest request, HttpResponse response, HttpContext context) -> {
response.setStatusCode(HttpStatus.SC_OK);
response.setEntity(new StringEntity(json, ContentType.APPLICATION_JSON));
});
handlerMapper.register("*.hpi", (HttpRequest request, HttpResponse response, HttpContext context) -> {
String plugin = request.getRequestLine().getUri().replaceFirst("^/(.+)[.]hpi$", "$1");
PluginMetadata meta = ucm.plugins.get(plugin);
if (meta == null) {
LOGGER.log(Level.WARNING, "no such plugin {0}", plugin);
response.setStatusCode(HttpStatus.SC_NOT_FOUND);
return;
}
File local = meta.resolve(injector, meta.getVersion());
LOGGER.log(Level.INFO, "serving {0}", local);
response.setStatusCode(HttpStatus.SC_OK);
response.setEntity(new FileEntity(local));
});
handlerMapper.register("*", (HttpRequest request, HttpResponse response, HttpContext context) -> {
String location = original.replace("/update-center.json", request.getRequestLine().getUri());
LOGGER.log(Level.INFO, "redirect to {0}", location);
/* TODO for some reason DownloadService.loadJSONHTML does not seem to process the redirect, despite calling setInstanceFollowRedirects(true):
response.setStatusCode(HttpStatus.SC_MOVED_TEMPORARILY);
response.setHeader("Location", location);
*/
HttpURLConnection uc = (HttpURLConnection) new URL(location).openConnection();
uc.setInstanceFollowRedirects(true);
// TODO consider caching these downloads locally like CachedUpdateCenterMetadataLoader does for the main update-center.json
byte[] data = IOUtils.toByteArray(uc);
String contentType = uc.getContentType();
response.setStatusCode(HttpStatus.SC_OK);
response.setEntity(new ByteArrayEntity(data, ContentType.create(contentType)));
});
server = ServerBootstrap.bootstrap().setHttpProcessor(proc).setHandlerMapper(handlerMapper).setExceptionLogger(serverExceptionHandler()).create();
try {
server.start();
} catch (IOException x) {
LOGGER.log(Level.WARNING, "cannot start mock update center", x);
return;
}
original = sites.get(0);
// TODO figure out how to deal with Docker-based controllers which would need to have an IP address for the host
String override = "http://" + server.getInetAddress().getHostAddress() + ":" + server.getLocalPort() + "/update-center.json";
LOGGER.log(Level.INFO, "replacing update site {0} with {1}", new Object[] { original, override });
jenkins.runScript("DownloadService.signatureCheck = false; Jenkins.instance.updateCenter.sites.replaceBy([new UpdateSite(UpdateCenter.ID_DEFAULT, '%s')])", override);
}
use of org.apache.http.protocol.HttpProcessor in project LogHub by fbacchella.
the class HttpTestServer method before.
@Override
protected void before() throws Throwable {
HttpProcessorBuilder builder = HttpProcessorBuilder.create().add(new ResponseDate()).add(new ResponseServer("MyServer-HTTP/1.1")).add(new ResponseContent()).add(new ResponseConnControl());
HttpProcessor httpProcessor = builder.build();
SocketConfig socketConfig = SocketConfig.custom().setSoTimeout(15000).setTcpNoDelay(true).build();
ServerBootstrap bootstrap = ServerBootstrap.bootstrap().setListenerPort(port).setHttpProcessor(httpProcessor).setSocketConfig(socketConfig);
if (ssl != null) {
bootstrap.setSslContext(ssl);
}
Arrays.stream(handlers).forEach(i -> bootstrap.registerHandler(i.getKey(), i.getValue()));
server = bootstrap.create();
server.start();
}
Aggregations