A Simple NIO-based HTTP/HTTPS Server Example
INTRODUCTION
============
This directory contains a simple HTTP/HTTPS server. HTTP/HTTPS are two
common network protocols that provide for data transfer, and are more
fully described in RFC 2616 and RFC 2818 (Available at
http://www.ietf.org ). HTTPS is essentially HTTP after the connection
has been secured with SSL/TLS. TLS is the successor to SSL, and is
described in RFC 2246.
This server was written to demonstrate some of the functionality new to
the Java 2 platform. The demo is not meant to be a full tutorial, and
assumes the reader has some familiarity with the subject matter.
In particular, it shows:
New I/O (java.nio, java.nio.channels, java.util.regex, java.nio.charset)
Introduced in version 1.4 of the platform, NIO was designed to
overcome some of the scalability limitations found in the
existing blocking java.net.* API's, and to address other
concepts such as Regular Expression parsing and Character
Sets.
This server demonstrates:
ByteBuffer
Blocking and Non-Blocking I/O
SocketChannel
ServerSocketChannel
Selector
CharacterSet
Pattern matching using Regular Expressions
JSSE (javax.net.ssl)
Introduced in version 1.4 of the platform, JSSE provides
network security using SSL/TLS for java.net.Socket-based
traffic. In version 1.5, the SSLEngine API was introduced
which separates the SSL/TLS functionality from the underlying
I/O model. By making this separation, applications can adapt
I/O and compute strategies to best fit their circumstances.
This server demonstrates:
Using SSLEngine to create a HTTPS server
Creating simple key material for use with HTTPS
Concurrency Library (java.util.concurrent)
Introduced in version 1.5 of the platform, the concurrency
library provides a mechanism which decouples task submission
from the mechanics of how each task will be run.
This server demonstrates:
A ThreadPool with a fixed number of threads, which is
based on the number of available processors.
SETUP
=====
The server must be built on version 1.5 (or later) of the platform.
Invoking the following should be sufficient:
% mkdir build
% javac -source 1.5 -target 1.5 -d build *.java
The following creates the document root:
% mkdir root
All documents should be placed in this directory.
For HTTPS, the server authenticates itself to clients by using simple
Public Key Infrastructure (PKI) credentials in the form of
X509Certificates. You must create the server's credentials before
attempting to run the server in "-secure" mode. The server is
currently hardcoded to look for its credentials in a file called
"testkeys".
In this example, we'll create credentials for a fictional widget web
site owned by the ubiquitous "Xyzzy, Inc.". When you run this in your
own environment, replace "widgets.xyzzy.com" with the hostname of your
server.
The easiest way to create the SSL/TLS credentials is to use the
java keytool, by doing the following:
(<CR> represents your end-of-line key)
% keytool -genkey -keyalg rsa -keystore testkeys -alias widgets
Enter keystore password: passphrase
What is your first and last name?
[Unknown]: widgets.xyzzy.com<CR>
What is the name of your organizational unit?
[Unknown]: Consumer Widgets Group<CR>
What is the name of your organization?
[Unknown]: Xyzzy, Inc.<CR>
What is the name of your City or Locality?
[Unknown]: Arcata<CR>
What is the name of your State or Province?
[Unknown]: CA<CR>
What is the two-letter country code for this unit?
[Unknown]: US<CR>
Is CN=widgets.xyzzy.com, OU=Consumer Widgets Group, O="Xyzzy, Inc.",
L=Arcata, ST=CA, C=US correct?
[no]: yes<CR>
Enter key password for <mykey>
(RETURN if same as keystore password): <CR>
This directory also contain a very simple URL reader (URLDumper), which
connects to a specified URL and places all output into a specified file.
SERVER EXECUTION
================
% java -classpath build Server N1
Usage: Server <type> [options]
type:
B1 Blocking/Single-threaded Server
BN Blocking/Multi-threaded Server
BP Blocking/Pooled-thread Server
N1 Nonblocking/Single-threaded Server
N2 Nonblocking/Dual-threaded Server
options:
-port port port number
default: 8000
-backlog backlog backlog
default: 1024
-secure encrypt with SSL/TLS
default is insecure
"http://" URLs should be used with insecure mode, and
"https://" for secure mode.
The "B*" servers use classic blocking I/O: in other words, calls to
read()/write() will not return until the I/O operation has completed. The
"N*" servers use non-blocking mode and Selectors to determine which
Channels are ready to perform I/O.
B1: A single-threaded server which completely services each
connection before moving to the next.
B2: A multi-threaded server which creates a new thread for each
connection. This is not efficient for large numbers of
connections.
BP: A multi-threaded server which creates a pool of threads for use
by the server. The Thread pool decides how to schedule those
threads.
N1: A single-threaded server. All accept() and read()/write()
operations are performed by a single thread, but only after
being selected for those operations by a Selector.
N2: A dual-threaded server which performs accept()s in one thread, and
services requests in a second. Both threads use select().
CLIENT EXECUTION
================
You can test the server using any standard browser such as Internet
Explorer or Mozilla, but since the browser will not trust the
credentials you just created, you may need to accept the credentials
via the browser's pop-up dialog box.
Alternatively, to use the certificates using the simple included JSSE
client URLDumper, export the server certificate into a new truststore,
and then run the application using the new truststore.
% keytool -export -keystore testkeys -alias widgets -file widgets.cer
Enter keystore password: passphrase<CR>
Certificate stored in file <widgets.cer>
% keytool -import -keystore trustCerts -alias widgetServer \
-file widgets.cer
Enter keystore password: passphrase<CR>
Owner: CN=widgets.xyzzy.com, OU=Consumer, O="xyzzy, inc.", L=Arcata,
ST=CA, C=US
Issuer: CN=widgets.xyzzy.com, OU=Consumer, O="xyzzy, inc.",
L=Arcata, ST=CA, C=US
Serial number: 4086cc7a
Valid from: Wed Apr 21 12:33:14 PDT 2004 until: Tue Jul 20 12:33:14
PDT 2004
Certificate fingerprints:
MD5: 39:71:42:CD:BF:0D:A9:8C:FB:8B:4A:CD:F8:6D:19:1F
SHA1: 69:5D:38:E9:F4:6C:E5:A7:4C:EA:45:8E:FB:3E:F3:9A:84:01:6F:22
Trust this certificate? [no]: yes<CR>
Certificate was added to keystore
% java -classpath build -Djavax.net.ssl.trustStore=trustCerts \
-Djavax.net.ssl.TrustStorePassword=passphrase \
URLDumper https://widgets.xyzzy.com:8000/ outputFile
NOTE: The server must be run with "-secure" in order to receive
"https://" URLs.
WARNING: This is just a simple example for code exposition, you should
spend more time understanding PKI security concerns.
SOURCE CODE OVERVIEW
====================
The main class is Server, which handles program startup, and is
subclassed by the "B*" and "N*" server classes.
Following a successful accept(), the "B*" variants each create a
RequestServicer object to perform the actual request/reply operations. The
primary differences between the different "B*" servers is how the
RequestServicer is actually run:
B1
Alibaba-Dragonwell-Standard-8.14.15-x64-linux.tar.gz
需积分: 0 201 浏览量
更新于2023-02-10
收藏 97.91MB GZ 举报
阿里巴巴龙井(Alibaba Dragonwell)是一款由阿里巴巴集团开发并维护的开源Java开发工具套件(JDK),它基于OpenJDK项目,并针对阿里巴巴的业务场景进行了优化和增强。"Alibaba-Dragonwell-Standard-8.14.15-x64-linux.tar.gz" 文件是针对64位Linux系统的龙井JDK的版本8.14.15的压缩包,包含了运行和开发Java应用程序所需的全套组件。
OpenJDK是Java Development Kit的一个开源实现,由Oracle、IBM、Red Hat等公司以及全球开发者社区共同参与维护。Dragonwell作为OpenJDK的一个分支,其主要目标是提供更稳定、高性能的JVM环境,尤其在大型分布式系统和云计算环境中表现出色。版本号8.14.15表明这是8系列的一个特定更新版本,其中可能包含性能提升、安全修复和其他改进。
在"dragonwell-8.14.15"这个压缩包内,用户可以找到以下关键组件:
1. **JRE(Java Runtime Environment)**: 这是运行Java应用程序的基础,包括Java虚拟机(JVM)、Java类库和必要的二进制文件。
2. **javac**: Java编译器,将源代码编译为字节码,以便JVM执行。
3. **jar**: 工具用于创建、管理和查看Java归档(JAR)文件,这些文件可以包含多个类、资源和元数据。
4. **javadoc**: 用于生成关于Java源代码的文档的工具。
5. **jconsole**: Java监视和管理控制台,帮助开发者监控应用程序的性能和资源消耗。
6. **jmap**: 用于生成堆转储或内存映射的工具,帮助分析内存使用情况。
7. **jstack**: 显示Java线程堆栈跟踪,用于调试多线程问题。
8. **jinfo**: 提供配置Java进程的信息。
9. **jhat**: 垃圾收集器和内存分析工具,用于分析heap dump文件。
10. **jcmd**: 一个命令行工具,用于执行各种JVM诊断命令。
11. **jfr**: Java飞行记录器,用于收集诊断和分析数据。
12. **jlink**: 工具用于创建自定义的JRE,只包含应用程序所需的模块,从而减小部署体积。
13. **jpackage**: Java应用打包工具,可以将Java应用程序打包成可执行文件,方便在不同操作系统上分发。
安装Dragonwell后,开发者可以利用其优化的JVM和类库来提升Java应用的性能。同时,由于其与OpenJDK的兼容性,已有的OpenJDK应用无需修改即可无缝迁移到Dragonwell上。通过定期的更新和维护,如8.14.15这样的版本发布,阿里巴巴致力于确保其用户能够在安全性和稳定性方面得到保障,同时享受到持续的性能优化。