Simple Embedded Tomcat 10 Example - java

I'm trying to get a simple embedded tomcat 10.1.0-M11 example working but I keep getting localhost refused to connect when I go to http://localhost:8080/aa. There is no StackOverflow label yet for embedded-tomcat-10.
Here is my code:
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import org.apache.catalina.Context;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.startup.Tomcat;
public class App {
public static void main(String[] args) throws LifecycleException {
Tomcat tomcat = new Tomcat();
tomcat.setBaseDir("temp");
tomcat.setPort(8080);
String contextPath = "";
String docBase = new File(".").getAbsolutePath();
Context context = tomcat.addContext(contextPath, docBase);
class SampleServlet extends HttpServlet {
#Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
PrintWriter writer = resp.getWriter();
writer.println("<html><title>Welcome</title><body>");
writer.println("<h1>Have a Great Day!</h1>");
writer.println("</body></html>");
}
}
String servletName = "SampleServlet";
String urlPattern = "/aa";
tomcat.addServlet(contextPath, servletName, new SampleServlet());
context.addServletMappingDecoded(urlPattern, servletName);
tomcat.start();
tomcat.getServer().await();
}
}
Dependencies
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>3.0-alpha-1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>10.1.0-M11</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>10.1.0-M11</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jasper</artifactId>
<version>10.1.0-M11</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jasper-el</artifactId>
<version>10.1.0-M11</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jsp-api</artifactId>
<version>10.1.0-M11</version>
</dependency>
</dependencies>
UPDATE
I have gone through every single version I found that this bug was introduced in version 9.0.0.M4 and never resolved since then. Any newer version renders this example useless.

I have added 2-solutions and you can choose anyone so your final main function will be like the following:
public static void main(String[] args) throws LifecycleException {
Tomcat tomcat = new Tomcat();
tomcat.setBaseDir("temp");
// Solution 1
//tomcat.setPort(8080);
//tomcat.getConnector();
// Solution 2, make server listen on 2 ports
Connector connector1 = tomcat.getConnector();
connector1.setPort(8080);
Connector connector2 = new Connector();
connector2.setPort(8090);
String contextPath = "";
String docBase = new File(".").getAbsolutePath();
Context context = tomcat.addContext(contextPath, docBase);
class SampleServlet extends HttpServlet {
#Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
PrintWriter writer = resp.getWriter();
writer.println("<html><title>Welcome</title><body>");
writer.println("<h1>Have a Great Day!</h1>");
writer.println("</body></html>");
}
}
String servletName = "SampleServlet";
String urlPattern = "/aa";
tomcat.addServlet(contextPath, servletName, new SampleServlet());
context.addServletMappingDecoded(urlPattern, servletName);
tomcat.start();
tomcat.getService().addConnector(connector1);
tomcat.getService().addConnector(connector2);
tomcat.getServer().await();
}

As explained in this question, you need to add a Connector, which can be done like this:
...
tomcat.setPort(8080);
tomcat.getConnector();
...

Related

Embedded Jersey 3 with Jetty into existing Application

