本文整理汇总了Java中org.apache.http.protocol.HttpProcessorBuilder类的典型用法代码示例。如果您正苦于以下问题:Java HttpProcessorBuilder类的具体用法?Java HttpProcessorBuilder怎么用?Java HttpProcessorBuilder使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
HttpProcessorBuilder类属于org.apache.http.protocol包,在下文中一共展示了HttpProcessorBuilder类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: LocalTestServer
import org.apache.http.protocol.HttpProcessorBuilder; //导入依赖的package包/类
public LocalTestServer(HttpRequestHandler handler) {
try {
setUp();
HttpProcessor httpproc = HttpProcessorBuilder.create()
.add(new ResponseDate())
.add(new ResponseServer(LocalServerTestBase.ORIGIN))
.add(new ResponseContent())
.add(new ResponseConnControl())
.add(new RequestBasicAuth())
.add(new ResponseBasicUnauthorized()).build();
this.serverBootstrap.setHttpProcessor(httpproc);
this.serverBootstrap.registerHandler("*", handler);
host = start();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
示例2: getHttpProcessor
import org.apache.http.protocol.HttpProcessorBuilder; //导入依赖的package包/类
private HttpProcessor getHttpProcessor() {
if (null == _httpProcessor) {
final HttpProcessorBuilder httpProcessorBuilder = HttpProcessorBuilder.create();
httpProcessorBuilder.add(new ResponseDate());
httpProcessorBuilder.add(new ResponseServer(getOriginServer()));
httpProcessorBuilder.add(new ResponseContent());
httpProcessorBuilder.add(new ResponseConnControl());
httpProcessorBuilder.add(getRequestInterceptorService());
httpProcessorBuilder.add(getResponseInterceptorService());
_httpProcessor = httpProcessorBuilder.build();
}
return _httpProcessor;
}
示例3: start
import org.apache.http.protocol.HttpProcessorBuilder; //导入依赖的package包/类
public void start() throws Exception {
// Create HTTP protocol basic processing chain
HttpProcessor httpProcessor = HttpProcessorBuilder.create()
.add(new ResponseDate()).add(new ResponseContent())
.add(new ResponseConnControl()).build();
// Create server
HttpAsyncService protocolHandler = new HttpAsyncService(httpProcessor,
uriMapper);
NHttpConnectionFactory<DefaultNHttpServerConnection> connFactory = new DefaultNHttpServerConnectionFactory();
IOEventDispatch ioEventDispatch = new DefaultHttpServerIODispatch(
protocolHandler, connFactory);
IOReactorConfig config = IOReactorConfig.custom()
.setIoThreadCount(threads).setSoReuseAddress(true).build();
ListeningIOReactor ioReactor = new DefaultListeningIOReactor(config);
// Start server
ioReactor.listen(new InetSocketAddress(port));
ioReactor.execute(ioEventDispatch);
}
示例4: turnOffDefaultInterceptor
import org.apache.http.protocol.HttpProcessorBuilder; //导入依赖的package包/类
/**
* 完全控制由httpclient发出去的HTTP请求协议的规格
*/
private void turnOffDefaultInterceptor() throws Exception {
// 注意看这个HTTP请求的协议, 完全由我们决定控制, 只会有一个User-Agent头
// POST / HTTP/1.1
// User-Agent: Android
HttpUriRequest request = RequestBuilder.post()
.setUri("http://baidu.com")
.addHeader("User-Agent", "Android")
.build();
// 删除所有的HttpRequestInterceptor/HttpResponseInterceptor
// 这样所有HTTP请求头都可以由我们自己来控制, 因此HTTP header中就不会出现你没有设置过的header了(有一些都是Interceptor自动加进去的)
// 也不会自动解压gzip的response了
CloseableHttpClient hc = HttpClients.custom()
.setHttpProcessor(HttpProcessorBuilder.create().build())
.build();
CloseableHttpResponse response = hc.execute(request);
System.out.println(EntityUtils.toString(response.getEntity()));
}
示例5: main
import org.apache.http.protocol.HttpProcessorBuilder; //导入依赖的package包/类
public static void main(String[] args) throws Exception {
int port = 8080;
String docRoot = "D:\\svn_file\\TEST2\\150503\\web-customer";
// Set up the HTTP protocol processor
HttpProcessor httpproc = HttpProcessorBuilder.create().add(new ResponseDate())
.add(new ResponseServer("Haogrgr/1.1")).add(new ResponseContent())
.add(new ResponseConnControl()).build();
// Set up request handlers
UriHttpRequestHandlerMapper reqistry = new UriHttpRequestHandlerMapper();
reqistry.register("*", new HttpFileHandler(docRoot));
// Set up the HTTP service
HttpService httpService = new HttpService(httpproc, reqistry);
Thread t = new RequestListenerThread(port, httpService, null);
t.setDaemon(false);
t.start();
}
示例6: test
import org.apache.http.protocol.HttpProcessorBuilder; //导入依赖的package包/类
@Test
public void test() throws ExecutionException, InterruptedException {
HttpHost target = new HttpHost("localhost");
BasicConnPool connpool = new BasicConnPool();
connpool.setMaxTotal(200);
connpool.setDefaultMaxPerRoute(10);
connpool.setMaxPerRoute(target, 20);
Future<BasicPoolEntry> future = connpool.lease(target, null);
BasicPoolEntry poolEntry = future.get();
HttpClientConnection conn = poolEntry.getConnection();
HttpProcessor httpproc = HttpProcessorBuilder.create()
.add(new ResponseDate())
.add(new ResponseServer("MyServer-HTTP/1.1"))
.add(new ResponseContent())
.add(new ResponseConnControl())
.build();
HttpRequestHandler myRequestHandler = new HttpRequestHandler() {
public void handle(
HttpRequest request,
HttpResponse response,
HttpContext context) throws HttpException, IOException {
response.setStatusCode(HttpStatus.SC_OK);
response.setEntity(
new StringEntity("some important message",
ContentType.TEXT_PLAIN));
}
};
UriHttpRequestHandlerMapper handlerMapper = new UriHttpRequestHandlerMapper();
handlerMapper.register("/service/*", myRequestHandler);
HttpService httpService = new HttpService(httpproc, handlerMapper);
}
示例7: httpGetOtherFile
import org.apache.http.protocol.HttpProcessorBuilder; //导入依赖的package包/类
@Override
public TransportFetch httpGetOtherFile(String uri) {
HttpProcessor httpproc = HttpProcessorBuilder.create()
.add(new RequestContent())
.add(new RequestTargetHost())
.add(new RequestConnControl())
.add(new RequestUserAgent("Test/1.1"))
.add(new RequestExpectContinue(true)).build();
try {
HttpRequest ascRequest = new BasicHttpRequest("GET", uri);
LOG.info("Will fetch {}", ascRequest.getRequestLine());
httpexecutor.preProcess(ascRequest, httpproc, context);
final HttpResponse otherResponse = httpexecutor.execute(ascRequest, conn, context);
httpexecutor.postProcess(response, httpproc, context);
final byte[] otherBody = EntityUtils.toByteArray(otherResponse.getEntity());
LOG.info("Read body of {} bytes", otherBody.length, " bytes");
EntityUtils.consume(otherResponse.getEntity());
boolean keepalive = connStrategy.keepAlive(response, context);
context.setAttribute(HTTP_CONN_KEEPALIVE, new Boolean(keepalive));
return new TransportFetch(otherResponse.getStatusLine().getStatusCode(), otherResponse.getStatusLine().getReasonPhrase(), otherBody);
} catch (HttpException | IOException ex) {
return new TransportFetch(500, ex.getLocalizedMessage(), null);
}
}
示例8: setUp
import org.apache.http.protocol.HttpProcessorBuilder; //导入依赖的package包/类
@Before @Override
public void setUp() throws Exception {
super.setUp();
final HttpProcessor httpproc = HttpProcessorBuilder.create()
.add(new ResponseDate())
.add(new ResponseServer(LocalServerTestBase.ORIGIN))
.add(new ResponseContent())
.add(new ResponseConnControl())
.add(new RequestBasicAuth())
.add(new ResponseBasicUnauthorized()).build();
this.serverBootstrap.setHttpProcessor(httpproc);
}
示例9: testBasicAuthenticationSuccessOnNonRepeatablePutExpectContinue
import org.apache.http.protocol.HttpProcessorBuilder; //导入依赖的package包/类
@Test
public void testBasicAuthenticationSuccessOnNonRepeatablePutExpectContinue() throws Exception {
final HttpProcessor httpproc = HttpProcessorBuilder.create()
.add(new ResponseDate())
.add(new ResponseServer(LocalServerTestBase.ORIGIN))
.add(new ResponseContent())
.add(new ResponseConnControl())
.add(new RequestBasicAuth())
.add(new ResponseBasicUnauthorized()).build();
this.serverBootstrap.setHttpProcessor(httpproc)
.setExpectationVerifier(new AuthExpectationVerifier())
.registerHandler("*", new AuthHandler());
final HttpHost target = start();
final RequestConfig config = RequestConfig.custom()
.setExpectContinueEnabled(true)
.build();
final HttpPut httpput = new HttpPut("/");
httpput.setConfig(config);
httpput.setEntity(new InputStreamEntity(
new ByteArrayInputStream(
new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 } ),
-1));
final HttpClientContext context = HttpClientContext.create();
final TestCredentialsProvider credsProvider = new TestCredentialsProvider(
new UsernamePasswordCredentials("test", "test"));
context.setCredentialsProvider(credsProvider);
final HttpResponse response = this.httpclient.execute(target, httpput, context);
final HttpEntity entity = response.getEntity();
Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
Assert.assertNotNull(entity);
}
示例10: NioServer
import org.apache.http.protocol.HttpProcessorBuilder; //导入依赖的package包/类
@SneakyThrows
public NioServer( int port, int workers ) {
this.port = port;
this.mapper.register( "/static/*", new NioClasspathResourceHandler( "/static", "/WEB-INF" ) );
val ioReactorConfig = IOReactorConfig.custom().setIoThreadCount( workers ).build();
val httpProcessor = HttpProcessorBuilder.create()
.add( new ResponseDate() )
.add( new ResponseServer( "OAP Server/1.0" ) )
.add( new ResponseContent() )
.add( new ResponseConnControl() )
.build();
SSLContext sslContext = getSslContext( port );
server = ServerBootstrap.bootstrap()
.setListenerPort( port )
.setServerInfo( "OAP Server/1.0" )
.setConnectionReuseStrategy( DefaultClientConnectionReuseStrategy.INSTANCE )
.setHttpProcessor( httpProcessor )
.setIOReactorConfig( ioReactorConfig )
.setSslContext( sslContext )
.setExceptionLogger( ex -> log.debug( ex.getMessage(), ex ) )
.setHandlerMapper( mapper )
.create();
}
示例11: startServer
import org.apache.http.protocol.HttpProcessorBuilder; //导入依赖的package包/类
public static void startServer(int port) {
// 设置端口号 后期需要对端口号进行判断是否已经被占用,并把端口号信息写进模板中
isRun = true;
// Set up the HTTP protocol processor
HttpProcessor httpproc = HttpProcessorBuilder.create()
.add(new ResponseDate())
.add(new ResponseServer("Test/1.1"))
.add(new ResponseContent())
.add(new ResponseConnControl()).build();
// Set up request handlers
UriHttpRequestHandlerMapper reqistry = new UriHttpRequestHandlerMapper();
reqistry.register("*", new HttpFileHandler(null));
// Set up the HTTP service
httpService = new HttpService(httpproc, reqistry);
SSLServerSocketFactory sf = null;
try {
t = new RequestListenerThread(port, httpService, sf);
} catch (IOException e) {
e.printStackTrace();
}
t.setDaemon(true);
t.start();
}
示例12: ProxyProxy
import org.apache.http.protocol.HttpProcessorBuilder; //导入依赖的package包/类
ProxyProxy(int listenPort, InetAddress listenAddress, HttpHost proxy) throws IOException {
this.serversocket = new ServerSocket(listenPort, 0, listenAddress);
HttpProcessor httpproc = HttpProcessorBuilder.create().add(new ResponseConnControl()).build();
UriHttpRequestHandlerMapper reqistry = new UriHttpRequestHandlerMapper();
reqistry.register("*", new HttpForwardingHandler(proxy));
this.httpService = new HttpService(httpproc, new UpgradedConnectionAwareReusingStrategy(), null, reqistry);
}
示例13: testKeepAliveHeaderRespected
import org.apache.http.protocol.HttpProcessorBuilder; //导入依赖的package包/类
@Test
public void testKeepAliveHeaderRespected() throws Exception {
final HttpProcessor httpproc = HttpProcessorBuilder.create()
.add(new ResponseDate())
.add(new ResponseServer(LocalServerTestBase.ORIGIN))
.add(new ResponseContent())
.add(new ResponseConnControl())
.add(new ResponseKeepAlive()).build();
this.serverBootstrap.setHttpProcessor(httpproc)
.registerHandler("/random/*", new RandomHandler());
this.connManager.setMaxTotal(1);
this.connManager.setDefaultMaxPerRoute(1);
final HttpHost target = start();
HttpResponse response = this.httpclient.execute(target, new HttpGet("/random/2000"));
EntityUtils.consume(response.getEntity());
Assert.assertEquals(1, this.connManager.getTotalStats().getAvailable());
response = this.httpclient.execute(target, new HttpGet("/random/2000"));
EntityUtils.consume(response.getEntity());
Assert.assertEquals(1, this.connManager.getTotalStats().getAvailable());
// Now sleep for 1.1 seconds and let the timeout do its work
Thread.sleep(1100);
response = this.httpclient.execute(target, new HttpGet("/random/2000"));
EntityUtils.consume(response.getEntity());
Assert.assertEquals(1, this.connManager.getTotalStats().getAvailable());
// Do another request just under the 1 second limit & make
// sure we reuse that connection.
Thread.sleep(500);
response = this.httpclient.execute(target, new HttpGet("/random/2000"));
EntityUtils.consume(response.getEntity());
Assert.assertEquals(1, this.connManager.getTotalStats().getAvailable());
}
示例14: pageAsString
import org.apache.http.protocol.HttpProcessorBuilder; //导入依赖的package包/类
/**
* Downloads the site and returns it.
*
* @since 2.12.9
* @param infoLink
* URL
* @return Plain xml text of the website.
*/
public String pageAsString(final InfoLink infoLink) {
MDC.put("infoLink", infoLink.getUrl());
if (!infoLink.isValid()) {
log.warn("Seems not be a valid URL");
return null;
}
final CloseableHttpClient hc = HttpClients.custom().setHttpProcessor(HttpProcessorBuilder.create().build()).build();
final HttpGet request = new HttpGet(infoLink.getUrl());
newArrayList(request.getAllHeaders()).forEach(header -> {
request.removeHeader(header);
});
request.setProtocolVersion(HttpVersion.HTTP_1_1);
request.setHeader("Host", "myanimelist.net");
request.setHeader("User-Agent", "curl/7.53.0");
request.setHeader("Accept", "*/*");
String ret = null;
try {
final CloseableHttpResponse execute = hc.execute(request);
if (execute.getStatusLine().getStatusCode() == HTTP_TOO_MANY_CONNECTIONS) {
log.warn("Too many connections");
final long waitingTime = ThreadLocalRandom.current().nextLong(MIN_WAITING_TIME, MAX_WAITING_TIME);
log.warn("Waiting [{}]ms then retry.", waitingTime);
Thread.sleep(waitingTime);
return pageAsString(infoLink);
}
if (execute.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
log.error("Other error, status code [{}]", execute.getStatusLine().getStatusCode());
}
ret = EntityUtils.toString(execute.getEntity());
} catch (final IOException | InterruptedException e) {
log.error("An error occured during download of infosite: ", e);
}
return ret;
}
示例15: main
import org.apache.http.protocol.HttpProcessorBuilder; //导入依赖的package包/类
/**
* @param args the command line arguments
* @throws java.lang.Exception
*/
public static void main(String[] args) throws Exception {
int fixedExecutorSize = 4;
if (args.length != 2) {
System.err.println("Invalid command line parameters for worker");
System.exit(-1);
}
// Set worker mode
String mode = args[1];
// Creating fixed size executor
ThreadPoolExecutor taskExecutor = new ThreadPoolExecutor(fixedExecutorSize, fixedExecutorSize, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
int port = Integer.parseInt(args[0]);
// Set up the HTTP protocol processor
HttpProcessor httpproc = HttpProcessorBuilder.create()
.add(new ResponseDate())
.add(new ResponseServer("Test/1.1"))
.add(new ResponseContent())
.add(new ResponseConnControl()).build();
// Set up request handler (either generic or late binding)
UriHttpRequestHandlerMapper reqistry = new UriHttpRequestHandlerMapper();
if (mode.equals("--late"))
reqistry.register("*", new LateBindingRequestHandler(taskExecutor));
else
reqistry.register("*", new GenericRequestHandler(taskExecutor));
// Set up the HTTP service
HttpService httpService = new HttpService(httpproc, reqistry);
SSLServerSocketFactory sf = null;
// SSL code removed as it is not needed
// create a thread to listen for possible scheduler available connections
Thread t = new RequestListenerThread(port, httpService, sf);
System.out.println("Request Listener Thread created");
t.setDaemon(false);
t.start();
// main thread should wait for the listener to exit before shutdown the
// task executor pool
t.join();
// shutdown task executor pool and wait for any taskExecutor thread
// still running
taskExecutor.shutdown();
while (!taskExecutor.isTerminated()) {}
System.out.println("Finished all task executor threads");
}