Java Code Examples for org.rapidoid.u.U#map()

The following examples show how to use org.rapidoid.u.U#map() . 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: Msc.java    From rapidoid with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public static <T> Map<String, T> protectSensitiveInfo(Map<String, T> data, T replacement) {
	Map<String, T> copy = U.map();

	for (Map.Entry<String, T> e : data.entrySet()) {
		T value = e.getValue();

		String key = e.getKey().toLowerCase();

		if (value instanceof Map<?, ?>) {
			value = (T) protectSensitiveInfo((Map<String, T>) value, replacement);

		} else if (sensitiveKey(key)) {
			value = replacement;
		}

		copy.put(e.getKey(), value);
	}

	return copy;
}
 
Example 2
Source File: ConfigHandler.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public Object call() {

	List<Object> grids = U.list();
	Map<String, Object> sections = U.cast(Conf.ROOT.toMap());
	sections = Msc.protectSensitiveInfo(sections, FA.QUESTION_CIRCLE);

	Map<String, Object> root = U.map();

	for (Map.Entry<String, Object> entry : sections.entrySet()) {
		String key = entry.getKey();
		Object value = entry.getValue();

		if (value instanceof Map<?, ?>) {
			grids.add(h4(span(key).class_("label " + styleOf(key))));
			grids.add(grid((Map<String, ?>) value));
		} else {
			root.put(key, value);
		}
	}

	if (!root.isEmpty()) {
		grids.add(0, h4(span("<root>").class_("label " + styleOf("<root>"))));
		grids.add(1, grid(root));
	}

	return multi(grids);
}
 
Example 3
Source File: KeyValueRanges.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
public Map<String, String> toMap(String data) {
	Map<String, String> map = U.map();

	for (int i = 0; i < count; i++) {
		map.put(keys[i].get(data), values[i].get(data));
	}

	return map;
}
 
Example 4
Source File: HttpMultipartFormTest.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldHandleBigUploads() {
	defaultServerSetup();

	Map<String, String> cookies = U.map("foo", "bar", "COOKIE1", "a");

	String hash1 = Crypto.md5(IO.loadBytes("test1.txt"));
	String hash2 = Crypto.md5(IO.loadBytes("test2.txt"));
	String hash3 = Crypto.md5(IO.loadBytes("rabbit.jpg"));

	Upload file1 = Upload.from("test1.txt");
	Upload file2 = Upload.from("test2.txt");
	Upload file3 = Upload.from("rabbit.jpg");

	Map<String, List<Upload>> files = U.map("f1", U.list(file1), "f2", U.list(file2), "f3", U.list(file3));

	HttpClient client = HTTP.client().host(LOCALHOST).cookies(cookies);

	String res = client.post(localhost("/upload"))
		.data("a", "d")
		.files(files)
		.fetch();

	eq(res, "bar:a:d:3:" + U.join(":", hash1, hash2, hash3));

	client.close();
}
 
Example 5
Source File: BufRanges.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
public Map<String, String> toMap(Bytes bytes, int from, int to, String separator) {
	Map<String, String> map = U.map();

	for (int i = from; i <= to; i++) {
		String s = ranges[i].str(bytes);
		String[] kv = s.split(separator, 2);
		map.put(kv[0], kv.length > 1 ? kv[1] : "");
	}

	return map;
}
 
Example 6
Source File: SerializationTest.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
@Test
public void testMiniSerialization() {
	ByteBuffer buf = ByteBuffer.allocateDirect(100);
	Map<?, ?> data = U.map("a", 213, true, "xyz", "f", None.NONE, "g", Deleted.DELETED);

	Serialize.serialize(buf, data);
	buf.rewind();
	Object data2 = Serialize.deserialize(buf);

	String expected = data.toString();
	String real = data2.toString();

	eq(real, expected);
}
 
Example 7
Source File: ConfigImpl.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
@Override
public synchronized ConfigChanges getChangesSince(Config previousConfig) {

	Map<String, Object> prevMap;
	boolean initial = previousConfig == null;

	if (!initial) {
		prevMap = isRoot ? previousConfig.toMap() : previousConfig.sub(keys()).toMap();
	} else {
		prevMap = U.map();
	}

	return ConfigChanges.from(keys(), prevMap, toMap(), initial);
}
 
Example 8
Source File: CustomHibernatePersistenceProviderTest.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
@Test
public void testAlternativeHibernateConfigFile() {
	Map props = U.map(CFG_FILE, "hibernate.xml");

	EntityManagerFactory emf = provider().createEMF(props);

	assertNotNull(emf);
}
 
Example 9
Source File: NoReqInfo.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
@Override
public Map<String, Object> attrs() {
	return U.map();
}
 
Example 10
Source File: NoReqInfo.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
@Override
public Map<String, Object> posted() {
	return U.map();
}
 
Example 11
Source File: IoCState.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
public synchronized Map<String, Object> info() {
	return U.map("Provided classes", Deep.copyOf(providedClasses, Msc.TRANSFORM_TO_SIMPLE_CLASS_NAME),
		"Provided instances", Deep.copyOf(providedInstances, Msc.TRANSFORM_TO_SIMPLE_CLASS_NAME),
		"Managed instances", Deep.copyOf(instances, Msc.TRANSFORM_TO_SIMPLE_CLASS_NAME),
		"By type", Deep.copyOf(providersByType, Msc.TRANSFORM_TO_SIMPLE_CLASS_NAME));
}
 
