@Override public HttpResponse failover(final HttpRequest request) { throw new MocoException("no failover response found"); } };
public static void idle(final long duration, final TimeUnit unit) { try { unit.sleep(duration); } catch (InterruptedException e) { throw new MocoException(e); } }
@Override public InputStream apply(final String input) { try { return new FileInputStream(input); } catch (FileNotFoundException e) { throw new MocoException(e); } } };
public static <T> T toObject(final String value, final Class<T> clazz) { try { return DEFAULT_MAPPER.readValue(value, clazz); } catch (IOException e) { throw new MocoException(e); } }
public static String toJson(final Object value) { try { return DEFAULT_MAPPER.writeValueAsString(value); } catch (JsonProcessingException e) { throw new MocoException(e); } }
public static <T> T toObject(final InputStream value, final Class<T> clazz) { try { return DEFAULT_MAPPER.readValue(value, clazz); } catch (IOException e) { throw new MocoException(e); } }
public final Builder withContent(final InputStream is) { try { this.content = toByteArray(is); return this; } catch (IOException e) { throw new MocoException(e); } }
private static FileInputStream toInputStream(final File file) { try { return new FileInputStream(file); } catch (FileNotFoundException e) { throw new MocoException(e); } }
public static String toJson(final Map map) { try { return DEFAULT_MAPPER.writeValueAsString(map); } catch (JsonProcessingException e) { throw new MocoException(e); } }
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); } }
public synchronized void stop() { if (this.running) { try { this.running = false; this.service.close(); this.result.get(); } catch (Exception e) { throw new MocoException(e); } } }
public int start(final int port, final ChannelInitializer<? extends Channel> pipelineFactory) { ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap.group(group) .channel(NioServerSocketChannel.class) .childHandler(pipelineFactory); try { future = bootstrap.bind(port).sync(); SocketAddress socketAddress = future.channel().localAddress(); return ((InetSocketAddress) socketAddress).getPort(); } catch (InterruptedException e) { throw new MocoException(e); } }
private ImmutableList<Session> restoreSessions(final File file) { try { InputStream inputStream = new FileInputStream(file); return Jsons.toObjects(inputStream, Session.class); } catch (MocoException me) { logger.error("exception found", me); return of(); } catch (IOException e) { throw new MocoException(e); } }
private static ResponseHandler createResponseHandler(final Resource resource) { Class clazz = HANDLERS.get(resource.id()); try { Constructor[] constructors = clazz.getConstructors(); return (ResponseHandler) constructors[0].newInstance(resource); } catch (Exception e) { throw new MocoException(e); } }
@Override public final void execute(final Request request) { try (CloseableHttpClient client = HttpClients.createDefault()) { doExecute(client, request); } catch (IOException e) { throw new MocoException(e); } }
@Override public void write(final String content) { try { Files.append(content, file, charset.or(Charset.defaultCharset())); } catch (Exception e) { throw new MocoException(e); } } }
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); } }
@Override protected final MessageContent responseContent(final Request request) { if (!HttpRequest.class.isInstance(request)) { throw new MocoException("Only HTTP request is allowed"); } return responseContent((HttpRequest) request); } }
private static ImmutableList<String> doGlob(final Path path, final Path searchPath) { final PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:" + path); try { final ImmutableList.Builder<String> builder = ImmutableList.builder(); Files.walkFileTree(searchPath, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException { if (matcher.matches(file)) { builder.add(file.toString()); } return FileVisitResult.CONTINUE; } }); return builder.build(); } catch (IOException e) { throw new MocoException(e); } }
protected byte[] doReadFor(final Optional<? extends Request> request) { String actualFilename = this.filename(request); URL resource = Resources.getResource(actualFilename); if (resource == null) { throw new IllegalArgumentException(format("%s does not exist", actualFilename)); } try { return toByteArray(resource.openStream()); } catch (IOException e) { throw new MocoException(e); } } }