Search in sources :

Example 1 with HessianSerializerInput

use of com.caucho.hessian.io.HessianSerializerInput in project httpx by servicex-sh.

the class DubboExecutor method execute.

public List<byte[]> execute(HttpRequest httpRequest) {
    final URI dubboUri = httpRequest.getRequestTarget().getUri();
    final Map<String, String> params = queryToMap(dubboUri);
    String methodSignature = params.get("method");
    String serviceName = dubboUri.getPath().substring(1);
    if (serviceName.contains("/")) {
        final String[] parts = serviceName.split("/", 2);
        serviceName = parts[0];
        methodSignature = parts[1];
    }
    String methodName = methodSignature;
    String[] paramsTypeArray = new String[] {};
    Object[] arguments = new Object[] {};
    if (methodSignature.contains("(")) {
        methodName = methodSignature.substring(0, methodSignature.indexOf('('));
        final String parts = methodSignature.substring(methodSignature.indexOf('(') + 1, methodSignature.indexOf(')'));
        if (!parts.isEmpty()) {
            paramsTypeArray = Arrays.stream(parts.split(",")).map(typeName -> SHORT_TYPE_MAPPING.getOrDefault(typeName, typeName)).toArray(String[]::new);
        }
    }
    if (paramsTypeArray.length > 0) {
        final byte[] body = httpRequest.getBodyBytes();
        if (body == null || body.length == 0) {
            arguments = new Object[] { null };
        } else {
            String text = new String(body, StandardCharsets.UTF_8);
            if (!text.startsWith("[")) {
                // one object
                if (text.startsWith("\"") && text.endsWith("\"")) {
                    // remove double quota
                    text = text.substring(1, text.length() - 1);
                }
                arguments = new Object[] { text };
            } else {
                // array
                try {
                    final List<?> items = JsonUtils.readValue(text, List.class);
                    arguments = new Object[items.size()];
                    for (int i = 0; i < items.size(); i++) {
                        final Object item = items.get(i);
                        if (item instanceof String) {
                            arguments[i] = item;
                        } else {
                            arguments[i] = JsonUtils.writeValueAsString(item);
                        }
                    }
                } catch (Exception e) {
                    log.error("HTX-103-500", text);
                }
            }
        }
    }
    int port = dubboUri.getPort();
    if (port <= 0) {
        port = 20880;
    }
    System.out.println("DUBBO " + dubboUri);
    System.out.println();
    try (SocketChannel socketChannel = SocketChannel.open(new InetSocketAddress(dubboUri.getHost(), port))) {
        DubboRpcInvocation invocation = new DubboRpcInvocation(serviceName, methodName, paramsTypeArray, arguments);
        final byte[] contentBytes = invocation.toBytes();
        final byte[] headerBytes = invocation.frameHeaderBytes(0L, contentBytes.length);
        socketChannel.write(ByteBuffer.wrap(headerBytes));
        socketChannel.write(ByteBuffer.wrap(contentBytes));
        final byte[] data = extractData(socketChannel);
        Hessian2Input input = new HessianSerializerInput(new ByteArrayInputStream(data));
        final Integer responseMark = (Integer) input.readObject();
        if (responseMark == 5 || responseMark == 2) {
            // null return
            System.out.print("===No return value===");
        } else {
            final Object result = input.readObject();
            if (responseMark == 3 || responseMark == 0) {
                // exception return
                System.err.print("=====Exception thrown====");
                System.err.print(result.toString());
            } else {
                if (result instanceof String || result instanceof Number) {
                    System.out.print(result);
                    return List.of(result.toString().getBytes(StandardCharsets.UTF_8));
                } else {
                    String text = JsonUtils.writeValueAsPrettyString(result);
                    System.out.print(prettyJsonFormat(text));
                    runJsTest(httpRequest, 200, Collections.emptyMap(), "application/json", text);
                    return List.of(text.getBytes(StandardCharsets.UTF_8));
                }
            }
        }
    } catch (Exception e) {
        log.error("HTX-103-408", dubboUri);
    }
    return Collections.emptyList();
}
Also used : SocketChannel(java.nio.channels.SocketChannel) InetSocketAddress(java.net.InetSocketAddress) URI(java.net.URI) Hessian2Input(com.caucho.hessian.io.Hessian2Input) ByteArrayInputStream(java.io.ByteArrayInputStream) HessianSerializerInput(com.caucho.hessian.io.HessianSerializerInput)

Example 2 with HessianSerializerInput

use of com.caucho.hessian.io.HessianSerializerInput in project httpx by httpx-sh.

the class SofaRpcExecutor method execute.

