public int compare(final Transcription o1, final Transcription o2) { if (o1.getCreatedAt() < o2.getCreatedAt()) { return -1; } if (o1.getCreatedAt() > o2.getCreatedAt()) { return 1; } return 0; } });
public String replaceFirstMatch(String uri) { return pattern.matcher(uri).replaceFirst(getReplacementPath()); }
/** * Convenience method which creates a {@link Transcription} with a {@link java.util.regex.Pattern} criteria. * if an HTTP request's path pattern the given regex, replace all occurrences with * the provided alternative * * @param regex the regex to use for search * @param replaceWith the string to replace it with if it begins with the startsWith param * @param replaceEntirePath if true then the entire request path is replace, otherwise only the * startWith portion is replaced from the start of the string * @param firstOccurrenceOnly if true only the first occurrence of the pattern is replaced/re-written */ public Transcription(Pattern regex, String replaceWith, boolean replaceEntirePath, boolean firstOccurrenceOnly) { if (regex == null) { throw new NullPointerException("A regex is required"); } if (replaceWith == null) { throw new NullPointerException("A replacement string is required"); } //setPattern(Pattern.compile(regex)); this.pattern = regex; setReplacementPath(replaceWith); setReplaceWholeRequest(replaceEntirePath); setReplaceFirstOccurrence(firstOccurrenceOnly); }
public void transcribe(HttpRequest request) { //apply any transcription for (Transcription transcription : transcriptions) { if (transcription.matches(request.getUri())) { if (transcription.isReplaceWholeRequest()) { request.setUri(transcription.getReplacementPath()); } else { String newPath; if (transcription.isReplaceFirstOccurrence()) { newPath = transcription.replaceFirstMatch(request.getUri()); } else { newPath = transcription.replaceAllMatches(request.getUri()); } request.setUri(newPath); } break; } } }
public static void main(String... args) { HttpProtocolConfiguration http = new HttpProtocolConfiguration(); //re-write all requests to /app/* to index.html http.getTranscriber().addTranscription(new Transcription("/app((?:\\/[\\w([^\\..]{1,4}\b)\\-]+)+)", "/index.html")); HiggsServer server = new HiggsServer().setConfig("config.yml", HttpConfig.class); server.registerProtocol(http); // server.registerPackage(Api.class.getPackage()); // server.registerClass(Api.class); // server.registerObject(new Api()); server.start(); } }
public static void main(String... args) { //handles HTTP GET requests WebSocketConfiguration ws = new WebSocketConfiguration(); //handles all other HTTP requests HttpProtocolConfiguration http = new HttpProtocolConfiguration(); //re-write all requests to /app/* to index.html ws.getTranscriber().addTranscription(new Transcription("/app((?:\\/[\\w([^\\..]{1,4}\b)\\-]+)+)", "/index.html")); HiggsServer server = new HiggsServer().setConfig("config.yml", HttpConfig.class); server.registerProtocol(ws); //HTTP must be registered after WebSockets server.registerProtocol(http); // server.registerClass(Api.class); server.start(); } }
public String replaceAllMatches(String uri) { return pattern.matcher(uri).replaceAll(getReplacementPath()); }