How to set Jetty Request Log to default Timezone using XML - java

I am not able to make Jetty Request-log log requests with the local time-zone time using the following code,
<Set name="LogTimeZone" type="java.lang.String">
<Get class="java.util.TimeZone" name="default">
<Get name="ID"/>
</Get>
</Set>
it defaults to GMT. The setLogTimeZone() (http://download.eclipse.org/jetty/9.3.9.v20160517/apidocs/org/eclipse/jetty/server/AbstractNCSARequestLog.html#setLogTimeZone-java.lang.String-) accepts a string argument and therefore should ideally work. But, it doesn't! Although, when providing the required String directly it works just fine,
<Set name="LogTimeZone">Europe/London</Set>
the jetty log(std error) seems to be working fine. Is this a known bug?
Jetty Version used : 9.1.1

Take the result of getID() out of the TimeZone.getDefault() into an xml id attribute, then reference it later in the <Set> call using a <Ref> element.
Example of how this works in Jetty XML:
foo.xml
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN"
"http://www.eclipse.org/jetty/configure_9_3.dtd">
<Configure id="foo" class="java.lang.Object">
<Get class="java.util.TimeZone" name="default">
<Get id="defaultTimeZone" name="ID"/>
</Get>
<Get id="sysout" class="java.lang.System" name="out">
<Call name="println">
<Arg>
<Ref refid="defaultTimeZone"/>
</Arg>
</Call>
</Get>
</Configure>
Testing it on the command line ...
$ cd /path/to/jetty-dist-9.3.9.v20160517/
$ java -cp lib/jetty-util-9.3.9.v20160517.jar:lib/jetty-xml-9.3.9.v20160517.jar \
org.eclipse.jetty.xml.XmlConfiguration foo.xml
2016-07-13 17:16:25.447:INFO::main: Logging initialized #121ms
America/Phoenix

Related

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.

Jetty 7 - Disable directory list at root folder /

I have a lot of production servers running RMI application in each one, and more 4 Java webapps, eg:
Server A:
RMI app by JNLP file;
webapp_1 (connected by RMI with local RMI app);
webapp_2 (connected by RMI with local RMI app);
webapp_3 (connected by RMI with local RMI app);
webapp_4 (connected by RMI with local RMI app);
Server B:
...the same..OK
All users access this servers directly on 8080 port (direct to Jetty) in a default context, eg 'main-area', where it can access by some html links all apps (RMI app, webapp_1, webapp_2, etc.).
When some user access the '/' page, eg:
www.foo.com:8080/
main-area/
webapp_1/
webapp_2/
webapp_3/
...
Jetty returns a list with all applications (just like directory list of Apache).
Is there some way to block it, or redirect to 'main-area' context ?
The list of webapp contexts that do not match "/" is presented to you as part of the responsibility of org.eclipse.jetty.server.handler.DefaultHandler
The DefaultHandler is enabled by default, to remain in conformance to the Servlet Spec.
Disabling DefaultHandler:
If you just want a simple 404, with no information presented by the DefaultHandler, then just comment it out in the ${jetty.home}/etc/jetty.xml
<!-- =========================================================== -->
<!-- Set handler Collection Structure -->
<!-- =========================================================== -->
<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>
<!-- Disable the DefaultHandler to avoid listing of non-matching contexts
<Item>
<New id="DefaultHandler" class="org.eclipse.jetty.server.handler.DefaultHandler"/>
</Item>
-->
</Array>
</Set>
</New>
</Set>
Presenting Static Content for "/" (ROOT) Context:
If you want the root context "/" (ROOT) to present something else, then create a ${jetty.home}/webapps/ROOT directory and put an index.html file in it.
[jetty-distribution-7.6.13.v20130916]$ cat webapps/ROOT/index.html
<h1>This is ROOT</h1>
This will deploy a static content webapp where you can put any content you want to in there, images, css, etc.
Automatically Redirecting "/" (ROOT) to another path:
Note: this will not work at the same time as the above ${jetty.home}/webapps/ROOT option, his this option, or that option, but not both.
If you want Jetty to redirect "/" automatically to another URL then use the rewrite handler.
Make sure you have the rewrite OPTION enabled, and include a set of rewrite rules xml
[jetty-distribution-7.6.13.v20130916]$ grep rewrite start.ini
OPTIONS=Server,jsp,jmx,resources,websocket,ext,rewrite
etc/jetty-rewrite.xml
Next, you'll want to define your rewrite rules ...
Contents of ${jetty.home}/etc/jetty-rewrite.xml to redirect accesses from "/" to "/test/"
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN"
"http://www.eclipse.org/jetty/configure.dtd">
<Configure id="Server" class="org.eclipse.jetty.server.Server">
<Get id="oldhandler" name="handler"/>
<Set name="handler">
<New id="Rewrite" class="org.eclipse.jetty.rewrite.handler.RewriteHandler">
<Set name="handler"><Ref id="oldhandler"/></Set>
<Set name="rewriteRequestURI">true</Set>
<Set name="rewritePathInfo">false</Set>
<Set name="originalPathAttribute">requestedPath</Set>
<!-- redirect from the welcome page to a specific page -->
<Call name="addRule">
<Arg>
<New class="org.eclipse.jetty.rewrite.handler.RedirectRegexRule">
<Set name="regex">^/$</Set>
<Set name="replacement">/test/</Set>
</New>
</Arg>
</Call>
</New>
</Set>
</Configure>
Create an index.html file for that location.
This will be served so the list does not need to be generated.
You can then put a simple redirect in there, along with a normal link if the browser does not respect redirects.
Thanks to Thorbjørn Ravn Andersen by solution:
I created a basic Dynamic Web Application with a single index.jsp with JSP HTML/JS redirect inside:
<%# page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<meta http-equiv="refresh" content="1;url=bar/index.jsp">
<script type="text/javascript">
window.location.href = "bar/index.jsp"
</script>
<title>Page Redirection</title>
</head>
<body>
If you are not redirected automatically, follow the <a href='bar/index.jsp'>main area</a>
</body>
</html>
I deployed as 'ROOT.war' and I configured a 'root.xml' in '...jetty/contexts/' with:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
<Set name="contextPath">/</Set>
<Set name="war"><SystemProperty name="jetty.home" default="."/>/webapps/ROOT.war</Set>
</Configure>

Form too Large Exception

When I send a large file using a post request the system shows an exception:
java.lang.IllegalStateException: Form too large1105723>200000
at org.mortbay.jetty.Request.extractParameters(Request.java:1404)
at org.mortbay.jetty.Request.getParameter(Request.java:749)......
When I search help for this in Google they give some help e.g.,
webappcontext.setMaxFormContentSize(5000000);
I am using this code but the problem is not solved
Also I am using the code
jettyServer.setAttribute("org.mortbay.jetty.Request.maxFormContentSize", 5000000);
But no result
Note:-I am using Jetty-6.1.0
Try setting System properties via jetty.xml
<Call class="java.lang.System" name="setProperty">
<Arg>org.mortbay.jetty.Request.maxFormContentSize</Arg>
<Arg>500000</Arg>
</Call>
ok you can configure it from your web app
Add WEB-INF/jetty-web.xml file in your web application
and configure the parameter in that file:
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN"
"http://jetty.mortbay.org/configure.dtd">
<Configure id="WebAppContext" class="org.mortbay.jetty.webapp.WebAppContext">
<Set name="maxFormContentSize" type="int">600000</Set>
</Configure>
Document
Version 7 or Higher
Since version 7, Jetty's classes have moved to a different package. You must replace org.mortbay... with org.eclipse... (Thanks to David for his comment).
import org.mortbay.jetty.Server;
//... other code here...//
int port = 8080;
Server server = new Server(port);
server.setAttribute("org.mortbay.jetty.Request.maxFormContentSize", -1);
This code works on jetty 6.0.2 which I'm using.
The size of "-1" means the form has no limit I tryed to post a form large 20,000,000 bytes and I had no problem.
For eclipse releases of Jetty(jetty 7) you have to use the following code:
import org.eclipse.jetty.server.Server;
//... other code here...//
int port = 8080;
Server server = new Server(port);
server.setAttribute("org.eclipse.jetty.server.Request.maxFormContentSize", -1);
Unfortunately, I'm not able to make any changes to jetty.xml, so instead I simply set some options to adjust the maxFormContentSize like so:
JVM_OPTS="$JVM_OPTS -Dorg.eclipse.jetty.server.Request.maxFormContentSize=5000000"
This exists in the shell script that we use to launch our instance of Solr.
More documentation on form size: http://wiki.eclipse.org/Jetty/Howto/Configure_Form_Size
I came across this problem too (running Jetty embedded in another application, so I'm not using jetty.xml).
I used the setMaxFormContentSize method on the ContextHandler class, which fixed the "form too large" exception. (See http://wiki.eclipse.org/Jetty/Tutorial/Embedding_Jetty#Setting_a_ServletContext for an example of creating/using a context handler).
<!-- Development Jetty -->
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>8.1.8.v20121106</version>
<configuration>
<scanIntervalSeconds>1</scanIntervalSeconds>
<webApp>
<contextPath>/${project.build.finalName}</contextPath>
</webApp>
<systemProperties>
<systemProperty>
<name>org.eclipse.jetty.server.Request.maxFormContentSize</name>
<value>10485760</value>
</systemProperty>
</systemProperties>
</configuration>
</plugin>
Work for jetty 8 in maven plugin
webappcontext.getServletContext().getContextHandler() .setMaxFormContentSize(10000000);
I use jetty 9.2.3.v20140905, and i fixed the problem use the follow:
confiure pom.xml
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.2.3.v20140905</version>
<configuration>
<jettyXml>
src/main/resources/jetty/jetty.xml
</jettyXml>
</configuration>
</plugin>
configure jetty.xml
<Configure id="Server" class="org.eclipse.jetty.server.Server">
<Call name="setAttribute">
<Arg>org.eclipse.jetty.server.Request.maxFormContentSize</Arg>
<Arg>-1</Arg>
</Call>
</Configure>
Depending on how old your Jetty Version is you are using (in my case jetty-5.1.14 embedded in Eclipse Equinox), it also could be that the property needs to be org.mortbay.http.HttpRequest.maxFormContentSize
From: org.mortbay.http.HttpRequest
/**
* Max size of the form content. Limits the size of the data a client can push at the server.
* Set via the org.mortbay.http.HttpRequest.maxContentSize system property.
*/
public static int __maxFormContentSize = Integer.getInteger(
"org.mortbay.http.HttpRequest.maxFormContentSize", 200000).intValue();
So you need to do something like this in your application on startup to set the value:
System.setProperty("org.mortbay.http.HttpRequest.maxFormContentSize", "10000000");
None of the above Solution worked for me ,
So in order to make this work I set the system property before creating the server, rather then setting it as server attribute
System.setProperty("org.eclipse.jetty.server.Request.maxFormContentSize", "500000000");
Server server = ServerFactory.createServer(host, port, contextPath, war);
I ran into a similar issue on ActiveMQ so i had to edit the jetty.xml and add
<property name="maxFormContentSize" value="-1" />
to the handler property.
from:-
<property name="handler">
<bean id="sec" class="org.eclipse.jetty.server.handler.HandlerCollection">
<property name="handlers">
<list>
<bean class="org.eclipse.jetty.webapp.WebAppContext">
<property name="contextPath" value="/admin" />
<property name="resourceBase" value="${activemq.home}/webapps/admin" />
<property name="logUrlOnStart" value="true" />
</bean>
to
<property name="handler">
<bean id="sec" class="org.eclipse.jetty.server.handler.HandlerCollection">
<property name="handlers">
<list>
<bean class="org.eclipse.jetty.webapp.WebAppContext">
<property name="contextPath" value="/admin" />
<property name="resourceBase" value="${activemq.home}/webapps/admin" />
<property name="logUrlOnStart" value="true" />
<property name="maxFormContentSize" value="-1" />
</bean>
If you use jetty in embedded mode,try this.
ServletContextHandler servletHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
servletHandler.setMaxFormContentSize(1024*1024*1024);//size you want to allow.
I use Spring boot and set server.jetty.max-http-post-size: maxSize in application.properties to fix it.
server.jetty.max-http-post-size: 500000
set in jetty/webapps -> configure .xml (e.g jetty-web.xml) file
"-1" for unlimited content
<Set name="maxFormContentSize" type="int">600000</Set>
OR
<Set name="maxFormContentSize" type="int">-1</Set>
Possibly because of changes in Jetty since version 7, but I only had success like so:
in jetty-web.xml, add the below to the Server object (1000000 is an example size, obv)
<Call name="setAttribute">
<Arg>org.eclipse.jetty.server.Request.maxFormContentSize</Arg>
<Arg>1000000</Arg>
</Call>
full file might look something like mine
<Configure id="Server" class="org.eclipse.jetty.server.Server">
<Call name="setAttribute">
<Arg>org.eclipse.jetty.server.Request.maxFormContentSize</Arg>
<Arg>1000000</Arg>
</Call>
<Ref id="DeploymentManager">
<Call id="webappprovider" name="addAppProvider">
<Arg>
(...)
ref http://wiki.eclipse.org/Jetty/Howto/Configure_Form_Size
If you're running from eclipse/spring add the below to vm arguments
-Dorg.mortbay.jetty.Request.maxFormContentSize=-1
Start jenkins by adding command line argument
-Dorg.eclipse.jetty.server.Request.maxFormContentSize=500000
i.e java -Dorg.eclipse.jetty.server.Request.maxFormContentSize=500000 -jar jenkins.war
ActiveMQ:
The problem here is with Jetty, on which ActiveMQ is based. You can find more details here, documentation
Solution is in apache-activemq-5.9.0/bin/win64/wrapper.conf file, add the following line a after b (refer below).
a: wrapper.java.additional.16=-Dorg.eclipse.jetty.server.Request.maxFormContentSize=1000000
b: wrapper.java.additional.15=-Djava.security.auth.login.config=%ACTIVEMQ_CONF%/login.config
If you are running on a 32 bit computer, then please add the same line in apache-activemq-5.9.0/bin/win32/wrapper.conf.
Happy Coding..

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