Skip to content

Commit

Permalink
Supporting custom context path per app sector.
Browse files Browse the repository at this point in the history
  • Loading branch information
nmihajlovski committed Apr 17, 2016
1 parent 77ad025 commit 82a2786
Show file tree
Hide file tree
Showing 27 changed files with 302 additions and 95 deletions.
25 changes: 0 additions & 25 deletions rapidoid-commons/src/main/java/org/rapidoid/util/Msc.java
Expand Up @@ -730,31 +730,6 @@ public static void logProperties(Properties props) {
}
}

public static String uriFor(Object target) {
if (!JPA.isEntity(target)) {
return "";
}

return uriFor(typeUri(target.getClass()), target);
}

public static String uriFor(String baseUri, Object target) {
if (!JPA.isEntity(target)) {
return "";
}

Object id = JPA.getIdentifier(target);
return id != null ? uri(baseUri, id + "") : "";
}

public static String typeUri(Class<?> entityType) {
return typeUri(entityType.getSimpleName());
}

public static String typeUri(String entityType) {
return "/" + English.plural(Str.uncapitalized(entityType)).toLowerCase();
}

public static boolean hasValidation() {
return Cls.exists("javax.validation.Validation");
}
Expand Down
28 changes: 15 additions & 13 deletions rapidoid-commons/src/main/resources/default/config.yml
Expand Up @@ -13,27 +13,29 @@ app:
navbar: true
fluid: false
cdn: auto # in DEV mode is false, in PRODUCTION is true
contextPath: /
home: /
menu: {}
sectors:
admin:
brand: '<i class="fa fa-dashboard"></i> Admin'
contextPath: /_
home: /_
brand: '<i class="fa fa-dashboard"></i> Admin'
menu:
Overview: /_
Data: /_data
Routes: /_routes
Config: /_config
Metrics: /_metrics
Data: /_/data
Routes: /_/routes
Config: /_/config
Metrics: /_/metrics
JMX:
Memory pool: /_jmx/mempool
JVM Threads: /_jmx/threads
Operating system: /_jmx/os
Garbage collection: /_jmx/gc
Memory: /_jmx/memory
Runtime: /_jmx/runtime
Classes: /_jmx/classes
Compilation: /_jmx/compilation
Memory pool: /_/jmx/mempool
JVM Threads: /_/jmx/threads
Operating system: /_/jmx/os
Garbage collection: /_/jmx/gc
Memory: /_/jmx/memory
Runtime: /_/jmx/runtime
Classes: /_/jmx/classes
Compilation: /_/jmx/compilation

users: {}

Expand Down
3 changes: 1 addition & 2 deletions rapidoid-gui/src/main/java/org/rapidoid/gui/Grid.java
Expand Up @@ -31,7 +31,6 @@
import org.rapidoid.model.Items;
import org.rapidoid.model.Property;
import org.rapidoid.u.U;
import org.rapidoid.util.Msc;
import org.rapidoid.var.Var;

import java.util.List;
Expand Down Expand Up @@ -162,7 +161,7 @@ protected Tag itemRow(List<Property> properties, Item item) {
}

protected String onClickScript(Item item) {
String uri = toUri != null ? Lmbd.eval(toUri, item.value()) : Msc.uriFor(item.value());
String uri = toUri != null ? Lmbd.eval(toUri, item.value()) : GUI.uriFor(item.value());
return U.frmt("Rapidoid.goAt('%s');", uri);
}

