Last active
March 10, 2017 12:02
-
-
Save gorshkov-leonid/895e34925722e753f2ce979380e2a9af to your computer and use it in GitHub Desktop.
SWARM: JAX-RS
This file contains hidden or 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
@ApplicationPath("/components") | |
public class JaxRsActivator extends Application { | |
//all resources will be loaded automatically (see Main.java) | |
} |
This file contains hidden or 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
public class Main { | |
public static void main(String[] args) throws Exception { | |
Swarm swarm = new Swarm(); | |
//.... | |
JAXRSArchive jaxrs = ShrinkWrap.create(JAXRSArchive.class, "time.war"); | |
/* ===> */jaxrs.addPackage(TimeResource.class.getPackage()); | |
jaxrs.addAllDependencies(); | |
jaxrs.staticContent(); | |
jaxrs.as(TopologyArchive.class).advertise(); | |
swarm.start().deploy(jaxrs); | |
} | |
} |
This file contains hidden or 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
import org.jboss.vfs.VFS; | |
import org.jboss.vfs.VirtualFile; | |
import javax.ws.rs.GET; | |
import javax.ws.rs.Path; | |
import javax.ws.rs.PathParam; | |
import javax.ws.rs.Produces; | |
import javax.ws.rs.core.MediaType; | |
import javax.ws.rs.core.Response; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.net.URISyntaxException; | |
import java.net.URL; | |
//JAX-RS proxy to collect all webcomponents from /resources/components (path is equal to ApplicationPath in JaxRsActivator.java) | |
@Path("/") | |
public class TimeResource | |
{ | |
public static final String COMPONENTS_FOLDER_NAME = "components"; | |
@GET | |
@Produces(MediaType.TEXT_PLAIN) | |
//get combined webcomponents html | |
// ``` | |
// <link rel="import" href="/components/compA.html"> | |
// <link rel="import" href="/components/compB.html"> | |
// ``` | |
// ... etc. | |
public Response get() throws URISyntaxException, IOException | |
{ | |
URL resource = this.getClass().getResource("/" + COMPONENTS_FOLDER_NAME); | |
VirtualFile componentsFolder = VFS.getChild(resource); | |
if (!componentsFolder.exists() || !componentsFolder.isDirectory()) | |
{ | |
return Response.ok("", MediaType.TEXT_PLAIN).build(); | |
} | |
StringBuilder sb = new StringBuilder(); | |
for (VirtualFile vf : componentsFolder.getChildren()) | |
{ | |
sb.append("<link rel=\"import\" href=\"").append(COMPONENTS_FOLDER_NAME).append("/").append(vf.getName()).append("\">\n"); | |
} | |
sb.append("<script>window.alert('!!!!!!!!!!!!')</script>"); | |
return Response.ok((Object) sb.toString(), MediaType.TEXT_PLAIN).build(); | |
} | |
@GET | |
@Produces(MediaType.TEXT_PLAIN) | |
@Path("/{name:.+}") | |
//proxy for webcomponents resource (/components/compA.html) | |
public Response get(@PathParam("name") String componentFileName) throws URISyntaxException, IOException | |
{ | |
URL resource = this.getClass().getResource("/" + COMPONENTS_FOLDER_NAME); | |
VirtualFile componentsFolder = VFS.getChild(resource); | |
//todo security ../ | |
VirtualFile child = componentsFolder.getChild(componentFileName); | |
if (!child.exists()) | |
{ | |
return Response.status(Response.Status.NOT_FOUND).build(); | |
} | |
return Response.ok((Object) child.openStream(), MediaType.TEXT_PLAIN).build(); | |
} | |
} |
This file contains hidden or 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
import org.jboss.vfs.VFS; | |
import org.jboss.vfs.VirtualFile; | |
import javax.ws.rs.GET; | |
import javax.ws.rs.Path; | |
import javax.ws.rs.PathParam; | |
import javax.ws.rs.Produces; | |
import javax.ws.rs.core.MediaType; | |
import javax.ws.rs.core.Response; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.net.URISyntaxException; | |
import java.net.URL; | |
//example: how to load text file | |
@Path("/") | |
public class TimeResource2 | |
{ | |
public static final String COMPONENTS_FOLDER_NAME = "components"; | |
@GET | |
@Produces(MediaType.TEXT_PLAIN) | |
public Response get() throws URISyntaxException, IOException | |
{ | |
Response.ResponseBuilder response = Response.ok((Object) this.getClass().getResource("/components.html")); | |
response.header("Content-Disposition", | |
"attachment; filename=\"components.html\""); | |
return response.build(); | |
return Response.ok((Object) this.getClass().getResourceAsStream("/" + COMPONENTS_FOLDER_NAME + "/components.html")).build(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment