How can I serve static content with Glassfish embedded? - java

I'm trying to setup Glassfish embedded with a WAR project that implements a REST API and then some static Javascript content that calls it. I got the WAR to deploy and the REST API is available with a context root of "/Users".
How can I use Glassfish to serve static content with a context root of "/". So for example, if the user requests http://myserver.com/Users/some-REST-call it routes to the WAR application and http://myserver.com/somefile.js serves a static file from some directory?
Here's my Main class file so far:
public class Main{
public static void main(String[] args) throws Exception {
String port = System.getenv("PORT");
port = port != null ? port : "8080";
GlassFishProperties gfProps = new GlassFishProperties();
gfProps.setPort("http-listener", Integer.parseInt(port));
GlassFish glassfish = GlassFishRuntime.bootstrap().newGlassFish(gfProps);
glassfish.start();
Deployer deployer = glassfish.getDeployer();
deployer.deploy(new File("target/Users-Rest.war"));
}
}
P.S. I'm aware that ideally you'd use Apache to serve the static content, however, I'm using Glassfish embedded to try to deploy to Heroku. Thanks a ton for your help!

Related

multiple servlet in jetty

I am new to Jetty and trying to understand by online example program. Here is the sample program I used:
public class EmbeddedJettyMain {
public static void main(String[] args) throws Exception {
Server server = new Server(7070);
ServletContextHandler handler = new ServletContextHandler(server, "/example");
handler.addServlet(ExampleServlet.class, "/");
server.start();
}
}
With that I can use:
http://localhost:7070/example/
Now I want to add one more servlet URI
http://localhost:7070/example2
How can I do this ?
I can see some reference such as webapp, looking for a good approach.
Server server = new Server(7070);
ServletContextHandler handler = new ServletContextHandler(server, "/");
handler.addServlet(ExampleServlet.class, "/example");
handler.addServlet(ExampleServlet.class, "/example2");
Each addServlet creates a mapping. Jetty will create an instance of the Servlet that will be a singleton for each mapping, meaning that init(ServletConfig config) will only be called once in each instance and all requests to a mapping go to the same instance.
Jetty provides a Web server and javax.servlet container.
Your servlets are stored and served via jetty's embedded container to serve when needed.

In Java EE 6, how do I get the URL of a JAX-RS endpoint programmatically?

TomEE 1.7.2, Java EE 6, Java 8
I have this JAX-RS Application:
#ApplicationPath("/api")
public class CallStatsCatcherApplication extends Application {
#Override
public Set<Class<?>> getClasses() {
return new HashSet<Class<?>>(Arrays.asList(RestEndpoint.class));
}
}
#ApplicationScoped
#Path("/rest")
public class RestEndpoint {
#GET
public String echo(#QueryParam("foo") String foo) {
return foo;
}
}
During startup, TomEE prints:
INFO: GET http://localhost:8080/test-application/api/rest/ -> String echo(String)
How can I get that URL programmatically during startup? I'd like to create a framework that advertises this URL locally on the network.
To get server adress part you can try use something like that:
Java EE: how to get the URL of my application?
But if you need IP address in startup bean, you can try use:
InetAddress ip = InetAddress.getLocalHost();
String ipAddress = ip.getHostAddress();`
Another option is to use:
MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer()`
But it may be associated with the server platform and you will need to search the property.
You can use the UriInfo class in conjunction with UriBuilder class to mount or create URLs using the current path.
There is similar question in stackoverflow: getting the base url...

What is URL of WSDL of EJB WebService (JAX-WS)?

