Skip to content

Commit d06ffe2

Browse files
committed
Make interfaces more sensible
1 parent cf68dda commit d06ffe2

File tree

108 files changed

+3870
-586
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

108 files changed

+3870
-586
lines changed

simple-demo/simple-demo/src/main/java/org/simpleframework/demo/http/WebServer.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,21 @@
88

99
import org.simpleframework.demo.ssl.Certificate;
1010
import org.simpleframework.http.core.Container;
11-
import org.simpleframework.http.core.ContainerServer;
12-
import org.simpleframework.transport.Server;
11+
import org.simpleframework.http.core.ContainerSocketConnector;
12+
import org.simpleframework.transport.SocketConnector;
1313
import org.simpleframework.transport.connect.Connection;
1414
import org.simpleframework.transport.connect.SocketConnection;
15-
import org.simpleframework.transport.trace.Analyzer;
15+
import org.simpleframework.transport.trace.TraceAnalyzer;
1616

1717
public class WebServer {
1818

1919
private final Certificate certificate;
2020
private final Connection connection;
2121
private final SocketAddress address;
22-
private final Server server;
22+
private final SocketConnector server;
2323

24-
public WebServer(Container container, Certificate certificate, Analyzer analyzer, int port) throws IOException {
25-
this.server = new ContainerServer(container, 2);
24+
public WebServer(Container container, Certificate certificate, TraceAnalyzer analyzer, int port) throws IOException {
25+
this.server = new ContainerSocketConnector(container, 2);
2626
this.connection = new SocketConnection(server, analyzer);
2727
this.address = new InetSocketAddress(port);
2828
this.certificate = certificate;

simple-demo/simple-demo/src/main/java/org/simpleframework/demo/trace/LogAnalyzer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99

1010
import org.apache.log4j.Logger;
1111
import org.simpleframework.common.thread.Daemon;
12-
import org.simpleframework.transport.trace.Analyzer;
12+
import org.simpleframework.transport.trace.TraceAnalyzer;
1313
import org.simpleframework.transport.trace.Trace;
1414

15-
public class LogAnalyzer extends Daemon implements Analyzer {
15+
public class LogAnalyzer extends Daemon implements TraceAnalyzer {
1616

1717
private static final Logger LOG = Logger.getLogger(LogAnalyzer.class);
1818

simple-test/src/main/java/org/simpleframework/http/validate/test/Adapter.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
import org.simpleframework.http.Request;
1010
import org.simpleframework.http.Response;
1111
import org.simpleframework.http.core.Container;
12-
import org.simpleframework.http.core.ContainerServer;
13-
import org.simpleframework.transport.Server;
12+
import org.simpleframework.http.core.ContainerSocketConnector;
13+
import org.simpleframework.transport.SocketConnector;
1414
import org.simpleframework.transport.connect.Connection;
1515
import org.simpleframework.transport.connect.SocketConnection;
1616

@@ -19,7 +19,7 @@ class Adapter implements Container {
1919
private final Analyser handler;
2020
private final Scenario scenario;
2121
private final Connection connection;
22-
private final Server server;
22+
private final SocketConnector server;
2323
private final SecurityManager manager;
2424
private final AtomicInteger requests;
2525
private final AtomicInteger failures;
@@ -28,7 +28,7 @@ class Adapter implements Container {
2828

2929
public Adapter(Analyser handler, Scenario scenario) throws Exception {
3030
this.requestIds = Collections.synchronizedSet(new HashSet<String>());
31-
this.server = new ContainerServer(this, 10);
31+
this.server = new ContainerSocketConnector(this, 10);
3232
this.connection = new SocketConnection(server);
3333
this.manager = new SecurityManager();
3434
this.requests = new AtomicInteger();

simple-test/src/test/java/org/simpleframework/http/core/DribbleCursor.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
import java.io.IOException;
44

5-
import org.simpleframework.transport.Cursor;
5+
import org.simpleframework.transport.ByteCursor;
66

7-
public class DribbleCursor implements Cursor {
7+
public class DribbleCursor implements ByteCursor {
88

9-
private Cursor cursor;
9+
private ByteCursor cursor;
1010
private byte[] swap;
1111
private int dribble;
1212

13-
public DribbleCursor(Cursor cursor, int dribble) {
13+
public DribbleCursor(ByteCursor cursor, int dribble) {
1414
this.cursor = cursor;
1515
this.dribble = dribble;
1616
this.swap = new byte[1];

simple-test/src/test/java/org/simpleframework/http/core/StreamCursor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
import java.io.InputStream;
66
import java.io.OutputStream;
77

8-
import org.simpleframework.transport.Cursor;
8+
import org.simpleframework.transport.ByteCursor;
99
import org.simpleframework.transport.Transport;
1010
import org.simpleframework.transport.TransportCursor;
1111

12-
public class StreamCursor implements Cursor {
12+
public class StreamCursor implements ByteCursor {
1313

1414
private TransportCursor cursor;
1515
private Transport transport;
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
* BodyEncoder.java February 2007
3+
*
4+
* Copyright (C) 2001, Niall Gallagher <niallg@users.sf.net>
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
15+
* implied. See the License for the specific language governing
16+
* permissions and limitations under the License.
17+
*/
18+
19+
package org.simpleframework.http.core;
20+
21+
import java.io.IOException;
22+
import java.nio.ByteBuffer;
23+
24+
/**
25+
* The <code>BodyEncoder</code> object is used to encode content from
26+
* the HTTP response. This acts in much the same way as an output
27+
* stream would. As a requirement of RFC 2616 any HTTP/1.1 compliant
28+
* server must support a set of transfer types. These are fixed size,
29+
* chunked encoded, and connection close. A producer implementation
30+
* is required to implement one of this formats for delivery of the
31+
* response message.
32+
*
33+
* @author Niall Gallagher
34+
*
35+
* @see org.simpleframework.http.core.BodyObserver
36+
*/
37+
interface BodyEncoder {
38+
39+
/**
40+
* This method is used to encode the provided array of bytes in
41+
* a HTTP/1.1 compliant format and sent it to the client. Once
42+
* the data has been encoded it is handed to the transport layer
43+
* within the server, which may choose to buffer the data if the
44+
* content is too small to send efficiently or if the socket is
45+
* not write ready.
46+
*
47+
* @param array this is the array of bytes to send to the client
48+
*/
49+
void encode(byte[] array) throws IOException;
50+
51+
/**
52+
* This method is used to encode the provided array of bytes in
53+
* a HTTP/1.1 compliant format and sent it to the client. Once
54+
* the data has been encoded it is handed to the transport layer
55+
* within the server, which may choose to buffer the data if the
56+
* content is too small to send efficiently or if the socket is
57+
* not write ready.
58+
*
59+
* @param array this is the array of bytes to send to the client
60+
* @param off this is the offset within the array to send from
61+
* @param size this is the number of bytes that are to be sent
62+
*/
63+
void encode(byte[] array, int off, int size) throws IOException;
64+
65+
/**
66+
* This method is used to encode the provided buffer of bytes in
67+
* a HTTP/1.1 compliant format and sent it to the client. Once
68+
* the data has been encoded it is handed to the transport layer
69+
* within the server, which may choose to buffer the data if the
70+
* content is too small to send efficiently or if the socket is
71+
* not write ready.
72+
*
73+
* @param buffer this is the buffer of bytes to send to the client
74+
*/
75+
void encode(ByteBuffer buffer) throws IOException;
76+
77+
/**
78+
* This method is used to encode the provided buffer of bytes in
79+
* a HTTP/1.1 compliant format and sent it to the client. Once
80+
* the data has been encoded it is handed to the transport layer
81+
* within the server, which may choose to buffer the data if the
82+
* content is too small to send efficiently or if the socket is
83+
* not write ready.
84+
*
85+
* @param buffer this is the buffer of bytes to send to the client
86+
* @param off this is the offset within the buffer to send from
87+
* @param size this is the number of bytes that are to be sent
88+
*/
89+
void encode(ByteBuffer buffer, int off, int size) throws IOException;
90+
91+
/**
92+
* This method is used to flush the contents of the buffer to
93+
* the client. This method will block until such time as all of
94+
* the data has been sent to the client. If at any point there
95+
* is an error sending the content an exception is thrown.
96+
*/
97+
void flush() throws IOException;
98+
99+
/**
100+
* This is used to signal to the producer that all content has
101+
* been written and the user no longer needs to write. This will
102+
* either close the underlying transport or it will notify the
103+
* monitor that the response has completed and the next request
104+
* can begin. This ensures the content is flushed to the client.
105+
*/
106+
void close() throws IOException;
107+
}
108+
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* BodyEncoderException.java February 2007
3+
*
4+
* Copyright (C) 2001, Niall Gallagher <niallg@users.sf.net>
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
15+
* implied. See the License for the specific language governing
16+
* permissions and limitations under the License.
17+
*/
18+
19+
package org.simpleframework.http.core;
20+
21+
import java.io.IOException;
22+
23+
/**
24+
* The <code>BodyEncoderException</code> object is used to represent
25+
* an exception that is thrown when there is a problem producing the
26+
* response body. This can be used to wrap <code>IOException</code>
27+
* objects that are thrown from the underlying transport.
28+
*
29+
* @author Niall Gallagher
30+
*/
31+
class BodyEncoderException extends IOException {
32+
33+
/**
34+
* Constructor for the <code>BodyEncoderException</code> object. This
35+
* is used to represent an exception that is thrown when producing
36+
* the response body. The encoder exception is an I/O exception
37+
* and thus exceptions can propagate out of stream methods.
38+
*
39+
* @param message this is the message describing the exception
40+
*/
41+
public BodyEncoderException(String message) {
42+
super(message);
43+
}
44+
45+
/**
46+
* Constructor for the <code>BodyEncoderException</code> object. This
47+
* is used to represent an exception that is thrown when producing
48+
* the response body. The encoder exception is an I/O exception
49+
* and thus exceptions can propagate out of stream methods.
50+
*
51+
* @param message this is the message describing the exception
52+
* @param cause this is the cause of the encoder exception
53+
*/
54+
public BodyEncoderException(String message, Throwable cause) {
55+
super(message);
56+
initCause(cause);
57+
}
58+
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* BodyEncoderFactory.java February 2007
3+
*
4+
* Copyright (C) 2001, Niall Gallagher <niallg@users.sf.net>
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
15+
* implied. See the License for the specific language governing
16+
* permissions and limitations under the License.
17+
*/
18+
19+
package org.simpleframework.http.core;
20+
21+
import org.simpleframework.transport.Channel;
22+
import org.simpleframework.transport.ByteWriter;
23+
24+
/**
25+
* The <code>BodyEncoderFactory</code> is used to create a producer to
26+
* match the HTTP header sent with the response. This interprets the
27+
* headers within the response and composes a producer that will
28+
* match those. Producers can be created to send in chunked encoding
29+
* format, as well as fixed length and connection close for HTTP/1.0.
30+
*
31+
* @author Niall Gallagher
32+
*
33+
* @see org.simpleframework.http.core.ResponseEncoder
34+
*/
35+
class BodyEncoderFactory {
36+
37+
/**
38+
* This is used to determine the semantics of the HTTP pipeline.
39+
*/
40+
private final Conversation support;
41+
42+
/**
43+
* This is the monitor used to notify the initiator of events.
44+
*/
45+
private final BodyObserver observer;
46+
47+
/**
48+
* This is the underlying sender used to deliver the raw data.
49+
*/
50+
private final ByteWriter writer;
51+
52+
/**
53+
* Constructor for the <code>BodyEncoderFactory</code> object.
54+
* This is used to create producers that can encode data in a HTTP
55+
* compliant format. Each producer created will produce its data
56+
* and deliver it to the specified sender, should an I/O events
57+
* occur such as an error, or completion of the response then
58+
* the monitor is notified and the server kernel takes action.
59+
*
60+
* @param observer this is used to deliver signals to the kernel
61+
* @param support this contains details regarding the semantics
62+
* @param writer this is used to send to the underlying transport
63+
*/
64+
public BodyEncoderFactory(BodyObserver observer, Conversation support, Channel channel) {
65+
this.writer = channel.getWriter();
66+
this.observer = observer;
67+
this.support = support;
68+
}
69+
70+
/**
71+
* This is used to create an a <code>BodyEncoder</code> object
72+
* that can be used to encode content according to the HTTP header.
73+
* If the request was from a HTTP/1.0 client that did not ask
74+
* for keep alive connection semantics a simple close producer
75+
* is created. Otherwise the content is chunked encoded or sent
76+
* according the the Content-Length.
77+
*
78+
* @return this returns the producer used to send the response
79+
*/
80+
public BodyEncoder getInstance() {
81+
boolean keepAlive = support.isKeepAlive();
82+
boolean chunkable = support.isChunkedEncoded();
83+
boolean tunnel = support.isTunnel();
84+
85+
if(!keepAlive || tunnel) {
86+
return new CloseEncoder(observer, writer);
87+
}
88+
return getInstance(chunkable);
89+
}
90+
91+
/**
92+
* This is used to create an a <code>BodyEncoder</code> object
93+
* that can be used to encode content according to the HTTP header.
94+
* If the request was from a HTTP/1.0 client that did not ask
95+
* for keep alive connection semantics a simple close producer
96+
* is created. Otherwise the content is chunked encoded or sent
97+
* according the the Content-Length.
98+
*
99+
* @param chunkable does the connected client support chunked
100+
*
101+
* @return this returns the producer used to send the response
102+
*/
103+
private BodyEncoder getInstance(boolean chunkable) {
104+
long length = support.getContentLength();
105+
106+
if(!support.isHead()) {
107+
if(length > 0) {
108+
return new FixedLengthEncoder(observer, writer, length);
109+
}
110+
if(chunkable) {
111+
return new ChunkedEncoder(observer, writer);
112+
}
113+
}
114+
return new EmptyEncoder(observer, writer);
115+
}
116+
}
117+
118+

0 commit comments

Comments
 (0)