Skip to content

Commit

Permalink
[69] robo4j-socket-http partial
Browse files Browse the repository at this point in the history
  • Loading branch information
mirage22 committed Oct 7, 2024
1 parent e4e7860 commit 6f7d407
Show file tree
Hide file tree
Showing 12 changed files with 1,101 additions and 1,074 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import com.robo4j.RoboReference;
import com.robo4j.socket.http.request.DatagramResponseProcess;
import com.robo4j.socket.http.units.ServerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.channels.SelectionKey;
import java.util.Map;
Expand All @@ -29,20 +31,21 @@
* @author Miroslav Wengner (@miragemiko)
*/
public class WriteDatagramSelectionKeyHandler implements SelectionKeyHandler {
private final Map<SelectionKey, DatagramResponseProcess> outBuffers;
private final SelectionKey key;
private static final Logger LOGGER = LoggerFactory.getLogger(WriteDatagramSelectionKeyHandler.class);
private final Map<SelectionKey, DatagramResponseProcess> outBuffers;
private final SelectionKey key;

public WriteDatagramSelectionKeyHandler(RoboContext context, ServerContext serverContext,
Map<SelectionKey, DatagramResponseProcess> outBuffers, SelectionKey key) {
this.outBuffers = outBuffers;
this.key = key;
}
public WriteDatagramSelectionKeyHandler(RoboContext context, ServerContext serverContext,
Map<SelectionKey, DatagramResponseProcess> outBuffers, SelectionKey key) {
this.outBuffers = outBuffers;
this.key = key;
}

@Override
public SelectionKey handle() {
@Override
public SelectionKey handle() {
// final DatagramChannel channel = (DatagramChannel) key.channel();
// ByteBuffer buffer = ByteBuffer.allocate(serverContext.getPropertySafe(Integer.class, PROPERTY_BUFFER_CAPACITY));
final DatagramResponseProcess responseProcess = outBuffers.get(key);
final DatagramResponseProcess responseProcess = outBuffers.get(key);

// buffer.clear();
// buffer.put("ACCEPTED".getBytes());
Expand All @@ -53,12 +56,12 @@ public SelectionKey handle() {
// throw new SocketException("handle", e);
// }

RoboReference<Object> reference = responseProcess.getTarget();
Object responseMessage = responseProcess.getResult();
reference.sendMessage(responseMessage);
System.out.println("Wrote: " + responseMessage);
RoboReference<Object> reference = responseProcess.getTarget();
Object responseMessage = responseProcess.getResult();
reference.sendMessage(responseMessage);
LOGGER.debug("responseMessage:{}", responseMessage);

key.cancel();
return key;
}
key.cancel();
return key;
}
}
1 change: 1 addition & 0 deletions robo4j-socket-http/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module robo4j.http {
requires transitive robo4j.core;
requires org.slf4j;

exports com.robo4j.socket.http;
exports com.robo4j.socket.http.codec;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@
import com.robo4j.socket.http.HttpVersion;
import com.robo4j.socket.http.ProtocolType;
import com.robo4j.socket.http.enums.StatusCode;
import com.robo4j.socket.http.message.HttpDenominator;
import com.robo4j.socket.http.message.HttpRequestDenominator;
import com.robo4j.socket.http.provider.DefaultValuesProvider;
import com.robo4j.socket.http.util.HttpHeaderBuilder;
import com.robo4j.socket.http.util.HttpMessageBuilder;
import com.robo4j.socket.http.util.HttpMessageUtils;
import com.robo4j.socket.http.util.RoboHttpUtils;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static com.robo4j.socket.http.HttpHeaderFieldValues.CONNECTION_KEEP_ALIVE;
import static com.robo4j.socket.http.provider.DefaultValuesProvider.ROBO4J_CLIENT;
Expand All @@ -43,53 +44,54 @@
* @author Miro Wengner (@miragemiko)
*/
class HttpHeaderTests {
private static final Logger LOGGER = LoggerFactory.getLogger(HttpHeaderTests.class);

@Test
void createHeaderTest() {
String header = HttpHeaderBuilder.Build().addFirstLine("units/controller")
.addAll(DefaultValuesProvider.BASIC_HEADER_MAP).build(HttpMethod.GET, HttpVersion.HTTP_1_1);
@Test
void createHeaderTest() {
String header = HttpHeaderBuilder.Build().addFirstLine("units/controller")
.addAll(DefaultValuesProvider.BASIC_HEADER_MAP).build(HttpMethod.GET, HttpVersion.HTTP_1_1);

assertNotNull(header);
assertEquals(8, header.split(HTTP_NEW_LINE).length);
assertEquals(header.split(HTTP_NEW_LINE)[2],
createHeaderField(HttpHeaderFieldNames.USER_AGENT, ROBO4J_CLIENT));
assertEquals(header.split(HTTP_NEW_LINE)[3],
createHeaderField(HttpHeaderFieldNames.CONNECTION, CONNECTION_KEEP_ALIVE));
}
assertNotNull(header);
assertEquals(8, header.split(HTTP_NEW_LINE).length);
assertEquals(header.split(HTTP_NEW_LINE)[2],
createHeaderField(HttpHeaderFieldNames.USER_AGENT, ROBO4J_CLIENT));
assertEquals(header.split(HTTP_NEW_LINE)[3],
createHeaderField(HttpHeaderFieldNames.CONNECTION, CONNECTION_KEEP_ALIVE));
}

@Test
void characterParser() {
assertEquals("[", HttpMessageUtils.getHttpSeparator(13));
assertEquals("]",HttpMessageUtils.getHttpSeparator(14));
}
@Test
void characterParser() {
assertEquals("[", HttpMessageUtils.getHttpSeparator(13));
assertEquals("]", HttpMessageUtils.getHttpSeparator(14));
}

@Test
void test() {
String uid = "1234";
String expectedResult = "HTTP/1.1 200 OK" + HTTP_NEW_LINE + "uid: " + uid + HTTP_NEW_LINE;
//@formatter:off
@Test
void test() {
String uid = "1234";
String expectedResult = "HTTP/1.1 200 OK" + HTTP_NEW_LINE + "uid: " + uid + HTTP_NEW_LINE;
//@formatter:off
String getHeader = HttpHeaderBuilder.Build()
.addFirstLine(HttpVersion.HTTP_1_1.getValue())
.addFirstLine(StatusCode.OK.getCode())
.addFirstLine(StatusCode.OK.getReasonPhrase())
.add(HttpHeaderFieldNames.ROBO_UNIT_UID, uid)
.build();
//@formatter:on
assertEquals(expectedResult, getHeader);
}
assertEquals(expectedResult, getHeader);
}

@Test
void extractHeaderParameter() {
String message = "message";
HttpDenominator denominator = new HttpRequestDenominator(HttpMethod.POST, HttpVersion.HTTP_1_1);
String postRequest = HttpMessageBuilder.Build().setDenominator(denominator)
.addHeaderElement(HttpHeaderFieldNames.HOST, RoboHttpUtils.createHost("127.0.0.1", ProtocolType.HTTP.getPort()))
.addHeaderElement(HttpHeaderFieldNames.CONTENT_LENGTH, String.valueOf(message.length()))
.build(message);
System.out.println("HEADER: " + postRequest);
}
@Test
void extractHeaderParameter() {
var message = "message";
var denominator = new HttpRequestDenominator(HttpMethod.POST, HttpVersion.HTTP_1_1);
var postRequest = HttpMessageBuilder.Build().setDenominator(denominator)
.addHeaderElement(HttpHeaderFieldNames.HOST, RoboHttpUtils.createHost("127.0.0.1", ProtocolType.HTTP.getPort()))
.addHeaderElement(HttpHeaderFieldNames.CONTENT_LENGTH, String.valueOf(message.length()))
.build(message);
LOGGER.debug("postRequest:{}", postRequest);
}

private String createHeaderField(String key, String value) {
return key + ": " + value;
}
private String createHeaderField(String key, String value) {
return key + ": " + value;
}
}
Loading

0 comments on commit 6f7d407

Please sign in to comment.