Example 12
Source File: HttpUtils.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
public static void reload(Req x) {
	Map<String, String> sel = U.map("body", PAGE_RELOAD);
	x.response().json(U.map("_sel_", sel));
}
 
Example 13
Source File: CustomHibernatePersistenceProviderTest.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
@Test
public void shouldFailOnWrongConfigFile() {
	Map props = U.map(CFG_FILE, "non-existing.xml");

	assertThrows(ConfigurationException.class, () -> provider().createEMF(props));
}
 
Example 14
Source File: PathPatternTest.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
@Test
public void shouldMatchPathPatterns() {
	Map<String, String> empty = U.map();

	match("/abc", "/abc", "/abc", empty);

	match("/.*", "/.*", "/", empty);
	match("/.*", "/.*", "/abc", empty);

	noMatch("/.+", "/.+", "/");
	match("/.+", "/.+", "/abc", empty);

	String anyUri = "/(?<g1>.*)";

	match("/*", anyUri, "/", U.map(PathPattern.ANY, ""));
	match("/*", anyUri, "/xy", U.map(PathPattern.ANY, "xy"));
	match("/*", anyUri, "/a/bb/ccc", U.map(PathPattern.ANY, "a/bb/ccc"));

	String anySuffix1 = "(?:/(?<g1>.*))?";
	String anySuffix2 = "(?:/(?<g2>.*))?";

	match("/msgs/*", "/msgs" + anySuffix1, "/msgs", empty);
	match("/msgs/*", "/msgs" + anySuffix1, "/msgs/abc", U.map(PathPattern.ANY, "abc"));
	match("/msgs/*", "/msgs" + anySuffix1, "/msgs/foo/bar", U.map(PathPattern.ANY, "foo/bar"));

	match("/{cat}", "/" + g(1), "/books", U.map("cat", "books"));
	match("/{cat}/*", "/" + g(1) + anySuffix2, "/books", U.map("cat", "books"));
	match("/{cat}/*", "/" + g(1) + anySuffix2, "/books/x", U.map("cat", "books", PathPattern.ANY, "x"));
	match("/{_}/view", "/" + g(1) + "/view", "/movies/view", U.map("_", "movies"));

	match("/abc/{id}", "/abc/" + g(1), "/abc/123", U.map("id", "123"));
	match("/abc/{_x}", "/abc/" + g(1), "/abc/1-2", U.map("_x", "1-2"));
	match("/x/{a}/{b}", "/x/" + g(1) + "/" + g(2), "/x/ab/CDE", U.map("a", "ab", "b", "CDE"));
	match("/{1}/{2}/{3}", "/" + g(1) + "/" + g(2) + "/" + g(3), "/x/yy/zzz", U.map("1", "x", "2", "yy", "3", "zzz"));

	// custom regex
	match("/x/{abc:[a-zA-Z-]+}-{d}/{some_numbers:\\d+-\\d+}::{x:.*}",

		"/x/"
			+ g(1, "[a-zA-Z-]+")
			+ "-" + g(2)
			+ "/" + g(3, "\\d+-\\d+")
			+ "::" + g(4, ".*"),

		"/x/Hello-World-!!!/123-4567::zzz",

		U.map("abc", "Hello-World",
			"d", "!!!",
			"some_numbers", "123-4567",
			"x", "zzz")
	);

	noMatch("/{cat}", "/" + g(1), "/");
	noMatch("/x/{y}", "/x/" + g(1), "/x/");

	noMatch("/x/{y}", "/x/" + g(1), "/x/");
}
 
Example 15
Source File: NoReqInfo.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
@Override
public Map<String, String> cookies() {
	return U.map();
}
 
Example 16
Source File: ConfigAlternatives.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
@Override
public Map<String, Object> toMap() {
	Map<String, Object> map = U.map(alternative.toMap());
	map.putAll(primary.toMap());
	return map;
}
 
Example 17
Source File: HttpTestCommons.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
protected static Map<String, Object> reqResp(Req req, Resp resp) {
	return U.map("verb", req.verb(), "uri", req.uri(), "data", req.data(), "code", resp.code());
}
 
Example 18
Source File: TCPClientDemo.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
private Object msg(String s, int n) {
	return U.map("x", n, "s", s);
}
 
Example 19
Source File: GUI.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
public static Object dygraph(String uri, TimeSeries ts, String divClass) {
	List<Object> points = U.list();

	NavigableMap<Long, Double> values = ts.overview();

	for (Map.Entry<Long, Double> e : values.entrySet()) {
		points.add(U.map("date", e.getKey(), "values", e.getValue()));
	}

	Map<String, ?> model = U.map("points", points, "names", U.list(ts.title()), "title", ts.title(),
		"id", newId(), "class", divClass, "uri", Str.triml(uri, "/"));

	Tag graph = render("dygraphs.html", model);

	return div(graph);
}
 
Example 20
Source File: OpenAPIModel.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
public static Map<String, String> primitiveSchema(String type) {
	return U.map("type", type);
}