io.vertx.reactivex.core.file.FileSystem Java Examples

The following examples show how to use io.vertx.reactivex.core.file.FileSystem. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AbstractTemplate.java    From redpipe with Apache License 2.0 6 votes vote down vote up
protected Single<TemplateVariants> loadVariants() {
	String path = name;
	int lastSlash = path.lastIndexOf('/');
	String templateDir;
	String namePart; 
	if(lastSlash != -1) {
		templateDir = path.substring(0, lastSlash);
		namePart = path.substring(lastSlash+1);
	} else {
		templateDir = ""; // current dir
		namePart = path;
	}
	FileSystem fs = AppGlobals.get().getVertx().fileSystem();
	return fs.rxReadDir(templateDir)
		.map(list -> loadVariants(fs, templateDir, namePart, list));
}
 
Example #2
Source File: Template.java    From redpipe with Apache License 2.0 6 votes vote down vote up
public Single<String> selectVariant(Request request){
	return loadVariants()
			.flatMap(variants -> {
				// no variant
				if(variants.variants.isEmpty())
					return Single.just(variants.defaultTemplate);
				Variant selectedVariant = request.selectVariant(new ArrayList<>(variants.variants.keySet()));
				// no acceptable variant
				if(selectedVariant == null) {
					// if it does not exist, that's special
					String template = variants.defaultTemplate;
					FileSystem fs = AppGlobals.get().getVertx().fileSystem();
					return fs.rxExists(template)
							.map(exists -> {
								if(exists)
									return template;
								throw new WebApplicationException(Status.NOT_ACCEPTABLE);
							});
				}
				return Single.just(variants.variants.get(selectedVariant));
			});
}
 
Example #3
Source File: Code2.java    From rxjava2-lab with Apache License 2.0 5 votes vote down vote up
static Single<JsonArray> load() {
    Vertx vertx = Vertx.vertx();
    FileSystem fileSystem = vertx.fileSystem();
    return fileSystem.rxReadFile("src/main/resources/characters.json")
        .map(buffer -> buffer.toString())
        .map(content -> new JsonArray(content));
}
 
Example #4
Source File: AbstractSuperAPI.java    From rxjava2-lab with Apache License 2.0 5 votes vote down vote up
protected Flowable<Character> load() {
    Vertx vertx = Vertx.vertx();
    FileSystem fileSystem = vertx.fileSystem();
    return fileSystem.rxReadFile("src/main/resources/characters.json")
        .map(buffer -> buffer.toString())
        .map(content -> new JsonArray(content))
        .flatMapPublisher(array -> Flowable.fromIterable(array))
        .cast(JsonObject.class)
        .map(json -> json.mapTo(Character.class));
}
 
Example #5
Source File: Code3.java    From rxjava2-lab with Apache License 2.0 5 votes vote down vote up
static Flowable<Character> load() {
    Vertx vertx = Vertx.vertx();
    FileSystem fileSystem = vertx.fileSystem();
    return fileSystem.rxReadFile("src/main/resources/characters.json")
        .map(buffer -> buffer.toString())
        .map(content -> new JsonArray(content))
        .flatMapPublisher(array -> Flowable.fromIterable(array))
        .cast(JsonObject.class)
        .map(json -> json.mapTo(Character.class));
}
 
Example #6
Source File: AbstractTemplate.java    From redpipe with Apache License 2.0 5 votes vote down vote up
private TemplateVariants loadVariants(FileSystem fs, String templateDir, String namePart,
		List<String> list) {
	Map<Variant, String> variants = new HashMap<>();
	String defaultTemplateExtension = "";
	for (String entry : list) {
		// classpath dir entries are expanded into temp folders which we get as absolute paths
		int lastSlash = entry.lastIndexOf('/');
		if(lastSlash != -1)
			entry = entry.substring(lastSlash+1);
		if(!entry.equals(namePart)
				&& entry.startsWith(namePart)) {
			String extensionWithDot = entry.substring(namePart.length());
			if(!extensionWithDot.startsWith("."))
				continue;
			// get rid of the template extension
			int templateExtension = extensionWithDot.indexOf('.', 1);
			if(templateExtension == -1) {
				// we have a single extension, it's probably the default template extension
				defaultTemplateExtension = extensionWithDot;
				continue;
			}
			String mediaExtension = extensionWithDot.substring(1, templateExtension);
			MediaType mediaType = AbstractTemplate.parseMediaType(mediaExtension);
			variants.put(new Variant(mediaType, (String)null, null), templateDir+"/"+entry);
		}
	}
	return new TemplateVariants(templateDir+"/"+namePart+defaultTemplateExtension, variants);
}
 
Example #7
Source File: RxifiedExamples.java    From vertx-rx with Apache License 2.0 5 votes vote down vote up
public void toFlowable(Vertx vertx) {
  FileSystem fs = vertx.fileSystem();
  fs.open("/data.txt", new OpenOptions(), result -> {
    AsyncFile file = result.result();
    Flowable<Buffer> observable = file.toFlowable();
    observable.forEach(data -> System.out.println("Read data: " + data.toString("UTF-8")));
  });
}
 
Example #8
Source File: RxifiedExamples.java    From vertx-rx with Apache License 2.0 5 votes vote down vote up
public void unmarshaller(FileSystem fileSystem) {
  fileSystem.open("/data.txt", new OpenOptions(), result -> {
    AsyncFile file = result.result();
    Observable<Buffer> observable = file.toObservable();
    observable.compose(ObservableHelper.unmarshaller((MyPojo.class))).subscribe(
      mypojo -> {
        // Process the object
      }
    );
  });
}
 
Example #9
Source File: CoreTest.java    From vertx-rx with Apache License 2.0 5 votes vote down vote up
@Override
public void setUp() throws Exception {
  super.setUp();
  vertx = new Vertx(super.vertx);
  java.nio.file.FileSystem fs = FileSystems.getDefault();
  pathSep = fs.getSeparator();
  File ftestDir = testFolder.newFolder();
  testDir = ftestDir.toString();
}
 
Example #10
Source File: Helpers.java    From rxjava2-lab with Apache License 2.0 4 votes vote down vote up
public static FileSystem fs() {
    return vertx.fileSystem();
}
 
Example #11
Source File: Code1.java    From rxjava2-lab with Apache License 2.0 4 votes vote down vote up
static Single<String> getFile() {
	Vertx vertx = Vertx.vertx();
	FileSystem fileSystem = vertx.fileSystem();
	return fileSystem.rxReadFile("src/main/resources/characters.json")
			.map(buffer -> buffer.toString());
}