@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); } }
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); } } }
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); } }
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 public void onCompleteResponse(final HttpRequest request, final HttpResponse response) { try { ObjectWriter writer = mapper.writerWithDefaultPrettyPrinter(); Session targetSession = Session.newSession(request, response); writer.writeValue(this.file, prepareTargetSessions(this.file, targetSession)); } catch (IOException e) { throw new MocoException(e); } }
private boolean shouldShutdown(final String message) { try { return shutdownKey.equals(wrap(message).readFirstLine()); } catch (IOException e) { throw new MocoException(e); } } }
@Override public HttpResponse failover(final HttpRequest request) { ImmutableList<Session> sessions = restoreSessions(this.file); final Optional<Session> session = tryFind(sessions, isForRequest(request)); if (session.isPresent()) { return session.get().getResponse(); } logger.error("No match request found: {}", request); throw new MocoException("no failover response found"); }
private InputStream getResourceAsStream(final String filename) { try { return Resources.getResource(filename).openStream(); } catch (IOException e) { throw new MocoException(e); } } }
private boolean doMatch(final Request request, final MessageContent content) { try { JsonNode requestNode = mapper.readTree(content.toString()); JsonNode resourceNode = mapper.readTree(expected.readFor(of(request)).toString()); return requestNode.equals(resourceNode); } catch (JsonProcessingException jpe) { return false; } catch (IOException e) { throw new MocoException(e); } }
private Document extractDocument(final InputSource inputSource) throws SAXException { try { DocumentBuilder builder = documentBuilder(); Document document = builder.parse(inputSource); document.normalizeDocument(); trimNode(document); return document; } catch (IOException e) { throw new MocoException(e); } } }