Search in sources :

Example 16 with MocoException

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

the class WatcherService method registerDirectory.

private WatchKey registerDirectory(final Path directory) {
    if (directoryToKey.containsKey(directory)) {
        return directoryToKey.get(directory);
    }
    try {
        WatchKey key = directory.register(service, new WatchEvent.Kind[] { ENTRY_MODIFY }, HIGH);
        directoryToKey.put(directory, key);
        return key;
    } catch (IOException e) {
        throw new MocoException(e);
    }
}
Also used : WatchKey(java.nio.file.WatchKey) WatchEvent(java.nio.file.WatchEvent) IOException(java.io.IOException) MocoException(com.github.dreamhead.moco.MocoException)

Example 17 with MocoException

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

the class TemplateResourceReader method readFor.

@Override
public final MessageContent readFor(final Request request) {
    if (request == null) {
        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), writer);
        return content().withContent(stream.toByteArray()).build();
    } catch (ParseException e) {
        logger.warn("Fail to parse template: {}", content.toString());
        throw new MocoException(e);
    } catch (IOException | 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)

Example 18 with MocoException

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

the class XmlRequestMatcher method extractDocument.

private Document extractDocument(final InputSource inputSource) {
    try {
        DocumentBuilder builder = documentBuilder();
        Document document = builder.parse(inputSource);
        document.normalizeDocument();
        trimNode(document);
        return document;
    } catch (IOException | SAXException e) {
        throw new MocoException(e);
    }
}
Also used : DocumentBuilder(javax.xml.parsers.DocumentBuilder) IOException(java.io.IOException) MocoException(com.github.dreamhead.moco.MocoException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException)

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