Search in sources :

Example 1 with MessageContent

use of com.github.dreamhead.moco.model.MessageContent in project moco by dreamhead.

the class MocoSocketHandler method channelRead0.

@Override
protected void channelRead0(final ChannelHandlerContext ctx, final ByteBuf msg) throws Exception {
    try {
        MessageContent content = content().withContent(new ByteBufInputStream(msg)).build();
        SocketRequest request = new DefaultSocketRequest(content);
        this.monitor.onMessageArrived(request);
        SocketResponse response = new DefaultSocketResponse();
        handleSession(new SessionContext(request, response));
        this.monitor.onMessageLeave(response);
        ctx.write(Unpooled.wrappedBuffer(response.getContent().getContent()));
    } catch (Exception e) {
        this.monitor.onException(e);
    }
}
Also used : 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) SocketResponse(com.github.dreamhead.moco.SocketResponse) DefaultSocketResponse(com.github.dreamhead.moco.model.DefaultSocketResponse) DefaultSocketResponse(com.github.dreamhead.moco.model.DefaultSocketResponse) MocoException(com.github.dreamhead.moco.MocoException)

Example 2 with MessageContent

use of com.github.dreamhead.moco.model.MessageContent in project moco by dreamhead.

the class AbstractContentResponseHandler method doWriteToResponse.

private void doWriteToResponse(final HttpRequest httpRequest, final MutableHttpResponse httpResponse) {
    MessageContent content = requireResponseContent(httpRequest);
    httpResponse.setContent(content);
    httpResponse.addHeader(HttpHeaders.CONTENT_LENGTH, content.getContent().length);
    if (!detector.hasContentType(httpResponse)) {
        httpResponse.addHeader(HttpHeaders.CONTENT_TYPE, getContentType(httpRequest));
    }
}
Also used : MessageContent(com.github.dreamhead.moco.model.MessageContent)

Example 3 with MessageContent

use of com.github.dreamhead.moco.model.MessageContent in project moco by dreamhead.

the class TemplateResourceReader method readFor.

@Override
public MessageContent readFor(final Optional<? extends Request> request) {
    if (!request.isPresent()) {
        throw new IllegalStateException("Request is required to render template");
    }
    MessageContent content = this.template.readFor(request);
    try {
        Template targetTemplate = createTemplate(content);
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        Writer writer = new OutputStreamWriter(stream);
        targetTemplate.process(variables(request.get()), writer);
        return content().withContent(stream.toByteArray()).build();
    } catch (ParseException e) {
        logger.error("Fail to parse template: {}", content.toString());
        throw new MocoException(e);
    } catch (IOException e) {
        throw new MocoException(e);
    } catch (TemplateException e) {
        throw new MocoException(e);
    }
}
Also used : MessageContent(com.github.dreamhead.moco.model.MessageContent) TemplateException(freemarker.template.TemplateException) OutputStreamWriter(java.io.OutputStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ParseException(freemarker.core.ParseException) MocoException(com.github.dreamhead.moco.MocoException) IOException(java.io.IOException) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer) Template(freemarker.template.Template)

Aggregations

MessageContent (com.github.dreamhead.moco.model.MessageContent)3 MocoException (com.github.dreamhead.moco.MocoException)2 SocketRequest (com.github.dreamhead.moco.SocketRequest)1 SocketResponse (com.github.dreamhead.moco.SocketResponse)1 DefaultSocketRequest (com.github.dreamhead.moco.model.DefaultSocketRequest)1 DefaultSocketResponse (com.github.dreamhead.moco.model.DefaultSocketResponse)1 ParseException (freemarker.core.ParseException)1 Template (freemarker.template.Template)1 TemplateException (freemarker.template.TemplateException)1 ByteBufInputStream (io.netty.buffer.ByteBufInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Writer (java.io.Writer)1