@SuppressWarnings("unused")
public List<byte[]> execute(HttpRequest httpRequest) {
    final URI sofaUri = httpRequest.getRequestTarget().getUri();
    final Map<String, String> params = queryToMap(sofaUri);
    String methodSignature = params.get("method");
    String serviceName = sofaUri.getPath().substring(1);
    if (serviceName.contains("/")) {
        final String[] parts = serviceName.split("/", 2);
        serviceName = parts[0];
        methodSignature = parts[1];
    }
    String methodName = methodSignature;
    String[] paramsTypeArray = new String[] {};
    Object[] arguments = new Object[] {};
    if (methodSignature.contains("(")) {
        methodName = methodSignature.substring(0, methodSignature.indexOf('('));
        final String parts = methodSignature.substring(methodSignature.indexOf('(') + 1, methodSignature.indexOf(')'));
        if (!parts.isEmpty()) {
            paramsTypeArray = parts.split(",");
        }
    }
    if (paramsTypeArray.length > 0) {
        final byte[] body = httpRequest.getBodyBytes();
        if (body == null || body.length == 0) {
            arguments = new Object[] { null };
        } else {
            String text = new String(body, StandardCharsets.UTF_8);
            if (!text.startsWith("[")) {
                // one object  and convert to array
                text = "[" + text + "]";
            }
            try {
                final List<?> items = JsonUtils.readValue(text, List.class);
                arguments = new Object[items.size()];
                for (int i = 0; i < items.size(); i++) {
                    final Object item = items.get(i);
                    arguments[i] = item;
                }
            } catch (Exception e) {
                log.error("HTX-106-408", text);
            }
        }
    }
    int port = sofaUri.getPort();
    if (port <= 0) {
        port = 12200;
    }
    System.out.println("SOFA " + sofaUri);
    System.out.println();
    try (SocketChannel socketChannel = SocketChannel.open(new InetSocketAddress(sofaUri.getHost(), port))) {
        SofaRpcInvocation invocation = new SofaRpcInvocation(serviceName, methodName, paramsTypeArray, arguments);
        final byte[] contentBytes = invocation.content();
        final byte[] frameBytes = invocation.frameBytes(contentBytes);
        socketChannel.write(ByteBuffer.wrap(frameBytes));
        final byte[] receivedBytes = extractData(socketChannel);
        final ByteBuffer in = ByteBuffer.wrap(receivedBytes);
        final byte protocol = in.get();
        byte type = in.get();
        short cmdCode = in.getShort();
        byte ver2 = in.get();
        int requestId = in.getInt();
        byte serializer = in.get();
        short status = in.getShort();
        short classLen = in.getShort();
        short headerLen = in.getShort();
        int contentLen = in.getInt();
        byte[] clazz = new byte[classLen];
        byte[] header = new byte[headerLen];
        byte[] content = new byte[contentLen];
        in.get(clazz);
        in.get(header);
        in.get(content);
        Hessian2Input input = new HessianSerializerInput(new ByteArrayInputStream(content));
        final SofaResponse response = (SofaResponse) input.readObject();
        if (!response.isError()) {
            final Object appResponse = response.getAppResponse();
            if (appResponse == null) {
                System.out.print("===No return value===");
            } else {
                String text = JsonUtils.writeValueAsPrettyString(appResponse);
                System.out.print(prettyJsonFormat(text));
                runJsTest(httpRequest, 200, Collections.emptyMap(), "application/json", text);
                return List.of(text.getBytes(StandardCharsets.UTF_8));
            }
        } else {
            System.out.println("Error:" + response.getErrorMsg());
        }
    } catch (Exception e) {
        log.error("HTX-103-408", sofaUri, e);
    }
    return Collections.emptyList();
}
Also used : SocketChannel(java.nio.channels.SocketChannel) InetSocketAddress(java.net.InetSocketAddress) URI(java.net.URI) ByteBuffer(java.nio.ByteBuffer) Hessian2Input(com.caucho.hessian.io.Hessian2Input) ByteArrayInputStream(java.io.ByteArrayInputStream) HessianSerializerInput(com.caucho.hessian.io.HessianSerializerInput) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse)

Example 3 with HessianSerializerInput

use of com.caucho.hessian.io.HessianSerializerInput in project httpx by httpx-sh.

the class DubboExecutor method execute.

