Search in sources :

Example 6 with SocketException

use of com.robo4j.socket.http.SocketException in project robo4j by Robo4J.

the class ChannelUtils method initServerSocketChannel.

public static ServerSocketChannel initServerSocketChannel(ServerContext context) {
    try {
        final ServerSocketChannel result = ServerSocketChannel.open();
        result.bind(new InetSocketAddress(context.getPropertySafe(Integer.class, PROPERTY_SOCKET_PORT)));
        result.configureBlocking(false);
        return result;
    } catch (Exception e) {
        SimpleLoggingUtil.error(ChannelUtils.class, "init server socket channel", e);
        throw new SocketException("init server socket channel", e);
    }
}
Also used : SocketException(com.robo4j.socket.http.SocketException) InetSocketAddress(java.net.InetSocketAddress) ServerSocketChannel(java.nio.channels.ServerSocketChannel) SocketException(com.robo4j.socket.http.SocketException) IOException(java.io.IOException)

Example 7 with SocketException

use of com.robo4j.socket.http.SocketException in project robo4j by Robo4J.

the class WriteSelectionKeyHandler method handle.

@Override
public SelectionKey handle() {
    SocketChannel channel = (SocketChannel) key.channel();
    final HttpResponseProcess responseProcess = outBuffers.get(key);
    ByteBuffer buffer;
    if (responseProcess.getMethod() != null) {
        switch(responseProcess.getMethod()) {
            case GET:
                String getResponse;
                if (responseProcess.getResult() != null && responseProcess.getCode().equals(StatusCode.OK)) {
                    // FIXME: 2/18/18 (miro) put abstraction
                    String responseMessage = responseProcess.getResult().toString();
                    HttpDenominator denominator = new HttpResponseDenominator(responseProcess.getCode(), HttpVersion.HTTP_1_1);
                    getResponse = HttpMessageBuilder.Build().setDenominator(denominator).addHeaderElement(HttpHeaderFieldNames.ROBO_UNIT_UID, context.getId()).addHeaderElement(HttpHeaderFieldNames.CONTENT_LENGTH, String.valueOf(responseMessage.length())).build(responseMessage);
                } else {
                    HttpDenominator denominator = new HttpResponseDenominator(responseProcess.getCode(), HttpVersion.HTTP_1_1);
                    getResponse = HttpMessageBuilder.Build().setDenominator(denominator).build();
                }
                buffer = ChannelBufferUtils.getByteBufferByString(getResponse);
                ChannelUtils.handleWriteChannelAndBuffer("get write", channel, buffer);
                break;
            case POST:
                HttpDenominator denominator = new HttpResponseDenominator(responseProcess.getCode(), HttpVersion.HTTP_1_1);
                String postResponse = HttpMessageBuilder.Build().setDenominator(denominator).build();
                if (responseProcess.getResult() != null && responseProcess.getCode().equals(StatusCode.ACCEPTED)) {
                    buffer = ChannelBufferUtils.getByteBufferByString(postResponse);
                    ChannelUtils.handleWriteChannelAndBuffer("post write", channel, buffer);
                    sendMessageToTargetRoboReference(responseProcess);
                } else {
                    buffer = ChannelBufferUtils.getByteBufferByString(postResponse);
                    ChannelUtils.handleWriteChannelAndBuffer("post write", channel, buffer);
                }
            default:
                break;
        }
    } else {
        HttpDenominator denominator = new HttpResponseDenominator(StatusCode.BAD_REQUEST, HttpVersion.HTTP_1_1);
        String badResponse = HttpMessageBuilder.Build().setDenominator(denominator).build();
        buffer = ChannelBufferUtils.getByteBufferByString(badResponse);
        try {
            ChannelUtils.writeBuffer(channel, buffer);
        } catch (Exception e) {
            throw new SocketException("post write", e);
        }
        buffer.clear();
    }
    try {
        key.cancel();
        key.channel().close();
    } catch (IOException e) {
        throw new SocketException(e.getMessage());
    }
    return key;
}
Also used : SocketChannel(java.nio.channels.SocketChannel) HttpDenominator(com.robo4j.socket.http.message.HttpDenominator) SocketException(com.robo4j.socket.http.SocketException) HttpResponseDenominator(com.robo4j.socket.http.message.HttpResponseDenominator) HttpResponseProcess(com.robo4j.socket.http.request.HttpResponseProcess) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) SocketException(com.robo4j.socket.http.SocketException) IOException(java.io.IOException)

Aggregations

SocketException (com.robo4j.socket.http.SocketException)7 IOException (java.io.IOException)6 InetSocketAddress (java.net.InetSocketAddress)3 HttpResponseProcess (com.robo4j.socket.http.request.HttpResponseProcess)2 ByteBuffer (java.nio.ByteBuffer)2 DatagramChannel (java.nio.channels.DatagramChannel)2 SocketChannel (java.nio.channels.SocketChannel)2 HttpDecoratedRequest (com.robo4j.socket.http.message.HttpDecoratedRequest)1 HttpDecoratedResponse (com.robo4j.socket.http.message.HttpDecoratedResponse)1 HttpDenominator (com.robo4j.socket.http.message.HttpDenominator)1 HttpResponseDenominator (com.robo4j.socket.http.message.HttpResponseDenominator)1 DatagramResponseProcess (com.robo4j.socket.http.request.DatagramResponseProcess)1 RoboRequestCallable (com.robo4j.socket.http.request.RoboRequestCallable)1 RoboRequestFactory (com.robo4j.socket.http.request.RoboRequestFactory)1 ClientContext (com.robo4j.socket.http.units.ClientContext)1 PathHttpMethod (com.robo4j.socket.http.units.PathHttpMethod)1 ServerContext (com.robo4j.socket.http.units.ServerContext)1 ServerPathConfig (com.robo4j.socket.http.units.ServerPathConfig)1 DatagramSocket (java.net.DatagramSocket)1 SocketAddress (java.net.SocketAddress)1