Deploy Static content in jetty - java

I am trying deploy the app static in jetty, but using xml config file, because i am using virtual hosts.
I create this file xml for deploy:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN"
"http://www.eclipse.org/jetty/configure.dtd">
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
<Set name="contextPath">/mail</Set>
<!--<Call name="setInitParameter">
<Arg>org.eclipse.jetty.servlet.Default.useFileMappedBuffer</Arg>
<Arg>false</Arg>
</Call> -->
<Set name="handler">
<New class="org.eclipse.jetty.server.handler.ResourceHandler">
<Set name="resourceBase">/ccmail</Set>
<Set name="directoriesListed">true</Set>
</New>
</Set>
<Set name="virtualHosts">
<Array type="java.lang.String">
<Item>apps.cairunet.ad.br</Item>
</Array>
</Set>
</Configure>
The name for my folder app is ccmail. Is located in webapps/ccmail
I already try pass to path for:
<Set name="resourceBase">/ccmail</Set>
<Set name="resourceBase">ccmail</Set>
<Set name="resourceBase">./ccmail</Set>
<Set name="resourceBase">webapps/ccmail</Set>
<Set name="resourceBase">/webapps/ccmail</Set>
Anyone this paths not works for me.
Jetty launch this error:
2019-02-25 09:36:46.422:WARN:oejs.ServletContextHandler:main:
ServletContextHandler.setHandler should not be called dire ctly. Use
insertHandler or setSessionHandler etc. 2019-02-25
09:36:46.484:WARN:oejw.WebInfConfiguration:main: Can't generate
resourceBase as part of webapp tmp dir name:
java.lang.IllegalStateException: No resourceBase or war set for
context 2019-02-25 09:36:46.500:WARN:oejw.WebAppContext:main: Failed
startup of context o.e.j.w.WebAppContext#7d0587f1{/mail,nul
l,UNAVAILABLE,apps.cairunet.ad.br} java.lang.IllegalStateException: No
resourceBase or war set for context
at org.eclipse.jetty.webapp.WebInfConfiguration.unpack(WebInfConfiguration.java:577)
at org.eclipse.jetty.webapp.WebInfConfiguration.preConfigure(WebInfConfiguration.java:152)
at org.eclipse.jetty.webapp.WebAppContext.preConfigure(WebAppContext.java:506)
at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:544)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at org.eclipse.jetty.deploy.bindings.StandardStarter.processBinding(StandardStarter.java:46)
at org.eclipse.jetty.deploy.AppLifeCycle.runBindings(AppLifeCycle.java:192)
at org.eclipse.jetty.deploy.DeploymentManager.requestAppGoal(DeploymentManager.java:505)
at org.eclipse.jetty.deploy.DeploymentManager.addApp(DeploymentManager.java:151)
at org.eclipse.jetty.deploy.providers.ScanningAppProvider.fileAdded(ScanningAppProvider.java:180)
at org.eclipse.jetty.deploy.providers.WebAppProvider.fileAdded(WebAppProvider.java:453)
at org.eclipse.jetty.deploy.providers.ScanningAppProvider$1.fileAdded(ScanningAppProvider.java:64)
at org.eclipse.jetty.util.Scanner.reportAddition(Scanner.java:610)
at org.eclipse.jetty.util.Scanner.reportDifferences(Scanner.java:529)
at org.eclipse.jetty.util.Scanner.scan(Scanner.java:392)
at org.eclipse.jetty.util.Scanner.doStart(Scanner.java:313)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at org.eclipse.jetty.deploy.providers.ScanningAppProvider.doStart(ScanningAppProvider.java:150)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at org.eclipse.jetty.deploy.DeploymentManager.startAppProvider(DeploymentManager.java:579)
at org.eclipse.jetty.deploy.DeploymentManager.doStart(DeploymentManager.java:240)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:138)
at org.eclipse.jetty.server.Server.start(Server.java:415)
at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:117)
at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:113)
at org.eclipse.jetty.server.Server.doStart(Server.java:382)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at org.eclipse.jetty.xml.XmlConfiguration$1.run(XmlConfiguration.java:1572)
at org.eclipse.jetty.xml.XmlConfiguration$1.run(XmlConfiguration.java:1512)
at java.security.AccessController.doPrivileged(Native Method)
at org.eclipse.jetty.xml.XmlConfiguration.main(XmlConfiguration.java:1511)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.jetty.start.Main.invokeMain(Main.java:220)
at org.eclipse.jetty.start.Main.start(Main.java:490)
at org.eclipse.jetty.start.Main.main(Main.java:77) 2019-02-25 09:36:46.578:INFO:oejs.AbstractConnector:main: Started
ServerConnector#27808f31{HTTP/1.1,[http/1.1]}{0.0.0.0: 8080}
2019-02-25 09:36:46.594:INFO:oejs.Server:main: Started #2970ms