public List<byte[]> execute(HttpRequest httpRequest) {
    final URI dubboUri = httpRequest.getRequestTarget().getUri();
    final Map<String, String> params = queryToMap(dubboUri);
    String methodSignature = params.get("method");
    String serviceName = dubboUri.getPath().substring(1);
    if (serviceName.contains("/")) {
        final String[] parts = serviceName.split("/", 2);
        serviceName = parts[0];
        methodSignature = parts[1];
    }
    String methodName = methodSignature;
    String[] paramsTypeArray = new String[] {};
    Object[] arguments = new Object[] {};
    if (methodSignature.contains("(")) {
        methodName = methodSignature.substring(0, methodSignature.indexOf('('));
        final String parts = methodSignature.substring(methodSignature.indexOf('(') + 1, methodSignature.indexOf(')'));
        if (!parts.isEmpty()) {
            paramsTypeArray = parts.split(",");
        }
    }
    if (paramsTypeArray.length > 0) {
        final byte[] body = httpRequest.getBodyBytes();
        if (body == null || body.length == 0) {
            arguments = new Object[] { null };
        } else {
            String text = new String(body, StandardCharsets.UTF_8);
            if (!text.startsWith("[")) {
                // one object
                if (text.startsWith("\"") && text.endsWith("\"")) {
                    // remove double quota
                    text = text.substring(1, text.length() - 1);
                }
                arguments = new Object[] { text };
            } else {
                // array
                try {
                    final List<?> items = JsonUtils.readValue(text, List.class);
                    arguments = new Object[items.size()];
                    for (int i = 0; i < items.size(); i++) {
                        final Object item = items.get(i);
                        if (item instanceof String) {
                            arguments[i] = item;
                        } else {
                            arguments[i] = JsonUtils.writeValueAsString(item);
                        }
                    }
                } catch (Exception e) {
                    log.error("HTX-103-500", text);
                }
            }
        }
    }
    int port = dubboUri.getPort();
    if (port <= 0) {
        port = 20880;
    }
    System.out.println("DUBBO " + dubboUri);
    System.out.println();
    try (SocketChannel socketChannel = SocketChannel.open(new InetSocketAddress(dubboUri.getHost(), port))) {
        DubboRpcInvocation invocation = new DubboRpcInvocation(serviceName, methodName, paramsTypeArray, arguments);
        final byte[] contentBytes = invocation.toBytes();
        final byte[] headerBytes = invocation.frameHeaderBytes(0L, contentBytes.length);
        socketChannel.write(ByteBuffer.wrap(headerBytes));
        socketChannel.write(ByteBuffer.wrap(contentBytes));
        final byte[] data = extractData(socketChannel);
        Hessian2Input input = new HessianSerializerInput(new ByteArrayInputStream(data));
        final Integer responseMark = (Integer) input.readObject();
        if (responseMark == 5 || responseMark == 2) {
            // null return
            System.out.print("===No return value===");
        } else {
            final Object result = input.readObject();
            if (responseMark == 3 || responseMark == 0) {
                // exception return
                System.err.print("=====Exception thrown====");
                System.err.print(result.toString());
            } else {
                if (result instanceof String || result instanceof Number) {
                    System.out.print(result);
                    return List.of(result.toString().getBytes(StandardCharsets.UTF_8));
                } else {
                    String text = JsonUtils.writeValueAsPrettyString(result);
                    System.out.print(prettyJsonFormat(text));
                    runJsTest(httpRequest, 200, Collections.emptyMap(), "application/json", text);
                    return List.of(text.getBytes(StandardCharsets.UTF_8));
                }
            }
        }
    } catch (Exception e) {
        log.error("HTX-103-408", dubboUri);
    }
    return Collections.emptyList();
}
Also used : SocketChannel(java.nio.channels.SocketChannel) InetSocketAddress(java.net.InetSocketAddress) URI(java.net.URI) Hessian2Input(com.caucho.hessian.io.Hessian2Input) ByteArrayInputStream(java.io.ByteArrayInputStream) HessianSerializerInput(com.caucho.hessian.io.HessianSerializerInput)

Example 4 with HessianSerializerInput

use of com.caucho.hessian.io.HessianSerializerInput in project httpx by servicex-sh.

the class SofaRpcExecutor method execute.

@SuppressWarnings("unused")
public List<byte[]> execute(HttpRequest httpRequest) {
    final URI sofaUri = httpRequest.getRequestTarget().getUri();
    final Map<String, String> params = queryToMap(sofaUri);
    String methodSignature = params.get("method");
    String serviceName = sofaUri.getPath().substring(1);
    if (serviceName.contains("/")) {
        final String[] parts = serviceName.split("/", 2);
        serviceName = parts[0];
        methodSignature = parts[1];
    }
    String methodName = methodSignature;
    String[] paramsTypeArray = new String[] {};
    Object[] arguments = new Object[] {};
    if (methodSignature.contains("(")) {
        methodName = methodSignature.substring(0, methodSignature.indexOf('('));
        final String parts = methodSignature.substring(methodSignature.indexOf('(') + 1, methodSignature.indexOf(')'));
        if (!parts.isEmpty()) {
            paramsTypeArray = Arrays.stream(parts.split(",")).map(typeName -> DubboExecutor.SHORT_TYPE_MAPPING.getOrDefault(typeName, typeName)).toArray(String[]::new);
        }
    }
    if (paramsTypeArray.length > 0) {
        final byte[] body = httpRequest.getBodyBytes();
        if (body == null || body.length == 0) {
            arguments = new Object[] { null };
        } else {
            String text = new String(body, StandardCharsets.UTF_8);
            if (!text.startsWith("[")) {
                // one object  and convert to array
                text = "[" + text + "]";
            }
            try {
                final List<?> items = JsonUtils.readValue(text, List.class);
                arguments = new Object[items.size()];
                for (int i = 0; i < items.size(); i++) {
                    final Object item = items.get(i);
                    arguments[i] = item;
                }
            } catch (Exception e) {
                log.error("HTX-106-408", text);
            }
        }
    }
    int port = sofaUri.getPort();
    if (port <= 0) {
        port = 12200;
    }
    System.out.println("SOFA " + sofaUri);
    System.out.println();
    try (SocketChannel socketChannel = SocketChannel.open(new InetSocketAddress(sofaUri.getHost(), port))) {
        SofaRpcInvocation invocation = new SofaRpcInvocation(serviceName, methodName, paramsTypeArray, arguments);
        final byte[] contentBytes = invocation.content();
        final byte[] frameBytes = invocation.frameBytes(contentBytes);
        socketChannel.write(ByteBuffer.wrap(frameBytes));
        final byte[] receivedBytes = extractData(socketChannel);
        final ByteBuffer in = ByteBuffer.wrap(receivedBytes);
        final byte protocol = in.get();
        byte type = in.get();
        short cmdCode = in.getShort();
        byte ver2 = in.get();
        int requestId = in.getInt();
        byte serializer = in.get();
        short status = in.getShort();
        short classLen = in.getShort();
        short headerLen = in.getShort();
        int contentLen = in.getInt();
        byte[] clazz = new byte[classLen];
        byte[] header = new byte[headerLen];
        byte[] content = new byte[contentLen];
        in.get(clazz);
        in.get(header);
        in.get(content);
        Hessian2Input input = new HessianSerializerInput(new ByteArrayInputStream(content));
        final SofaResponse response = (SofaResponse) input.readObject();
        if (!response.isError()) {
            final Object appResponse = response.getAppResponse();
            if (appResponse == null) {
                System.out.print("===No return value===");
            } else {
                String text = JsonUtils.writeValueAsPrettyString(appResponse);
                System.out.print(prettyJsonFormat(text));
                runJsTest(httpRequest, 200, Collections.emptyMap(), "application/json", text);
                return List.of(text.getBytes(StandardCharsets.UTF_8));
            }
        } else {
            System.out.println("Error:" + response.getErrorMsg());
        }
    } catch (Exception e) {
        log.error("HTX-103-408", sofaUri, e);
    }
    return Collections.emptyList();
}
Also used : SocketChannel(java.nio.channels.SocketChannel) InetSocketAddress(java.net.InetSocketAddress) URI(java.net.URI) ByteBuffer(java.nio.ByteBuffer) Hessian2Input(com.caucho.hessian.io.Hessian2Input) ByteArrayInputStream(java.io.ByteArrayInputStream) HessianSerializerInput(com.caucho.hessian.io.HessianSerializerInput) SofaResponse(com.alipay.sofa.rpc.core.response.SofaResponse)

Example 5 with HessianSerializerInput

use of com.caucho.hessian.io.HessianSerializerInput in project alibaba-rsocket-broker by alibaba.

the class HessianTest method testDecode.

@Test
public void testDecode() throws Exception {
    testEncode();
    ByteArrayInputStream bis = new ByteArrayInputStream(content);
    HessianSerializerInput input = new HessianSerializerInput(bis);
    Object[] object = (Object[]) input.readObject();
    System.out.println(object);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) HessianSerializerInput(com.caucho.hessian.io.HessianSerializerInput) Test(org.junit.jupiter.api.Test)

Aggregations

HessianSerializerInput (com.caucho.hessian.io.HessianSerializerInput)6 ByteArrayInputStream (java.io.ByteArrayInputStream)6 Hessian2Input (com.caucho.hessian.io.Hessian2Input)4 InetSocketAddress (java.net.InetSocketAddress)4 URI (java.net.URI)4 SocketChannel (java.nio.channels.SocketChannel)4 SofaResponse (com.alipay.sofa.rpc.core.response.SofaResponse)2 ByteBuffer (java.nio.ByteBuffer)2 Test (org.junit.jupiter.api.Test)2 HessianSerializerOutput (com.caucho.hessian.io.HessianSerializerOutput)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1