Expand Down
Expand Up @@ -4,10 +4,7 @@
import org.rapidoid.annotation.Since;
import org.rapidoid.beany.Beany;
import org.rapidoid.cls.Cls;
import org.rapidoid.commons.AnyObj;
import org.rapidoid.commons.Rnd;
import org.rapidoid.commons.Str;
import org.rapidoid.commons.TimeSeries;
import org.rapidoid.commons.*;
import org.rapidoid.gui.*;
import org.rapidoid.gui.reqinfo.IReqInfo;
import org.rapidoid.gui.reqinfo.ReqInfo;
Expand Down Expand Up @@ -513,7 +510,7 @@ public static Tag[] mediaList(List<Object> found) {

for (Object result : found) {
Object id = Beany.getId(result);
String uri = Msc.uriFor(result);
String uri = uriFor(result);

Tag left = h6("(ID", NBSP, "=", NBSP, id, ")");
Object header = span(result.getClass().getSimpleName());
Expand Down Expand Up @@ -763,7 +760,7 @@ private static Object _display(Object item) {
}

if (JPA.isEntity(item)) {
return a(escape(item.toString())).href(Msc.uriFor(item) + "/view");
return a(escape(item.toString())).href(uriFor(item) + "/view");
}

if (isBean(item)) {
Expand Down Expand Up @@ -970,4 +967,31 @@ public static Object dygraph(String uri, TimeSeries ts) {
return dygraph(uri, ts, "rapidoid-dygraph");
}

public static String uriFor(Object target) {
if (!JPA.isEntity(target)) {
return "";
}

return uriFor(typeUri(target.getClass()), target);
}

public static String uriFor(String baseUri, Object target) {
if (!JPA.isEntity(target)) {
return "";
}

Object id = JPA.getIdentifier(target);
return id != null ? Msc.uri(baseUri, id + "") : "";
}

public static String typeUri(Class<?> entityType) {
return typeUri(entityType.getSimpleName());
}

public static String typeUri(String entityType) {
String contextPath = ReqInfo.get().contextPath();
String typeUri = English.plural(Str.uncapitalized(entityType)).toLowerCase();
return Msc.uri(contextPath, typeUri);
}

}
Expand Up @@ -63,4 +63,6 @@ public interface IReqInfo {
Set<String> roles();

String sector();

String contextPath();
}
Expand Up @@ -41,6 +41,8 @@ public class MockReqInfo extends AbstractReqInfo {

private String sector = "main";

private String contextPath = "/";

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

private Map<String, String> params = U.map();
Expand Down Expand Up @@ -208,6 +210,21 @@ public String sector() {
return sector;
}

public MockReqInfo sector(String sector) {
this.sector = sector;
return this;
}

@Override
public String contextPath() {
return contextPath;
}

public MockReqInfo contextPath(String contextPath) {
this.contextPath = contextPath;
return this;
}

@Override
public String toString() {
final int maxLen = 10;
Expand All @@ -216,6 +233,7 @@ public String toString() {
+ ", uri=" + uri
+ ", host=" + host
+ ", sector=" + sector
+ ", contextPath=" + contextPath
+ ", data=" + (data != null ? toString(data.entrySet(), maxLen) : null)
+ ", params=" + (params != null ? toString(params.entrySet(), maxLen) : null)
+ ", posted=" + (posted != null ? toString(posted.entrySet(), maxLen) : null)
Expand Down
Expand Up @@ -117,4 +117,9 @@ public String sector() {
return "main";
}

@Override
public String contextPath() {
return "/";
}

}
Expand Up @@ -113,4 +113,9 @@ public String sector() {
return req().sector();
}

@Override
public String contextPath() {
return req().contextPath();
}

}
Expand Up @@ -158,7 +158,7 @@ public void onRequest(Channel channel, boolean isGet, boolean isKeepAlive, Range
cookies = Collections.synchronizedMap(cookies);

req = new ReqImpl(this, channel, isKeepAlive, verb, uri, path, query, body, params, headers, cookies,
posted, files, contentType, sector);
posted, files, contentType, sector, customization);

if (!attributes.isEmpty()) {
req.attrs().putAll(attributes);
Expand Down
Expand Up @@ -409,6 +409,8 @@ private void addOrRemove(boolean add, String verbs, String path, HttpHandler han
U.notNull(verbs, "HTTP verbs");
U.notNull(path, "HTTP path");

U.must(path.startsWith("/"), "The URI must start with '/', but found: '%s'", path);

if (add) {
U.notNull(handler, "HTTP handler");
}
Expand Down
11 changes: 11 additions & 0 deletions rapidoid-http-fast/src/main/java/org/rapidoid/http/HttpUtils.java
Expand Up @@ -25,7 +25,9 @@
import org.rapidoid.commons.MediaType;
import org.rapidoid.commons.Str;
import org.rapidoid.config.Conf;
import org.rapidoid.config.Config;
import org.rapidoid.crypto.Crypto;
import org.rapidoid.http.customize.Customization;
import org.rapidoid.http.customize.JsonResponseRenderer;
import org.rapidoid.io.Res;
import org.rapidoid.lambda.Mapper;
Expand Down Expand Up @@ -285,4 +287,13 @@ public static void resultToResponse(Req req, Object result) {
}
}

public static String getContextPath(Customization customization, String sector) {
Config cfg = customization.appConfig();

if (sector != null) {
cfg = cfg.sub("sectors", sector);
}

return cfg.entry("contextPath").or("/");
}
}
Expand Up @@ -22,9 +22,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand Down
12 changes: 12 additions & 0 deletions rapidoid-http-fast/src/main/java/org/rapidoid/http/Req.java
Expand Up @@ -109,6 +109,18 @@ public interface Req {
*/
Req sector(String sector);

/**
* Gets the <b>context path</b> of the application sector handling the request. <br>
* The default context path is <code>/</code> for the <code>On</code> API, and <code>/_</code> for the <code>Admin</code> API.
*/
String contextPath();

/**
* Overwrites the <b>context path</b> of the application sector handling the request. <br>
* The default context path is <code>/</code> for the <code>On</code> API, and <code>/_</code> for the <code>Admin</code> API.
*/
Req contextPath(String contextPath);

/**
* Gets the <b>IP address</b> of the HTTP client sending the request.
*/
Expand Down
33 changes: 29 additions & 4 deletions rapidoid-http-fast/src/main/java/org/rapidoid/http/ReqImpl.java
Expand Up @@ -26,6 +26,7 @@
import org.rapidoid.cls.Cls;
import org.rapidoid.commons.MediaType;
import org.rapidoid.commons.Str;
import org.rapidoid.http.customize.Customization;
import org.rapidoid.io.Upload;
import org.rapidoid.log.Log;
import org.rapidoid.net.abstracts.Channel;
Expand Down Expand Up @@ -55,6 +56,10 @@ public class ReqImpl implements Req, Constants, HttpMetadata {

private volatile String query;

private volatile String sector = "main";

private volatile String contextPath;

private volatile byte[] body;

private final Map<String, String> params;
Expand All @@ -75,8 +80,6 @@ public class ReqImpl implements Req, Constants, HttpMetadata {

private volatile RespImpl response;

private volatile String sector = "main";

private volatile boolean rendering;

private volatile int posConLen;
Expand All @@ -91,10 +94,12 @@ public class ReqImpl implements Req, Constants, HttpMetadata {

private final MediaType defaultContentType;

private final Customization customization;

public ReqImpl(FastHttp http, Channel channel, boolean isKeepAlive, String verb, String uri, String path,
String query, byte[] body, Map<String, String> params, Map<String, String> headers,
Map<String, String> cookies, Map<String, Object> posted, Map<String, List<Upload>> files,
MediaType defaultContentType, String sector) {
MediaType defaultContentType, String sector, Customization customization) {

this.http = http;
this.channel = channel;
Expand All @@ -111,6 +116,7 @@ public ReqImpl(FastHttp http, Channel channel, boolean isKeepAlive, String verb,
this.files = files;
this.defaultContentType = defaultContentType;
this.sector = sector;
this.customization = customization;
}

@Override
Expand Down Expand Up @@ -618,7 +624,8 @@ public Map<String, Serializable> cookiepack() {
try {
cpack = HttpUtils.initAndDeserializeCookiePack(this);
} catch (Exception e) {
Log.warn("Cookie-pack deserialization error! Maybe the secret was changed?", e);
Log.warn("Cookie-pack deserialization error! Maybe the secret was changed?");
Log.debug("Cookie-pack deserialization error!", e);
}

cookiepack = Collections.synchronizedMap(U.safe(cpack));
Expand Down Expand Up @@ -653,6 +660,24 @@ public Req sector(String sector) {
return this;
}

@Override
public String contextPath() {
if (contextPath == null) {
synchronized (this) {
if (contextPath == null) {
contextPath = HttpUtils.getContextPath(customization, sector());
}
}
}
return contextPath;
}

@Override
public Req contextPath(String contextPath) {
this.contextPath = contextPath;
return this;
}

@Override
public String toString() {
return verb() + " " + uri();
Expand Down

0 comments on commit 82a2786

Please sign in to comment.