i am trying to embed a HTTP-Server into an existing java Application. My goal is to create a small rest API as an interface to send commands to the server application it runs in.
I planned using Jakarta and Jersey 3 with Jetty as embeded HTTP-Server. My starting point was the following topic which was for Jersey 2 but i tried my luck: Embed jersey in java application
My problem is that i get 404 Not Found back when i try to call http://localhost/login/status in my browser. The page is blank. When i switch to using Grizzly2 as embedded HTTP-Server and type the url in the browser, the result is the same. The only difference in Grizzly2 i could spot is when i only call http://localhost/ i get an error page additionally to the 404 Not Found response back. As soon as i add /login to the url, i get the 404 Not Found response without an error page. What could be the reason the server does not pick up my resources?
I am using the Eclipse IDE. First i created a clean Maven project, added the following dependencies and created my test code:
org.glassfish.jersey.core -> jersey-server
org.glassfish.jersey.containers -> jersey-container-jetty-http
On my first startup i got some missing class errors, searched for the dependencies they are in and added them to the pom. Following is my current test code.
<!-- pom.xml -->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test-ee</groupId>
<artifactId>test-ee-embed</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>3.0.4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-bom</artifactId>
<version>3.0.2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>5.0.0</version>
</dependency>
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-jetty-http</artifactId>
</dependency>
</dependencies>
</project>
// LoginserverRestApi.java
package de.l2d;
import org.glassfish.jersey.server.ResourceConfig;
import jakarta.ws.rs.ApplicationPath;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
#ApplicationPath("/login")
public class LoginserverRestApi extends ResourceConfig {
#GET #Path("/status")
public String status() {
// TODO Return real server statistics.
return "{\"status\":\"ok\"}";
}
}
// RestApiServer.java
package de.l2d;
import java.net.URI;
import org.eclipse.jetty.server.Server;
import org.glassfish.jersey.jetty.JettyHttpContainerFactory;
import jakarta.ws.rs.core.UriBuilder;
public class RestApiServer {
private Server server;
private RestApiServer() {
URI baseUri = UriBuilder.fromUri("http://localhost/").build();
server = JettyHttpContainerFactory.createServer(baseUri, new LoginserverRestApi(), false);
}
public void start() throws Exception {
server.start();
}
public void stop() throws Exception {
server.stop();
}
private static final class SingletonHolder {
protected static RestApiServer instance = new RestApiServer();
}
public static RestApiServer getInstance() {
return SingletonHolder.instance;
}
}
// Main.java
package de.l2d;
public class Main {
public static void main(String[] args) throws Exception {
RestApiServer.getInstance().start();
Thread.currentThread().join();
RestApiServer.getInstance().stop();
}
}
I found the solution to my problem. However, i do not know why it behaves like this.
I had to use the Path annotation instead of the ApplicationPath annotation on the LoginserverRestApi class and additionally construct a ResourceConfig with LoginserverRestApi.class as parameter and pass that Object to the JettyHttpContainerFactory.createServer static method. Following are the two files which differ from the initial post:
// LoginserverRestApi.java
package de.l2d;
import org.glassfish.jersey.server.ResourceConfig;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
#Path("/login")
public class LoginserverRestApi extends ResourceConfig {
#GET #Path("/status")
public String status() {
// TODO Return real server statistics.
return "{\"status\":\"ok\"}";
}
}
// RestApiServer.java
package de.l2d;
import java.net.URI;
import org.eclipse.jetty.server.Server;
import org.glassfish.jersey.jetty.JettyHttpContainerFactory;
import org.glassfish.jersey.server.ResourceConfig;
import jakarta.ws.rs.core.UriBuilder;
public class RestApiServer {
private Server server;
private RestApiServer() {
URI baseUri = UriBuilder.fromUri("http://localhost/").build();
ResourceConfig config = new ResourceConfig(LoginserverRestApi.class);
server = JettyHttpContainerFactory.createServer(baseUri, config, false);
}
public void start() throws Exception {
server.start();
}
public void stop() throws Exception {
server.stop();
}
private static final class SingletonHolder {
protected static RestApiServer instance = new RestApiServer();
}
public static RestApiServer getInstance() {
return SingletonHolder.instance;
}
}

File upload with Jersey 2 and Jetty