As far as I understood, you can introduce:
#Stateless
#WebService
public class MyWebServiceEndpoint {
#Inject SomeBean aBean;
#WebMethod
public String getSomething() {
return "something";
}
}
and when application deployed, the WebService is exposed in the Application Server (such as WebSphere). Then what is the URL of WSDL, where other applications can find my service?
The URL of your WSDL on the server should be in the following format:
http://hostname:port/contextRoot/MyWebServiceEndpoint?WSDL
But this is running under the assumption that the url-pattern attribute of the endpoint entity in your sun-jaxws.xml file uses the same name as your web service class.
Just a hint to the first answer/URL,
In some cases the "context Root" get excluded from the webservice url after deployment.
I can't say for other application servers but for WebLogic and EJB web service URL follow the following default pattern:
http://host:port/'className'/'className'+Service
where 'className' is the simple name of the Java class implementing the web service.
You can easily override the end of the URL by setting the serviceName attribute in the #WebService annotation. If you need to change the root context you must package your web service as an EJB jar embedded in an EAR and use the WebLogic specific deployement descriptor (which I would avoid at all costs).
Hope it helps :)
you need to publish it, You can have a Publisher class like this:
import javax.xml.ws.Endpoint;
//Endpoint publisher
public class Publisher {
public static void main(String[] args) {
Endpoint.publish("http://localhost:9999/webserv/Test", new FilenetWebService());
}
}

Different web service object after each call

I am new with Java EE and SOAP. I have tried to create a simple web service application and its client (environment: NetBeans 7.2.1 IDE, GlassFish Server 3.1, Java 1.6).
Web service code:
package simplews;
import javax.jws.*;
#WebService(serviceName = "SimpleWebService")
public class SimpleWebService {
String something = null;
#WebMethod(operationName = "setSomething")
#Oneway
public void setSomething(#WebParam(name = "smth") String smth) {
something = smth;
}
#WebMethod(operationName = "getSomething")
public String getSomething() {
return something;
}
}
Client application code:
package simpleclientapp;
import simplews.*;
public class SimpleClientApp {
public static void main(String[] args) {
SimpleWebService_Service service = new SimpleWebService_Service();
SimpleWebService port = service.getSimpleWebServicePort();
port.setSomething("trololo");
String smth = port.getSomething();
System.out.println(smth);
}
}
Unfortunately, the client application printed out null. After short investigation I have realised, that on the server side a new SimpleWebService object is created for each client call (sounds like stateless approach).
What is wrong here? Why the client port does not refer to the same WS object for each call?
Web services are stateless by nature. In order to keep state between requests, you have to persist the data (in a file,database etc.).
You're right, JAX-WS web services are stateless by default and you can't rely on something thatviolates this premise. Follow a different approach in storing such values. You can read this doc Java TM API for XML Web Services (JAX-WS) Stateful Web Service with JAX-WS RI, if you really want to follow the direction in your post.

Context of RESTlet Application is null using internal RESTlet Server

I have a Restlet (2.0.10) application, I start with the following code:
public static void main(final String[] args) {
try {
// Create a new Component
final Component component = new Component();
// Add a new HTTP server listening on port 8182
component.getServers().add(Protocol.HTTP, SERVER_PORT);
// Add a client protocol connector for static files
component.getClients().add(Protocol.FILE);
// Attach the sample application.
component.getDefaultHost().attach("/myApp", new MyApplication(Context.getCurrent()));
// Start the component.
component.start();
} catch (Exception e) {
LOGGER.error(e);
}
}
Now I require the applications root (i.e. /myApp) inside the application and I try to get this according to Java accessing ServletContext from within restlet Resource:
Client serverDispatcher = context.getServerDispatcher();
ServletContext servletContext = (ServletContext)serverDispatcher.getContext().getAttributes()
.get("org.restlet.ext.servlet.ServletContext");
String contextPath = servletContext.getContextPath();
This works perfectly fine while deploying my application to a Tomcat Server, but as soon as I start the server using a Component as shown above, my Context is always null. Can someone please tell me how to get a properly initialized context using restlets internal server capabilities?
Seems logic.
You want a servlet context but you are not running in a servlet container, so the servlet context is NULL.
When doing component.start() you are using the Restlet connectors to server HTTP/HTTPS requests, not a servlet container like Tomcat.
You would need to pick up the context from the Component Class:
component.getDefaultHost().attach("/myApp",
new MyApplication(component.getContext().createChildContext());
That would just give you the Restlet context, but Servlet Context still won't be available since this is a standalone Java application.

Categories

Resources