Note: be careful with your DOCTYPE, what you have declared is from Jetty 7.x thru Jetty 8.x, and is not correct for Jetty 9.x
Don't mix ResourceHandler and WebAppContext / ServletContextHandler.
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN"
"http://www.eclipse.org/jetty/configure_9_3.dtd">
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
<Set name="contextPath">/mail</Set>
<Set name="virtualHosts">
<Array type="java.lang.String">
<Item>apps.cairunet.ad.br</Item>
</Array>
</Set>
</Configure>
The most basic support is to not reference /ccmail in your <Configure>.
The fact that it exists as ${jetty.base}/webapps/ccmail/ is enough, that will deploy /ccmail as a static resource base for you.
BUT if you want to combine static resources with virtual hosts, then you can either use a WebAppContext with an alternate base, or a new ResourceHandler.
Example of alternate bases:
Serving static files from alternate path in embedded Jetty
Example of ResourceHandler usage:
https://www.eclipse.org/jetty/documentation/current/static-content-deployment.html
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN"
"http://www.eclipse.org/jetty/configure_9_3.dtd">
<Configure class="org.eclipse.jetty.server.handler.ContextHandler">
<Set name="contextPath">/ccmail</Set>
<Set name="handler">
<New class="org.eclipse.jetty.server.handler.ResourceHandler">
<Set name="resourceBase">/fully/qualified/path/to/my/jetty.base/webapps/ccmail</Set>
<Set name="directoriesListed">true</Set>
</New>
</Set>
<Set name="virtualHosts">
<Array type="java.lang.String">
<Item>apps.cairunet.ad.br</Item>
</Array>
</Set>
</Configure>

Related

Custom Logging in Jetty11

I am implementing Custom logging in Jetty11. My use case is to add custom attributes while logging. It is the follow-up question for
this
I have written a custom class 'MyJettyLogger' mentioned below that extends CustomRequestLog
package com.ashish.Jetty.customLoggerImplementation;
import org.eclipse.jetty.server.CustomRequestLog;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Response;
public class MyJettyLogger extends CustomRequestLog
{
#Override
public void log(Request request, Response response)
{
super.log(request, response);
}
public MyJettyLogger(Writer writer, String logStr)
{
super(writer, setCustomAttributesToLog(logStr));
}
private static String setCustomAttributesToLog(String logStr){
StringBuilder logBuffer = new StringBuilder(logStr);
logBuffer.append(" ");
logBuffer.append(1234);
logBuffer.append(" ");
logBuffer.append("Ashishs");
logBuffer.append(" ");
logBuffer.append("");
logBuffer.append(" dcn");
return logBuffer.toString() ;
}
}
I have also copied ${jetty.home}/etc/jetty-requestlog.xml to ${jetty.base}/etc/jetty-requestlog.xml and replaced 'com.ashish.Jetty.customLoggerImplementation.MyJettyLogger' instead of 'org.eclipse.jetty.server.CustomRequestLog' as mentioned below
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://www.eclipse.org/jetty/configure_10_0.dtd">
<!-- =============================================================== -->
<!-- Configure the Jetty Request Log -->
<!-- =============================================================== -->
<Configure id="Server" class="org.eclipse.jetty.server.Server">
<!-- =========================================================== -->
<!-- Configure Request Log for Server -->
<!-- (Use RequestLogHandler for a context specific RequestLog -->
<!-- =========================================================== -->
<Set name="RequestLog">
<New id="RequestLog" class="com.ashish.Jetty.customLoggerImplementation.MyJettyLogger">
<!-- Writer -->
<Arg>
<New class="org.eclipse.jetty.server.AsyncRequestLogWriter">
<Arg>
<Call name="resolvePath" class="org.eclipse.jetty.xml.XmlConfiguration">
<Arg><Property name="jetty.base"/></Arg>
<Arg>
<Property name="jetty.requestlog.filePath">
<Default>
<Property name="jetty.requestlog.dir" default="logs"/>/yyyy_mm_dd.request.log
</Default>
</Property>
</Arg>
</Call>
</Arg>
<Set name="filenameDateFormat"><Property name="jetty.requestlog.filenameDateFormat" default="yyyy_MM_dd"/></Set>
<Set name="retainDays"><Property name="jetty.requestlog.retainDays" default="90"/></Set>
<Set name="append"><Property name="jetty.requestlog.append" default="false"/></Set>
<Set name="timeZone"><Property name="jetty.requestlog.timezone" default="GMT"/></Set>
</New>
</Arg>
<!-- Format String -->
<Arg>
<Property name="jetty.requestlog.formatString" deprecated="jetty.customrequestlog.formatString">
<Default>
<Get class="org.eclipse.jetty.server.CustomRequestLog" name="EXTENDED_NCSA_FORMAT"/>
</Default>
</Property>
</Arg>
</New>
</Set>
</Configure>
I am testing this via Jetty stand alone server using deployed war and getting below error while starting Jetty.
I
2021-08-13 10:20:46.757:WARN :oejx.XmlConfiguration:main: Unable to execute XmlConfiguration
java.security.PrivilegedActionException: java.lang.ClassNotFoundException: com.ashish.Jetty.customLoggerImplementation.MyJettyLogger
at java.base/java.security.AccessController.doPrivileged(AccessController.java:558)
at org.eclipse.jetty.xml.XmlConfiguration.main(XmlConfiguration.java:1810)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at org.eclipse.jetty.start.Main.invokeMain(Main.java:226)
at org.eclipse.jetty.start.Main.start(Main.java:502)
at org.eclipse.jetty.start.Main.main(Main.java:73)
It might be possible that I am doing couple of mistakes here as I am exploring Jetty11.
Please help.

EXIT_ON_INIT_FAILURE in Jetty-9.3.12 standalone server

Is there any equivalent of EXIT_ON_INIT_FAILURE from Tomcat in Jetty? I have war which throws IllegalArgumentException during start up (in Spring Bean initialization), but Jetty only prints WARN from DeploymentManager (Unable to reach node goal: started) and started normally (of course with no context). I have tried options:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_3.dtd">
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
<Set name="contextPath">{context_path}</Set>
<Set name="throwUnavailableOnStartupException">true</Set>
<Set name="war">{war_path}</Set>
<Set name="maxFormContentSize">10485760</Set>
</Configure>
but with no results.
Logs from Jetty:
2017-03-16 21:45:05.798:WARN:oejd.DeploymentManager:main: Unable to reach node goal: started
java.lang.RuntimeException: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'intranetClient' defined in class path resource
{...stacktrace...}
2017-03-16 21:45:05.836:INFO:oejs.AbstractConnector:main: Started httpConnector#48140564{HTTP/1.1,[http/1.1]}{0.0.0.0:5070}
2017-03-16 21:45:05.840:INFO:oejs.AbstractConnector:main: Started httpMngConnector#4439f31e{HTTP/1.1,[http/1.1]}{0.0.0.0:5075}
2017-03-16 21:45:05.841:INFO:oejs.Server:main: Started #40994ms
Thanks in advance for any advice how it should be configured.
Your DTD is outdated.
Use:
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN"
"http://www.eclipse.org/jetty/configure_9_3.dtd">
Get rid of the block ...
<Set name="servletHandler">
<New class="org.eclipse.jetty.servlet.ServletHandler">
<Set name="startWithUnavailable">false</Set>
</New>
</Set>
The rest is all that's needed in your ${jetty.base}/webapps/${context}.xml file to trigger the failure.

How to change timezone for Jetty request log *filenames*?

I have configured request logs as seen below - in jetty.xml. However, the LogTimeZone which is set to GMT-5 below will only change the timezone of the log entries in request.yyyy_mm_dd.log but the filename of request.yyyy_mm_dd.log doesn't reflect "GMT-5". As an example, with the below setting when I started Jetty (01/28/2014), it generated log files with file names - request.2014_56_28.log. Not sure where "56" came from for the file name "request.2014_56_28.log" Any suggestion would be of big help!!
<Ref id="Handlers">
<Call name="addHandler">
<Arg>
<New id="RequestLog" class="org.eclipse.jetty.server.handler.RequestLogHandler">
<Set name="requestLog">
<New id="RequestLogImpl" class="org.eclipse.jetty.server.NCSARequestLog">
<Set name="filename">
logs/request.yyyy_mm_dd.log
</Set>
<Set name="filenameDateFormat">yyyy_mm_dd</Set>
<Set name="retainDays">365</Set>
<Set name="append">true</Set>
<Set name="extended">false</Set>
<Set name="logCookies">false</Set>
<Set name="LogTimeZone">GMT-5</Set>
</New>
</Set>
</New>
</Arg>
</Call>
</Ref>
What you need is a TimeZone string that will identified by your system. Which means:
TimeZone identifier does not use GMT offset notation
TimeZone identifier is not 3-letters (see javadoc section about "Three-letter time zone IDs")
TimeZone identifier is long form.
References
Getting jetty to log with the correct timezone
java.util.TimeZone

Jetty error - java.lang.IllegalStateException: No object for id=Contexts

I want to run two webapps(different ports) on two parallel jetty instances.
I'm using jetty7.1.6 and following the instructions here.
My jetty-cp.xml(customized jetty.xml) contains this section:
<Set name="handler">
<New id="Handlers" class="org.eclipse.jetty.server.handler.HandlerCollection">
<Set name="handlers">
<Array type="org.eclipse.jetty.server.Handler">
<Item>
<New id="Contexts" class="org.eclipse.jetty.server.handler.ContextHandlerCollection"/>
</Item>
<Item>
<New id="DefaultHandler" class="org.eclipse.jetty.server.handler.DefaultHandler"/>
</Item>
</Array>
</Set>
</New>
</Set>
Now when I run one jetty instance from command line(java -Djetty.home=/opt/jetty -jar /opt/jetty/start.jar etc/jetty-cp.xml), I get the following error :
2014-01-02 18:03:47.649:WARN::Config error at <Call name="addBean"><Arg>| <New id="DeploymentManager" class="org.eclipse.jetty.deploy.DeploymentManager"><Set name="contexts">| <Ref id="Contexts"/>| </Set><Call name="setContextAttribute"><Arg>org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern</Arg><Arg>.*/jsp-api-[^/]*\.jar$|.*/jsp-[^/]*\.jar$</Arg></Call><Call name="addAppProvider"><Arg>| <New class="org.eclipse.jetty.deploy.providers.ContextProvider"><Set name="monitoredDir"><Property name="jetty.home" default="."/>/contexts</Set><Set name="scanInterval">5</Set></New>| </Arg></Call><Call name="addAppProvider"><Arg>| <New class="org.eclipse.jetty.deploy.providers.WebAppProvider"><Set name="monitoredDir"><Property name="jetty.home" default="."/>/webapps</Set><Set name="defaultsDescriptor"><Property name="jetty.home" default="."/>/etc/webdefault.xml</Set><Set name="scanInterval">5</Set><Set name="contextXmlDir"><Property name="jetty.home" default="."/>/contexts</Set></New>| </Arg></Call></New>| </Arg></Call> java.lang.IllegalStateException: No object for id=Contexts
2014-01-02 18:03:47.650:WARN::EXCEPTION
java.lang.IllegalStateException: No object for id=Contexts
at org.eclipse.jetty.xml.XmlConfiguration.refObj(XmlConfiguration.java:676)
at org.eclipse.jetty.xml.XmlConfiguration.itemValue(XmlConfiguration.java:941)
This section of config is present in /opt/jetty/etc/jetty-deploy.xml file.
What am I doing wrong? I already specified the object with id=Context in jetty-cp.xml file which I specified in command line. Why is the section in jetty-deploy.xml unable to find it?
start.ini had jetty-deploy.xml uncommented which caused *<Ref id="Contexts"/>* to be parsed before its definition in jetty-cp.xml.
Basically, files mentioned in start.ini get loaded even before the jetty-cp.xml I mentioned on command line. This needs to be kept in mind.

Mule/Jetty Setup

I have a working Mule application that I want to setup Jetty on to respond to http requests. The following config:
<jetty:endpoint address="http://localhost:8080"
name="jettyEndpoint"
host="localhost"
port="8080" path="/"
synchronous="true" />
<service name="jettyUMO">
<inbound>
<jetty:inbound-endpoint ref="jettyEndpoint" />
</inbound>
<test:component appendString="Received" />
</service>
...works when I start the application, and point browser of choice to http://localhost:8080 - all that gets displayed is "Received", per the test:component.
What I want to do is update this so that instead of seeing "Received", I want to go to where I defined an index.html file. My assumption is that I have to change the test:component out for an outbound endpoint - is this correct? Where would I specify the path (relative or absolute)?
I had to add a jetty:connector instance:
<jetty:connector name="httpConnector"
configFile="conf/jettyConfig.xml"
useContinuations="true" />
Here's the contents of the jettyConfig.xml because the simple example has errors:
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd">
<Configure id="Server" class="org.mortbay.jetty.Server">
<Call name="addConnector">
<Arg>
<New class="org.mortbay.jetty.nio.SelectChannelConnector">
<Set name="port">8080</Set>
</New>
</Arg>
</Call>
<Set name="handler">
<New id="Handlers" class="org.mortbay.jetty.handler.HandlerCollection">
<Set name="handlers">
<Array type="org.mortbay.jetty.Handler">
<Item>
<New id="Contexts" class="org.mortbay.jetty.handler.ContextHandlerCollection"/>
</Item>
<Item>
<New id="DefaultHandler" class="org.mortbay.jetty.handler.DefaultHandler"/>
</Item>
</Array>
</Set>
</New>
</Set>
<Call name="addLifeCycle">
<Arg>
<New class="org.mortbay.jetty.deployer.WebAppDeployer">
<Set name="contexts"><Ref id="Contexts"/></Set>
<Set name="webAppDir">path/webapps</Set>
</New>
</Arg>
</Call>
</Configure>
This did not work for me.
> [04-22 17:25:22] WARN log [main]:
> failed SelectChannelConnector#0.0.0.0:8080
> java.net.BindException: Address already in use
> at sun.nio.ch.Net.bind(Native Method)
I think, what happens is that one instance is being created on port defined in jettyConfig and then another through Mule. Changing the port in jettyConfig yields two identically behaving instances on two different ports.
The simplest solution is to remove the addConnector Call from jettyConfig.xml and let Mule assign the port.
It is also not needed to specify host and port on the endpoint. This suffices:
<jetty:endpoint address="http://localhost:8080" name="serverEndpoint" path="services/Foo" synchronous="false" />

Categories

Resources