Hello everyone.
I developed a webservice which runs on Jetty with a RESTful API using Jersey 2.
I later had to create a file upload method (mainly for XLS/XML files) and I tried to use Jersey 2's Multipart libraries for it.
However, as the server starts, it throws immediately a strange exception:
2016-09-21 01:13:13.578:INFO:oejs.AbstractConnector:main: Started ServerConnector#17f62e33{HTTP/1.1,[http/1.1]}{0.0.0.0:8080}
Exception in thread "main" java.lang.NoClassDefFoundError: org/glassfish/jersey/internal/inject/ExtractorException
at org.glassfish.jersey.media.multipart.internal.FormDataParamValueFactoryProvider.createValueFactory(FormDataParamValueFactoryProvider.java:436)
[.............................................]
Jetty Server
public static void main(String[] args) {
final ResourceConfig resourceConfig = new ResourceConfig(API.class);
resourceConfig.packages("the_package_where_these_classes_are");
resourceConfig.register(MultiPartFeature.class);
ServletHolder jerseyServlet
= new ServletHolder(new ServletContainer(resourceConfig));
Server jettyServer = new Server(8080);
ServletContextHandler context = new ServletContextHandler(jettyServer, "/");
context.addServlet(jerseyServlet, "/*");
try {
jettyServer.start();
jettyServer.join();
} catch (Exception e) {
e.printStackTrace();
} finally {
//jettyServer.destroy();
// got an IllegalStateException uncommenting this and didn't quite understand why
}
}
Endpoints/Jersey/JAX-RS
import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataParam;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.InputStream;
#Path("/")
public class API {
#POST
#Path("test")
#Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(#FormDataParam("file") InputStream uploadedInputStream,
#FormDataParam("file") FormDataContentDisposition fileDetails) throws Exception {
System.out.println(fileDetails.getFileName());
return Response.ok().build();
}
}
Application configs
import org.glassfish.jersey.media.multipart.MultiPartFeature;
import javax.ws.rs.core.Application;
import java.util.Set;
public class ApplicationConfig extends Application {
#Override
public Set<Class<?>> getClasses() {
Set<Class<?>> resources = new java.util.HashSet<>();
resources.add(MultiPartFeature.class);
resources.add(API.class);
return resources;
}
}
Maven dependencies
<dependencies>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>9.3.11.v20160721</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.glassfish.jersey.media/jersey-media-multipart -->
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>2.23.2</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
<version>2.23.2</version>
</dependency>
</dependencies>
Am I using some wrong dependency? Shouldn't this simple service work? I never used Multipart before, so I tried to go by several Internet sources/tutorials.
Thanks in advance!
FIXED
I changed the version to latest in all Jersey dependencies and it worked!
jersey-media-multipart depends on jersey-common , add
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-common</artifactId>
<version>2.23.2</version>
</dependency>
For me worked like below:
final ResourceConfig resourceConfig = new ResourceConfig(ApplicationConfig.class);
resourceConfig.packages("com.econorma.rest");
resourceConfig.register(MultiPartFeature.class);
ServletHolder jerseyServlet = new ServletHolder(new ServletContainer(resourceConfig));
This is ApplicationConfig class
#ApplicationPath("/")
public class ApplicationConfig extends Application {
#Override
public Set<Class<?>> getClasses() {
final Set<Class<?>> resources = new HashSet<Class<?>>();
resources.add(MultiPartFeature.class);
resources.add(EntryPoint.class);
return resources;
}
#Override
public Map<String, Object> getProperties() {
Map<String, Object> properties = new HashMap<String, Object>();
properties.put("jersey.config.server.provider.packages", "com.econorma.rest");
return properties;
}
}
Jersey provides examples for corresponding versions. You can add below dependency to project and by using 'Download sources' feature of IDE you can download source code for this example.By clicking pom parent you can see all examples for same jersey version as well.Hope it helps!
<dependency>
<groupId>org.glassfish.jersey.examples</groupId>
<artifactId>multipart-webapp</artifactId>
<version>${jersey.version}</version>
</dependency>

Get Instance of WebTarget in OSGi

I am running some tests n OSGi and trying to create an instance of WebTarget to test the endpoints published in OSGi with the following code:
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
...
...
private static final String port = "8080";
private static final String CONTEXT = "/someContext";
private static final URI baseUri = URI.create("http://localhost:" + port + CONTEXT);
#Test
public void sentence() {
Client c = ClientBuilder.newClient();
final WebTarget target = c.target(baseUri);
Response response = target.path("/service").request().post(Entity.entity(new MockMessage("123", "sessionId123"), MediaType.APPLICATION_JSON_TYPE), Response.class);
logger.info("JERSEY RESULT = " + response.toString());
assertEquals(Response.ok().build(), response);
}
The problem is that when it runs it then throws the exception java.lang.ClassNotFoundException: Provider org.glassfish.jersey.internal.RuntimeDelegateImpl could not be instantiated: java.lang.IllegalStateException: No generator was provided and there is no default generator registered. And it comes worst because the missing class (RuntimeDelegateImpl) is under the internal package of the jersey-common-2.x.jar bundle.
So, has anyone an idea on how to create an instance of a WebTarget under OSGi?
BTW, I already have these two dependencies in the pom file:
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-common</artifactId>
<version>2.22.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0.1</version>
<scope>test</scope>
</dependency>
As I am using Arquillian to run the tests under OSGi, then I finally solved it by adding the annotation #RunAsClient to run the test outside the OSGi container and avoid dependencies not found in the .internal package.

Connecting to Solr 1.4 with SolrJ client connector 3.6

