Making the Web UIs delegate to the API URL that corresponds to the UI URL.
authorJan Lahoda <jlahoda@netbeans.org>
Wed, 26 Sep 2012 13:54:27 +0200
changeset 881a38ef7700c71
parent 880 08b0c35c1d4f
child 882 cbe09579995a
Making the Web UIs delegate to the API URL that corresponds to the UI URL.
language/server/web/language.web.api/src/org/netbeans/modules/jackpot30/backend/language/api/UI.java
remoting/server/web/web.ui/src/org/netbeans/modules/jackpot30/backend/ui/UI.java
     1.1 --- a/language/server/web/language.web.api/src/org/netbeans/modules/jackpot30/backend/language/api/UI.java	Mon Sep 24 15:58:16 2012 +0200
     1.2 +++ b/language/server/web/language.web.api/src/org/netbeans/modules/jackpot30/backend/language/api/UI.java	Wed Sep 26 13:54:27 2012 +0200
     1.3 @@ -60,7 +60,9 @@
     1.4  import javax.ws.rs.Path;
     1.5  import javax.ws.rs.Produces;
     1.6  import javax.ws.rs.QueryParam;
     1.7 +import javax.ws.rs.core.Context;
     1.8  import javax.ws.rs.core.Response;
     1.9 +import javax.ws.rs.core.UriInfo;
    1.10  import org.codeviation.pojson.Pojson;
    1.11  import org.netbeans.modules.jackpot30.backend.base.WebUtilities;
    1.12  import static org.netbeans.modules.jackpot30.backend.base.WebUtilities.escapeForQuery;
    1.13 @@ -75,16 +77,17 @@
    1.14      @GET
    1.15      @Path("/search")
    1.16      @Produces("text/html")
    1.17 -    public String search(@QueryParam("path") String path, @QueryParam("pattern") String pattern) throws URISyntaxException, IOException, TemplateException {
    1.18 +    public String search(@Context UriInfo uriInfo, @QueryParam("path") String path, @QueryParam("pattern") String pattern) throws URISyntaxException, IOException, TemplateException {
    1.19 +        String urlBase = uriInfo.getBaseUri().toString();
    1.20          Map<String, Object> configurationData = new HashMap<String, Object>();
    1.21  
    1.22 -        configurationData.put("paths", list());
    1.23 +        configurationData.put("paths", list(urlBase));
    1.24          configurationData.put("selectedPath", path);
    1.25          configurationData.put("pattern", pattern);
    1.26          configurationData.put("patternEscaped", escapeForQuery(pattern));
    1.27  
    1.28          if (pattern != null && path != null) {
    1.29 -            URI u = new URI("http://localhost:9998/index/language/search?path=" + escapeForQuery(path) + "&pattern=" + escapeForQuery(pattern));
    1.30 +            URI u = new URI(urlBase + "index/language/search?path=" + escapeForQuery(path) + "&pattern=" + escapeForQuery(pattern));
    1.31              List<Map<String, Object>> results = new LinkedList<Map<String, Object>>();
    1.32              long queryTime = System.currentTimeMillis();
    1.33              List<String> candidates = new ArrayList<String>(WebUtilities.requestStringArrayResponse(u));
    1.34 @@ -117,20 +120,22 @@
    1.35      @GET
    1.36      @Path("/show")
    1.37      @Produces("text/html")
    1.38 -    public Response show(@QueryParam("path") String path, @QueryParam("relative") String relativePath, @QueryParam("pattern") String pattern) throws URISyntaxException, IOException, TemplateException {
    1.39 -        URI spansURL = new URI("http://localhost:9998/index/language/searchSpans?path=" + escapeForQuery(path) + "&relativePath=" + escapeForQuery(relativePath) + "&pattern=" + escapeForQuery(pattern));
    1.40 +    public Response show(@Context UriInfo uriInfo, @QueryParam("path") String path, @QueryParam("relative") String relativePath, @QueryParam("pattern") String pattern) throws URISyntaxException, IOException, TemplateException {
    1.41 +        String urlBase = uriInfo.getBaseUri().toString();
    1.42 +        URI spansURL = new URI(urlBase + "index/language/searchSpans?path=" + escapeForQuery(path) + "&relativePath=" + escapeForQuery(relativePath) + "&pattern=" + escapeForQuery(pattern));
    1.43          return Response.temporaryRedirect(new URI("/index/ui/show?path=" + escapeForQuery(path) + "&relative=" + escapeForQuery(relativePath) + "&highlight=" + escapeForQuery(Pojson.save(parseSpans2(WebUtilities.requestStringResponse(spansURL)))))).build();
    1.44      }
    1.45      
    1.46      @GET
    1.47      @Path("/snippet")
    1.48      @Produces("text/html")
    1.49 -    public String snippet(@QueryParam("path") String path, @QueryParam("relative") String relativePath, @QueryParam("pattern") String pattern) throws URISyntaxException, IOException, TemplateException {
    1.50 +    public String snippet(@Context UriInfo uriInfo, @QueryParam("path") String path, @QueryParam("relative") String relativePath, @QueryParam("pattern") String pattern) throws URISyntaxException, IOException, TemplateException {
    1.51 +        String urlBase = uriInfo.getBaseUri().toString();
    1.52          List<Map<String, String>> snippets = new LinkedList<Map<String, String>>();
    1.53  
    1.54 -        URI codeURL = new URI("http://localhost:9998/index/source/cat?path=" + escapeForQuery(path) + "&relative=" + escapeForQuery(relativePath));
    1.55 +        URI codeURL = new URI(urlBase + "index/source/cat?path=" + escapeForQuery(path) + "&relative=" + escapeForQuery(relativePath));
    1.56          String code = WebUtilities.requestStringResponse(codeURL);
    1.57 -        URI spansURL = new URI("http://localhost:9998/index/language/searchSpans?path=" + escapeForQuery(path) + "&relativePath=" + escapeForQuery(relativePath) + "&pattern=" + escapeForQuery(pattern));
    1.58 +        URI spansURL = new URI(urlBase + "index/language/searchSpans?path=" + escapeForQuery(path) + "&relativePath=" + escapeForQuery(relativePath) + "&pattern=" + escapeForQuery(pattern));
    1.59  
    1.60          for (int[] span : parseSpans(WebUtilities.requestStringResponse(spansURL))) {
    1.61              snippets.add(prepareSnippet(code, span));
    1.62 @@ -139,10 +144,10 @@
    1.63          return processTemplate("/org/netbeans/modules/jackpot30/backend/language/api/ui-snippet.html", Collections.<String, Object>singletonMap("snippets", snippets));
    1.64      }
    1.65  
    1.66 -    private static List<Map<String, String>> list() throws URISyntaxException {
    1.67 +    private static List<Map<String, String>> list(String urlBase) throws URISyntaxException {
    1.68          List<Map<String, String>> result = new LinkedList<Map<String, String>>();
    1.69  
    1.70 -        for (String enc : WebUtilities.requestStringArrayResponse(new URI("http://localhost:9998/index/list"))) {
    1.71 +        for (String enc : WebUtilities.requestStringArrayResponse(new URI(urlBase + "index/list"))) {
    1.72              Map<String, String> rootDesc = new HashMap<String, String>();
    1.73              String[] col = enc.split(":", 2);
    1.74  
     2.1 --- a/remoting/server/web/web.ui/src/org/netbeans/modules/jackpot30/backend/ui/UI.java	Mon Sep 24 15:58:16 2012 +0200
     2.2 +++ b/remoting/server/web/web.ui/src/org/netbeans/modules/jackpot30/backend/ui/UI.java	Wed Sep 26 13:54:27 2012 +0200
     2.3 @@ -61,6 +61,8 @@
     2.4  import javax.ws.rs.Path;
     2.5  import javax.ws.rs.Produces;
     2.6  import javax.ws.rs.QueryParam;
     2.7 +import javax.ws.rs.core.Context;
     2.8 +import javax.ws.rs.core.UriInfo;
     2.9  import org.codeviation.pojson.Pojson;
    2.10  import org.netbeans.api.java.lexer.JavaTokenId;
    2.11  import org.netbeans.api.lexer.TokenHierarchy;
    2.12 @@ -76,15 +78,16 @@
    2.13  @Path("/index/ui")
    2.14  public class UI {
    2.15  
    2.16 -    private static final String URL_BASE = "http://localhost:9998/index";
    2.17 -//    private static final String URL_BASE = "http://lahoda.info/index";
    2.18 +    private static final String URL_BASE_OVERRIDE = null;
    2.19 +//    private static final String URL_BASE_OVERRIDE = "http://lahoda.info/";
    2.20  
    2.21      @GET
    2.22      @Path("/search")
    2.23      @Produces("text/html")
    2.24 -    public String searchType(@QueryParam("path") List<String> paths, @QueryParam("prefix") String prefix) throws URISyntaxException, IOException, TemplateException {
    2.25 +    public String searchType(@Context UriInfo uriInfo, @QueryParam("path") List<String> paths, @QueryParam("prefix") String prefix) throws URISyntaxException, IOException, TemplateException {
    2.26 +        String urlBase = URL_BASE_OVERRIDE != null ? URL_BASE_OVERRIDE : uriInfo.getBaseUri().toString();
    2.27          Map<String, Object> configurationData = new HashMap<String, Object>();
    2.28 -        Map<String,Map<String, String>> pathsList = list();
    2.29 +        Map<String,Map<String, String>> pathsList = list(urlBase);
    2.30  
    2.31          configurationData.put("paths", pathsList.values());
    2.32          configurationData.put("selectedPath", paths);
    2.33 @@ -94,7 +97,7 @@
    2.34              List<Map<String, Object>> results = new LinkedList<Map<String, Object>>();
    2.35  
    2.36              for (String path : paths) {
    2.37 -                URI u = new URI(URL_BASE + "/symbol/search?path=" + escapeForQuery(path) + "&prefix=" + escapeForQuery(prefix));
    2.38 +                URI u = new URI(urlBase + "index/symbol/search?path=" + escapeForQuery(path) + "&prefix=" + escapeForQuery(prefix));
    2.39                  @SuppressWarnings("unchecked") //XXX: should not trust something got from the network!
    2.40                  Map<String, List<Map<String, Object>>> symbols = Pojson.load(LinkedHashMap.class, u);
    2.41                  Map<String, ?> segmentDescription = pathsList.get(path);
    2.42 @@ -114,7 +117,7 @@
    2.43                      }
    2.44                  }
    2.45  
    2.46 -                URI typeSearch = new URI(URL_BASE + "/type/search?path=" + escapeForQuery(path) + "&prefix=" + escapeForQuery(prefix));
    2.47 +                URI typeSearch = new URI(urlBase + "index/type/search?path=" + escapeForQuery(path) + "&prefix=" + escapeForQuery(prefix));
    2.48                  @SuppressWarnings("unchecked") //XXX: should not trust something got from the network!
    2.49                  Map<String, List<String>> types = Pojson.load(LinkedHashMap.class, typeSearch);
    2.50  
    2.51 @@ -176,8 +179,9 @@
    2.52      @GET
    2.53      @Path("/show")
    2.54      @Produces("text/html")
    2.55 -    public String show(@QueryParam("path") String segment, @QueryParam("relative") String relative, @QueryParam("highlight") @DefaultValue("[]") String highlightSpec) throws URISyntaxException, IOException, TemplateException {
    2.56 -        URI u = new URI(URL_BASE + "/source/cat?path=" + escapeForQuery(segment) + "&relative=" + escapeForQuery(relative));
    2.57 +    public String show(@Context UriInfo uriInfo, @QueryParam("path") String segment, @QueryParam("relative") String relative, @QueryParam("highlight") @DefaultValue("[]") String highlightSpec) throws URISyntaxException, IOException, TemplateException {
    2.58 +        String urlBase = URL_BASE_OVERRIDE != null ? URL_BASE_OVERRIDE : uriInfo.getBaseUri().toString();
    2.59 +        URI u = new URI(urlBase + "index/source/cat?path=" + escapeForQuery(segment) + "&relative=" + escapeForQuery(relative));
    2.60          String content = WebUtilities.requestStringResponse(u);
    2.61          List<Long> highlightSpans = Pojson.load(ArrayList.class, highlightSpec);
    2.62          Map<String, Object> configurationData = new HashMap<String, Object>();
    2.63 @@ -271,10 +275,11 @@
    2.64      @GET
    2.65      @Path("/usages")
    2.66      @Produces("text/html")
    2.67 -    public String usages(@QueryParam("path") String segment, @QueryParam("signatures") final String signatures) throws URISyntaxException, IOException, TemplateException {
    2.68 -        Map<String, Object> configurationData = usagesSubclassesImpl(segment, signatures, "files", new ComputeSegmentData() {
    2.69 +    public String usages(@Context UriInfo uriInfo, @QueryParam("path") String segment, @QueryParam("signatures") final String signatures) throws URISyntaxException, IOException, TemplateException {
    2.70 +        final String urlBase = URL_BASE_OVERRIDE != null ? URL_BASE_OVERRIDE : uriInfo.getBaseUri().toString();
    2.71 +        Map<String, Object> configurationData = usagesSubclassesImpl(urlBase, segment, signatures, "files", new ComputeSegmentData() {
    2.72              @Override public Object compute(String currentSegment) throws URISyntaxException, TemplateException, IOException {
    2.73 -                URI u = new URI(URL_BASE + "/usages/search?path=" + escapeForQuery(currentSegment) + "&signatures=" + escapeForQuery(simplify(signatures)));
    2.74 +                URI u = new URI(urlBase + "index/usages/search?path=" + escapeForQuery(currentSegment) + "&signatures=" + escapeForQuery(simplify(signatures)));
    2.75                  List<String> files = new ArrayList<String>(WebUtilities.requestStringArrayResponse(u));
    2.76                  Collections.sort(files);
    2.77                  return files;
    2.78 @@ -287,15 +292,16 @@
    2.79      @GET
    2.80      @Path("/implements")
    2.81      @Produces("text/html")
    2.82 -    public String impl(@QueryParam("path") String segment, @QueryParam("type") String typeSignature, @QueryParam("method") final String methodSignature) throws URISyntaxException, IOException, TemplateException {
    2.83 +    public String impl(@Context UriInfo uriInfo, @QueryParam("path") String segment, @QueryParam("type") String typeSignature, @QueryParam("method") final String methodSignature) throws URISyntaxException, IOException, TemplateException {
    2.84 +        final String urlBase = URL_BASE_OVERRIDE != null ? URL_BASE_OVERRIDE : uriInfo.getBaseUri().toString();
    2.85          Map<String, Object> configurationData;
    2.86  
    2.87          if (typeSignature != null) {
    2.88              final String type = strip(typeSignature, "CLASS:", "INTERFACE:", "ENUM:", "ANNOTATION_TYPE:");
    2.89 -            configurationData = usagesSubclassesImpl(segment, typeSignature, "implementors", new ComputeSegmentData() {
    2.90 +            configurationData = usagesSubclassesImpl(urlBase, segment, typeSignature, "implementors", new ComputeSegmentData() {
    2.91                  @Override
    2.92                  public Object compute(String currentSegment) throws URISyntaxException, TemplateException, IOException {
    2.93 -                    URI u = new URI(URL_BASE + "/implements/search?path=" + escapeForQuery(currentSegment) + "&type=" + escapeForQuery(type));
    2.94 +                    URI u = new URI(urlBase + "index/implements/search?path=" + escapeForQuery(currentSegment) + "&type=" + escapeForQuery(type));
    2.95                      Map<String, List<Map<String, String>>> data = Pojson.load(HashMap.class, u);
    2.96                      List<Map<String, String>> implementors = new ArrayList<Map<String, String>>();
    2.97                      for (Entry<String, List<Map<String, String>>> relpath2ImplementorsE : data.entrySet()) {
    2.98 @@ -320,10 +326,10 @@
    2.99              configurationData.put("isSubtypes", true);
   2.100          } else {
   2.101              final String method = methodSignature.substring(0, methodSignature.length() - 1);
   2.102 -            configurationData = usagesSubclassesImpl(segment, methodSignature, "implementors", new ComputeSegmentData() {
   2.103 +            configurationData = usagesSubclassesImpl(urlBase, segment, methodSignature, "implementors", new ComputeSegmentData() {
   2.104                  @Override
   2.105                  public Object compute(String currentSegment) throws URISyntaxException, TemplateException, IOException {
   2.106 -                    URI u = new URI(URL_BASE + "/implements/search?path=" + escapeForQuery(currentSegment) + "&method=" + escapeForQuery(method));
   2.107 +                    URI u = new URI(urlBase + "index/implements/search?path=" + escapeForQuery(currentSegment) + "&method=" + escapeForQuery(method));
   2.108                      Map<String, List<Map<String, String>>> data = Pojson.load(HashMap.class, u);
   2.109                      List<Map<String, String>> implementors = new ArrayList<Map<String, String>>();
   2.110                      for (Entry<String, List<Map<String, String>>> relpath2ImplementorsE : data.entrySet()) {
   2.111 @@ -351,10 +357,10 @@
   2.112          return FreemarkerUtilities.processTemplate("org/netbeans/modules/jackpot30/backend/ui/implementors.html", configurationData);
   2.113      }
   2.114  
   2.115 -    private Map<String, Object> usagesSubclassesImpl(String segment, String elementSignature, String dataKey, ComputeSegmentData computeSegmentData) throws URISyntaxException, TemplateException, IOException {
   2.116 +    private Map<String, Object> usagesSubclassesImpl(String urlBase, String segment, String elementSignature, String dataKey, ComputeSegmentData computeSegmentData) throws URISyntaxException, TemplateException, IOException {
   2.117          List<Map<String, String>> segments2Process = new ArrayList<Map<String, String>>();
   2.118  
   2.119 -        for (Map<String, String> m : list().values()) {
   2.120 +        for (Map<String, String> m : list(urlBase).values()) {
   2.121              if (segment != null) {
   2.122                  if (segment.equals(m.get("segment"))) {
   2.123                      segments2Process.add(m);
   2.124 @@ -410,10 +416,10 @@
   2.125          return elementDisplayName.toString();
   2.126      }
   2.127  
   2.128 -    private static Map<String, Map<String, String>> list() throws URISyntaxException {
   2.129 +    private static Map<String, Map<String, String>> list(String urlBase) throws URISyntaxException {
   2.130          Map<String, Map<String, String>> result = new LinkedHashMap<String, Map<String, String>>();
   2.131  
   2.132 -        for (String enc : WebUtilities.requestStringArrayResponse(new URI(URL_BASE + "/list"))) {
   2.133 +        for (String enc : WebUtilities.requestStringArrayResponse(new URI(urlBase + "index/list"))) {
   2.134              Map<String, String> rootDesc = new HashMap<String, String>();
   2.135              String[] col = enc.split(":", 2);
   2.136