forked from spring-projects/spring-data-rest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Visiting /jsclient/{library} (currently supports jquery) will yield a dynamically generated javascript library for interacting with the REST endpoints.
- Loading branch information
Showing
20 changed files
with
371 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
...webmvc/src/main/java/org/springframework/data/rest/webmvc/JavascriptClientController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
package org.springframework.data.rest.webmvc; | ||
|
||
import java.io.IOException; | ||
import java.nio.charset.Charset; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.core.io.ClassPathResource; | ||
import org.springframework.data.repository.support.Repositories; | ||
import org.springframework.data.rest.core.mapping.MethodResourceMapping; | ||
import org.springframework.data.rest.core.mapping.RepositoryResourceMappings; | ||
import org.springframework.data.web.PagedResourcesAssembler; | ||
import org.springframework.hateoas.EntityLinks; | ||
import org.springframework.hateoas.Link; | ||
import org.springframework.hateoas.ResourceSupport; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.util.StreamUtils; | ||
import org.springframework.util.StringUtils; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestMethod; | ||
import org.springframework.web.bind.annotation.ResponseBody; | ||
|
||
@RepositoryRestController | ||
public class JavascriptClientController extends AbstractRepositoryRestController { | ||
|
||
public static final MediaType APPLICATION_JAVASCRIPT = MediaType.valueOf("application/javascript"); | ||
public static final String BASE_MAPPING = "/jsclient"; | ||
private static final Logger LOG = LoggerFactory.getLogger(JavascriptClientController.class); | ||
|
||
private final Repositories repositories; | ||
private final EntityLinks entityLinks; | ||
private final RepositoryResourceMappings mappings; | ||
|
||
@Autowired | ||
public JavascriptClientController(PagedResourcesAssembler<Object> assembler, Repositories repositories, | ||
EntityLinks entityLinks, RepositoryResourceMappings mappings) { | ||
|
||
super(assembler); | ||
|
||
this.repositories = repositories; | ||
this.entityLinks = entityLinks; | ||
this.mappings = mappings; | ||
} | ||
|
||
@ResponseBody | ||
@RequestMapping(value = BASE_MAPPING, method = RequestMethod.GET) | ||
public ResourceSupport listSdks() { | ||
|
||
LOG.error("Trying to look something up..."); | ||
|
||
ResourceSupport resourceSupport = new ResourceSupport(); | ||
resourceSupport.add(new Link(BASE_MAPPING + "/{library}", "library")); | ||
return resourceSupport; | ||
} | ||
|
||
@ResponseBody | ||
@RequestMapping(value = BASE_MAPPING + "/{library}", method = RequestMethod.GET, produces = "application/javascript") | ||
public String createJavascriptRepository( | ||
@PathVariable String library) throws IOException { | ||
|
||
List<String> modules = new ArrayList<String>(); | ||
|
||
for (Class<?> domainType : repositories) { | ||
if (mappings.exportsMappingFor(domainType)) { | ||
LOG.error("Building " + library + " for " + domainType.getName() + "..."); | ||
|
||
List<String> functions = new ArrayList<String>(); | ||
|
||
final String url = mappings.getMappingFor(domainType).getPath().toString(); | ||
|
||
for (String file : new String[]{"/findAll.js", "/post.js", "/put.js", "/patch.js", "/delete.js"}) { | ||
final String code = loadJavascriptFunction(library + file, url); | ||
functions.add(StringUtils.trimTrailingWhitespace(indent(code))); | ||
} | ||
|
||
for (MethodResourceMapping method : | ||
mappings.getSearchResourceMappings(domainType)) { | ||
if (method.getParameterNames().size() > 0) { | ||
final List<String> javascriptParams = new ArrayList<String>(); | ||
for (String paramName : method.getParameterNames()) { | ||
javascriptParams.add(paramName + ": " + paramName); | ||
} | ||
|
||
final String code = loadJavascriptFunction(library + "/findBy.js", url) | ||
.replace("__ARGS__", StringUtils. | ||
collectionToCommaDelimitedString(method.getParameterNames())) | ||
.replace("__METHOD__", method.getMethod().getName()) | ||
.replace("__INPUTS__", StringUtils. | ||
collectionToCommaDelimitedString(javascriptParams)); | ||
|
||
functions.add(StringUtils.trimTrailingWhitespace(indent(code))); | ||
} else { | ||
throw new RuntimeException(method.getMethod().getName() + " doesn't have any args!"); | ||
} | ||
} | ||
|
||
String module = StringUtils.trimTrailingWhitespace(indent(StringUtils.uncapitalize(domainType.getSimpleName()) + "Repository: {\n" + | ||
StringUtils.collectionToDelimitedString(functions, ",\n") + | ||
"\n}")); | ||
modules.add(module); | ||
} | ||
} | ||
|
||
String javascript = "var sdr = {\n" + | ||
StringUtils.collectionToDelimitedString(modules, ",\n") | ||
+ "\n};"; | ||
|
||
return javascript; | ||
} | ||
|
||
/** | ||
* Embed four spaces in front of each line. This can be run more than once if need be. | ||
* @param code | ||
* @return | ||
*/ | ||
private String indent(String code) { | ||
|
||
String indentedCode = ""; | ||
for (String line : code.split("\n")) { | ||
indentedCode += " " + line + "\n"; | ||
} | ||
return indentedCode; | ||
} | ||
|
||
/** | ||
* Load a javascript code template and plug in the URL | ||
* @param filename | ||
* @param url | ||
* @return | ||
* @throws IOException | ||
*/ | ||
private String loadJavascriptFunction(String filename, String url) throws IOException { | ||
|
||
ClassPathResource resource = new ClassPathResource(filename, JavascriptClientController.class); | ||
final String code = StreamUtils.copyToString(resource.getInputStream(), Charset.defaultCharset()); | ||
return code.replace("__URL__", url); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.