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);
}
}
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));
}
}
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);
}
}
Aggregations