Search in sources :

Example 1 with MocoException

use of com.github.dreamhead.moco.MocoException in project moco by dreamhead.

the class DefaultFailoverExecutor method restoreSessions.

private ImmutableList<Session> restoreSessions(final File file) {
    InputStream inputStream = null;
    try {
        inputStream = new FileInputStream(file);
        List<Session> sessions = Jsons.toObject(inputStream, new TypeReference<List<Session>>() {
        });
        return copyOf(sessions);
    } catch (MocoException me) {
        logger.error("exception found", me);
        return of();
    } catch (IOException e) {
        throw new MocoException(e);
    } finally {
        if (inputStream != null) {
            Closeables.closeQuietly(inputStream);
        }
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) MocoException(com.github.dreamhead.moco.MocoException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) Session(com.github.dreamhead.moco.model.Session)

Example 2 with MocoException

use of com.github.dreamhead.moco.MocoException in project moco by dreamhead.

the class MocoClient method run.

public final void run(final String host, final int port, final ChannelHandler pipelineFactory) {
    EventLoopGroup group = new NioEventLoopGroup();
    Bootstrap bootstrap = new Bootstrap();
    bootstrap.group(group).channel(NioSocketChannel.class).remoteAddress(host, port).option(ChannelOption.TCP_NODELAY, true).handler(pipelineFactory);
    try {
        Channel channel = bootstrap.connect().sync().channel();
        ChannelFuture future = channel.closeFuture().sync();
        future.addListener(ChannelFutureListener.CLOSE);
    } catch (InterruptedException e) {
        throw new MocoException(e);
    } finally {
        group.shutdownGracefully();
    }
}
Also used : NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) ChannelFuture(io.netty.channel.ChannelFuture) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Channel(io.netty.channel.Channel) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Bootstrap(io.netty.bootstrap.Bootstrap) MocoException(com.github.dreamhead.moco.MocoException) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Example 3 with MocoException

use of com.github.dreamhead.moco.MocoException in project moco by dreamhead.

the class XmlRequestMatcher method documentBuilder.

private DocumentBuilder documentBuilder() {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    dbf.setCoalescing(true);
    dbf.setIgnoringElementContentWhitespace(true);
    dbf.setIgnoringComments(true);
    try {
        return dbf.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        throw new MocoException(e);
    }
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) MocoException(com.github.dreamhead.moco.MocoException)

Example 4 with MocoException

use of com.github.dreamhead.moco.MocoException in project moco by dreamhead.

the class MocoSocketHandler method channelRead0.

@Override
protected void channelRead0(final ChannelHandlerContext ctx, final ByteBuf msg) {
    MessageContent content = content().withContent(new ByteBufInputStream(msg)).build();
    SocketRequest request = new DefaultSocketRequest(content);
    SessionContext context = new SessionContext(request, new DefaultSocketResponse());
    Optional<Response> response = server.getResponse(context);
    Response actual = response.orElseThrow(() -> new MocoException(format("No handler found for request: %s", context.getRequest().getContent())));
    ctx.write(ByteBufs.toByteBuf(actual.getContent().getContent()));
}
Also used : DefaultSocketResponse(com.github.dreamhead.moco.model.DefaultSocketResponse) Response(com.github.dreamhead.moco.Response) MessageContent(com.github.dreamhead.moco.model.MessageContent) DefaultSocketRequest(com.github.dreamhead.moco.model.DefaultSocketRequest) SocketRequest(com.github.dreamhead.moco.SocketRequest) DefaultSocketRequest(com.github.dreamhead.moco.model.DefaultSocketRequest) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) MocoException(com.github.dreamhead.moco.MocoException) DefaultSocketResponse(com.github.dreamhead.moco.model.DefaultSocketResponse)

Example 5 with MocoException

use of com.github.dreamhead.moco.MocoException in project moco by dreamhead.

the class FileResourceReader method doReadFor.

@Override
protected byte[] doReadFor(final Request request) {
    String pathname = targetFileName(request);
    Path path = Paths.get(pathname);
    if (!Files.exists(path)) {
        throw new IllegalArgumentException(format("%s does not exist", path.getFileName().toString()));
    }
    try {
        return Files.readAllBytes(path);
    } catch (IOException e) {
        throw new MocoException(e);
    }
}
Also used : Path(java.nio.file.Path) IOException(java.io.IOException) MocoException(com.github.dreamhead.moco.MocoException)

Aggregations

MocoException (com.github.dreamhead.moco.MocoException)18 IOException (java.io.IOException)13 MessageContent (com.github.dreamhead.moco.model.MessageContent)3 ImmutableList (com.google.common.collect.ImmutableList)2 ParseException (freemarker.core.ParseException)2 Template (freemarker.template.Template)2 TemplateException (freemarker.template.TemplateException)2 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)2 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 OutputStreamWriter (java.io.OutputStreamWriter)2 Writer (java.io.Writer)2 InetSocketAddress (java.net.InetSocketAddress)2 SocketAddress (java.net.SocketAddress)2 URL (java.net.URL)2 Path (java.nio.file.Path)2 Document (org.w3c.dom.Document)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 Response (com.github.dreamhead.moco.Response)1