How do I connect to Solr 1.4 search server configured with Basic Auth using the SolrJ 3.6 connector?
Based on the instructions on using SolrJ :
Fetch relevant dependencies for e.g. using maven:
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>3.6.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.2.2</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.1.2</version>
</dependency>
A sample application connecting to Solr 1.4 using SolrJ 3.6 with the latest Apache Http Components http client to negotiate Basic Auth:
import java.io.IOException;
import java.util.Iterator;
import org.apache.http.HttpException;
import org.apache.http.HttpRequest;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.AbstractHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HttpContext;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.impl.XMLResponseParser;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.client.solrj.response.UpdateResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
/**
* Sample app
*
*/
public class App
{
public static void main( String[] args ) throws SolrServerException, IOException
{
String url = "https://localhost:8080/solr/";
String httpAuthUser = "solr_admin";
String httpAuthPass= "somePassword";
// Configure latests Apache Http Components http client
HttpClient httpClient = new DefaultHttpClient();
if (httpAuthUser != null && httpAuthPass != null) {
AbstractHttpClient client = (AbstractHttpClient) httpClient;
App a = new App();
client.addRequestInterceptor(a.new PreEmptiveBasicAuthenticator(httpAuthUser, httpAuthPass));
}
// Configure XMLResponseParser as standard javabin parser does not work with 1.4
SolrServer solr = new HttpSolrServer(url, httpClient, new XMLResponseParser());
// Test adding some data
SolrInputDocument document = new SolrInputDocument();
document.addField("id", "552199");
document.addField("name", "Gouda cheese wheel");
document.addField("price", "49.99");
UpdateResponse uresponse = solr.add(document);
System.out.println("UpdateResponse"+uresponse.getStatus());
solr.commit();
// Query for the data just added
SolrQuery parameters = new SolrQuery();
parameters.set("q", "*");
QueryResponse response = solr.query(parameters);
SolrDocumentList list = response.getResults();
Iterator<SolrDocument> si = list.iterator();
System.out.println("Solr document"+list.getNumFound());
while(si.hasNext()){
System.out.println("Solr document"+si.next().toString());
}
}
protected class PreEmptiveBasicAuthenticator implements HttpRequestInterceptor {
private final UsernamePasswordCredentials credentials;
public PreEmptiveBasicAuthenticator(String user, String pass) {
credentials = new UsernamePasswordCredentials(user, pass);
}
public void process(HttpRequest request, HttpContext context)
throws HttpException, IOException {
request.addHeader(BasicScheme.authenticate(credentials,"US-ASCII",false));
}
}
}

Is it possible to do an embedded Java servlet code (like using libraries Jetty) in Tomcat libraries?

Suppose I have the following example of an embedded Java servlet in Jetty libraries - is it possible to do an equivalent using the Tomcat API libraries?
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.AbstractHandler;
public class HelloHandler extends AbstractHandler
{
public void handle(String target,Request baseRequest,HttpServletRequest request,HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html;charset=utf-8");
response.setStatus(HttpServletResponse.SC_OK);
baseRequest.setHandled(true);
response.getWriter().println("<h1>Hello World</h1>");
}
public static void main(String[] args) throws Exception
{
Server server = new Server(8080);
server.setHandler(new HelloHandler());
server.start();
server.join();
}
}
A Tomcat7 embedded equivalent would look like this:
public class TinyTomcat7Embedded {
public static void main(String[] args) {
Tomcat tomcat = new Tomcat();
tomcat.setPort(8080);
File baseDir = new File("tomcat");
tomcat.setBaseDir(baseDir.getAbsolutePath());
File applicationDir = new File(baseDir + "/webapps", "/ROOT");
if (!applicationDir.exists()) {
applicationDir.mkdirs();
}
try {
Context appContext = tomcat.addWebapp("/", "ROOT");
// A Jetty AbstractHandler is an HttpServlet here:
Tomcat.addServlet(appContext, "helloWorldServlet", new HttpServlet() {
#Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
response.setStatus(HttpServletResponse.SC_OK);
response.getWriter().println("<h1>Hello World</h1>");
}
});
appContext.addServletMapping("/helloworld", "helloWorldServlet");
tomcat.start();
System.out.println("Tomcat server: http://" + tomcat.getHost().getName() + ":" + 8080 + "/");
tomcat.getServer().await();
} catch (ServletException e) {
e.printStackTrace();
} catch (LifecycleException e) {
e.printStackTrace();
}
}
}
Add these dependencies to your pom.xml:
<!-- Eclipse Java Compiler -->
<dependency>
<groupId>org.eclipse.jdt.core.compiler</groupId>
<artifactId>ecj</artifactId>
<version>3.7.1</version>
</dependency>
<!-- Tomcat Embedded -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>7.0.26</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-logging-juli</artifactId>
<version>7.0.26</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-logging-log4j</artifactId>
<version>7.0.26</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>7.0.26</version>
</dependency>

Categories