Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#533 - mock socket refactoring #539

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/main/java/org/takes/http/Back.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@
package org.takes.http;

import java.io.IOException;
import java.net.Socket;
import org.takes.misc.Socket;

/**
* HTTP back.
*
* <p>All implementations of this interface must be immutable and thread-safe.
*
* @author Yegor Bugayenko (yegor@teamed.io)
* @author Andrey Eliseev (aeg.exper0@gmail.com)
* @version $Id$
* @since 0.1
*/
Expand Down
17 changes: 9 additions & 8 deletions src/main/java/org/takes/http/BkBasic.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.Socket;
import lombok.EqualsAndHashCode;
import org.takes.HttpException;
import org.takes.Request;
import org.takes.Response;
import org.takes.Take;
import org.takes.misc.Socket;
import org.takes.rq.RqLive;
import org.takes.rq.RqWithHeaders;
import org.takes.rs.RsPrint;
Expand Down Expand Up @@ -72,14 +72,14 @@ public BkBasic(final Take tks) {

@Override
public void accept(final Socket socket) throws IOException {
final InputStream input = socket.getInputStream();
final InputStream input = socket.input();
try {
this.print(
BkBasic.addSocketHeaders(
new RqLive(input),
socket
),
new BufferedOutputStream(socket.getOutputStream())
new BufferedOutputStream(socket.output())
);
} finally {
input.close();
Expand Down Expand Up @@ -134,21 +134,22 @@ private static Response failure(final Throwable err, final int code) {
* @param req Request
* @param socket Socket
* @return Request with custom headers
* @throws IOException If fails.
*/
private static Request addSocketHeaders(final Request req,
final Socket socket) {
final Socket socket) throws IOException {
return new RqWithHeaders(
req,
String.format(
"X-Takes-LocalAddress: %s",
socket.getLocalAddress().getHostAddress()
socket.localAddress().getHostAddress()
),
String.format("X-Takes-LocalPort: %d", socket.getLocalPort()),
String.format("X-Takes-LocalPort: %d", socket.localPort()),
String.format(
"X-Takes-RemoteAddress: %s",
socket.getInetAddress().getHostAddress()
socket.address().getHostAddress()
),
String.format("X-Takes-RemotePort: %d", socket.getPort())
String.format("X-Takes-RemotePort: %d", socket.port())
);
}
}
2 changes: 1 addition & 1 deletion src/main/java/org/takes/http/BkParallel.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@
package org.takes.http;

import java.io.IOException;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;
import lombok.EqualsAndHashCode;
import org.takes.misc.Socket;

/**
* Parallel back-end.
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/takes/http/BkSafe.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
*/
package org.takes.http;

import java.net.Socket;
import lombok.EqualsAndHashCode;
import org.takes.misc.Socket;

/**
* Safe back-end.
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/takes/http/BkTimeable.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@
package org.takes.http;

import java.io.IOException;
import java.net.Socket;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.TimeUnit;
import lombok.EqualsAndHashCode;
import org.takes.misc.Socket;

/**
* Back decorator with maximum lifetime.
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/takes/http/BkWrap.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
package org.takes.http;

import java.io.IOException;
import java.net.Socket;
import lombok.EqualsAndHashCode;
import org.takes.misc.Socket;

/**
* Back Wrap.
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/takes/http/FtBasic.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.concurrent.TimeUnit;
import lombok.EqualsAndHashCode;
import org.takes.Take;
import org.takes.misc.Socket;

/**
* Basic front.
Expand Down Expand Up @@ -113,7 +114,7 @@ public void start(final Exit exit) throws IOException {
*/
private void loop(final ServerSocket server) throws IOException {
try {
this.back.accept(server.accept());
this.back.accept(new Socket.TCPSocket(server.accept()));
} catch (final SocketTimeoutException ex) {
assert ex != null;
}
Expand Down
222 changes: 222 additions & 0 deletions src/main/java/org/takes/misc/Socket.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2015 Yegor Bugayenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.takes.misc;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.nio.charset.StandardCharsets;

/**
* Socket interface.
* @author Andrey Eliseev (aeg.exper0@gmail.com)
* @version $Id$
* @since 0.31
*/
@SuppressWarnings("PMD.TooManyMethods")
public interface Socket extends Closeable {

/**
* Returns an input stream for this socket.
* @return InputStream.
* @throws IOException If unsuccessful.
*/
InputStream input() throws IOException;

/**
* Returns an output stream for this socket.
* @return OutputStream.
* @throws IOException If unsuccessful.
*/
OutputStream output() throws IOException;

/**
* Returns local socket address.
* @return Local socket address
* @throws IOException If unsuccessful.
*/
InetAddress localAddress() throws IOException;

/**
* Returns remote socket address.
* @return Remote socket address.
* @throws IOException If unsuccessful.
*/
InetAddress address() throws IOException;

/**
* Returns local socket port.
* @return Local socket port.
*/
int localPort();

/**
* Returns remote socket port.
* @return Remote socket port.
*/
int port();

/**
* TCP socket implementation.
* @author Andrey Eliseev (aeg.exper0@gmail.com)
* @since 0.31
*/
class TCPSocket implements Socket {

/**
* Socket.
*/
private final transient java.net.Socket socket;

/**
* Ctor.
* @param sock Actual socket.
*/
public TCPSocket(final java.net.Socket sock) {
this.socket = sock;
}
@Override
public InputStream input() throws IOException {
return this.socket.getInputStream();
}

@Override
public OutputStream output() throws IOException {
return this.socket.getOutputStream();
}

@Override
public void close() throws IOException {
this.socket.close();
}

@Override
public InetAddress localAddress() {
return this.socket.getLocalAddress();
}

@Override
public InetAddress address() {
return this.socket.getInetAddress();
}

@Override
public int localPort() {
return this.socket.getLocalPort();
}

@Override
public int port() {
return this.socket.getPort();
}
}

/**
* Fake socket implementation.
* @author Andrey Eliseev (aeg.exper0@gmail.com)
* @since 0.31
*/
class FakeSocket implements Socket {

/**
* Input stream.
*/
private final transient InputStream input;

/**
* Output stream.
*/
private final transient OutputStream output;

/**
* Ctor.
* @param inp Input stream.
*/
public FakeSocket(final InputStream inp) {
this(inp, new ByteArrayOutputStream());
}

/**
* Ctor.
* @param inp Input stream.
*/
public FakeSocket(final String inp) {
this(
new ByteArrayInputStream(
inp.getBytes(StandardCharsets.UTF_8)
),
new ByteArrayOutputStream()
);
}

/**
* Ctor.
* @param inp Input stream.
* @param out Output stream.
*/
public FakeSocket(final InputStream inp, final OutputStream out) {
this.input = inp;
this.output = out;
}

@Override
public InputStream input() throws IOException {
return this.input;
}

@Override
public OutputStream output() throws IOException {
return this.output;
}

@Override
public InetAddress localAddress() throws IOException {
return InetAddress.getLocalHost();
}

@Override
public InetAddress address() throws IOException {
return InetAddress.getLocalHost();
}

@Override
public int localPort() {
return 0;
}

@Override
public int port() {
return 0;
}

@Override
public void close() throws IOException {
// No action
}
}
}
Loading