Why can't I read static file in Jetty 8.1.9 - configuration

In /opt/jetty/webapps, I have test.xml under directory w. I have this test.xml in contexts directory:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.eclipse.org/configure.dtd">
<Configure class="org.eclipse.jetty.server.handler.ContextHandler">
<Set name="contextPath">/w</Set>
<Set name="resourceBase">/opt/java/webapps/w/</Set>
<Set name="handler">
<New class="org.eclipse.jetty.server.handler.ResourceHandler">
<Set name="welcomeFiles">
<Array type="String">
<Item>test.xml</Item>
</Array>
</Set>
<Set name="cacheControl">max-age=3600,public</Set>
</New>
</Set>
</Configure>
Why Can't I read http://host/w/test.xml?

Your question is a bit confusing, as you mention test.xml twice, and in two different directories.
Anyway, this is a basic example of setting up what you want, using the standard jetty-distribution-8.1.9.v20130131.tar.gz available at download.eclipse.org/jetty/.
The deployable context
Create a file called contexts/w.xml with the following content
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.eclipse.org/configure.dtd">
<Configure class="org.eclipse.jetty.server.handler.ContextHandler">
<Set name="contextPath">/w</Set>
<Set name="resourceBase"><SystemProperty name="jetty.home" default="."/>/w/</Set>
<Set name="handler">
<New class="org.eclipse.jetty.server.handler.ResourceHandler">
<Set name="welcomeFiles">
<Array type="String">
<Item>test.xml</Item>
<Item>index.html</Item>
</Array>
</Set>
<Set name="cacheControl">max-age=3600,public</Set>
</New>
</Set>
</Configure>
Notes:
${jetty.home} points to whatever your /path/to/jetty-distribution-8.1.9.v20130131/ is
This context points to a directory named ${jetty.home}/w/, which is not in the webapps directory, this is intentional, as the webapps directory is for standalone Java Servlet or Java EE webapps, either in archive form, or in an exploded deployable form. Since you are using ContextHandler and ResourceHandler your deployable does not meet these requirements.
The content
In the ${jetty.home}/w/ directory create a few files.
$ mkdir /path/to/jetty-distribution-8.1.9.v20130131/w
$ echo "<h1>Hello World</h1>" > /path/to/jetty-distribution-8.1.9.v20130131/w/index.html
Testing it
Start Jetty
$ cd /path/to/jetty-distribution-8.1.9.v20130131
$ java -jar start.jar
Open up a browser and test it
http://localhost:8080/w/

Related

Junit empty reports while running soapui testsuites using ant

I am trying to run my testsuite using ant, for reporting I am using ant.
I did a lot of tries but unable to get the reports. My test suite is running properly, but my Junit report all values are 0.
My Build xml file
<?xml version="1.0"?>
<project basedir="." default="testreport" name="Automation">
<property name="results.dir" value="D:/airline.com/NDC_Automation/API/Results"/>
<property name="reports.dir" value="${results.dir}/Reports"/>
<property name="html.dir" value="${reports.dir}/html"/>
<property name="proj.dir" value="D:/airline.com/NDC_Automation/Project/ADC-NDC-SB8-soapui-project.xml"/>
<target name="testsuite1">
<exec dir="." executable="D:/SoapUI-5.2.1/bin/testrunner.bat">
<arg line="-r -j -a -f ${results.dir} -sDAC-3728 ${proj.dir}"/>
</exec>
</target>
<target name="testreport" depends="testsuite1">
<mkdir dir="${reports.dir}"/>
<junitreport todir="${reports.dir}">
<fileset dir="${results.dir}">
<include name="TEST-*.XML"/>
</fileset>
<report format="frames" todir="${html.dir}">
</report>
</junitreport>
</target>
</project>
I followed another post from stackoveflowTHIS POST and followed the steps mentioned in it as well. But still my junit report is empty.
My TESTS-TestSuites.XML
<?xml version="1.0" encoding="UTF-8" ?>
<testsuites />
Is my build file correct or have i missed any important tag ?

Empty Ant <junitreport> report for SoapUI testrunner XML results

I am running a SoapUI project using Ant to get a JUnit report.
Here is my build.xml:
<project basedir="." default="testreport" name="APIAutomation">
<target name="SoapUI">
<exec dir="." executable="C:\Program Files (x86)\SmartBear\SoapUI-5.0.0\bin\testrunner.bat">
<arg line="-r -j -a -f 'C:\Users\F3020722\Desktop\Notification\New folder' -sFirstLoginTest 'C:\Users\F3020722\Desktop\Notification\New folder\APIRegression.xml'"></arg>
</exec>
</target>
<target name="testreport" depends="SoapUI">
<junitreport todir="C:\Users\F3020722\Desktop\Notification\New folder\API">
<fileset dir="C:\Users\F3020722\Desktop\Notification\New folder\API">
<include name="TEST-*.xml"/>
</fileset>
<report format="frames"
todir="C:\Users\F3020722\Desktop\Notification\New folder\reports\html">
</report>
</junitreport>
</target>
</project>
I am getting an XML report properly. However, the JUnit report is empty. all contains 0 and successrate is Nan.
Can anyone check the build.xml is correct?
Looks build script seems ok
Avoid spaces in the directory names
Use forward slashes like unix style even on windows
Use property file or properties in build script so that other members do not have it edit the build scripts as paths might change machine to machine.
For now, added properties in the below script, you may externalize to a property file too.
build.xml
<project basedir="." default="testreport" name="APIAutomation">
<property name="test.suite" value="FirstLoginTest"/>
<property name="soapui.project" value="C:/Users/F3020722/Desktop/Notification/New folder/APIRegression.xml"/>
<property name="soapui.home" value="C:/Program Files (x86)/SmartBear/SoapUI-5.0.0"/>
<property name="results.dir" value="C:/Users/F3020722/Desktop/Notification/API/Results"/>
<property name="reports.dir" value="${results.dir}/Reports"/>
<property name="html.dir" value="${reports.dir}/html"/>
<target name="execute.project">
<exec dir="${soapui.home}/bin" executable="testrunner.bat">
<arg line="-raj -f ${results.dir} -s ${test.suite} ${soapui.project}" />
</exec>
</target>
<target name="testreport" depends="execute.project">
<mkdir dir="${reports.dir}"/>
<junitreport todir="${reports.dir}">
<fileset dir="${results.dir}">
<include name="TEST-*.xml"/>
</fileset>
<report format="frames" todir="${html.dir}" />
</junitreport>
</target>
</project>
You can also find a docker image for soapui and run tests & generate junit style html report as well. Refer soapui repository # hub.docker.com
Note: that build script used docker images is exactly the same as above except the machine path.

How to find path to executable in Ant

In my Ant build file I want to test whether the mysql command is found on the environment path. This should be system independent.
What I did until now was the following:
<trycatch property="mysql.error">
<try>
<echo message="Testing mysql..." />
<exec executable="mysql" outputproperty="null" append="true" />
<echo message="MySQL executable found in path." />
<property name="mysql.command" value="mysql"/>
</try>
<catch>
<echo message="MySQL executable not found in path, trying to locate default folder." />
<if>
<istrue value="${isWindows}"/>
<then>
<antcallback target="search-file-windows" return="search.result">
<param name="search.target" value="mysql.exe"/>
</antcallback>
<property name="mysql.command" value="${search.result}"/>
</then>
<else>
<property name="mysql.command" value="/usr/local/mysql/bin/mysql"/>
</else>
</if>
<echo message="MySQL executable found at location: ${mysql.command}." />
<trycatch property="mysql.error">
<try>
<echo message="Possible path found, testing again..." />
<exec executable="${mysql.command}" outputproperty="null" append="true" />
<echo message="MySQL executable found at location: ${mysql.command}." />
</try>
<catch>
<fail message="Unable to locate MySQL executable. Please add your local MySQL installation to the PATH environment variable."/>
</catch>
</trycatch>
</catch>
</trycatch>
So I just execute the mysql command and if that fails, I will run a batch file which does some magic to efficiently search for mysql. However the check fails if there is any error in calling mysql, even if it is found in the path. On my Windows machine this happens, because just starting mysql gives the following error: ERROR 1045 (28000): Access denied for user 'ODBC'#'localhost'
This error is fixable of course, but I am really looking for a generic solution. Now the PATH variable contains "C:\Program Files\MySQL\MySQL Server 5.6\bin" so the solutions in this question will not work: Check if executable command exists using ant
Any ideas?
available can do the trick for you, you "only" need to deal with the differences between Windows and Unix-likes.
Something like this
<!-- load environment variables into properties -->
<property environment="env"/>
<!-- On Windows the Environment-Variable is not all uppercase -->
<path id="combined-PATH">
<pathelement path="${env.PATH}"/>
<pathelement path="${env.Path}"/>
</path>
<!-- toString() -->
<property name="PATH" refid="combined-PATH"/>
<condition property="mysql.found">
<or>
<available file="mysql.exe" filepath="${PATH}"/>
<available file="mysql" filepath="${PATH}"/>
</or>
</condition>
will set the property mysql.found if and only if mysql is on the PATH.
The following Ant script uses the third-party Ant-Contrib library's <for> task:
<project name="ant-first-match-on-path" default="run">
<taskdef resource="net/sf/antcontrib/antlib.xml"/>
<target name="run">
<property name="executable-name" value="mysql"/>
<condition property="executable-filename"
value="${executable-name}.exe"
else="${executable-name}"
>
<os family="windows"/>
</condition>
<property environment="env" />
<for param="dir">
<path>
<pathelement path="${env.PATH}"/>
<pathelement path="${env.Path}"/>
</path>
<sequential>
<if>
<not>
<isset property="first-match"/>
</not>
<then>
<local name="executable-absolute-path"/>
<property
name="executable-absolute-path"
location="#{dir}/${executable-filename}"
/>
<available
file="${executable-absolute-path}"
property="first-match"
value="${executable-absolute-path}"
/>
</then>
</if>
</sequential>
</for>
<condition property="echo-message"
value="First [${executable-filename}] found at [${first-match}]."
else="[${executable-filename}] not found on PATH."
>
<isset property="first-match"/>
</condition>
<echo message="${echo-message}"/>
</target>
</project>

Keep JDBC references outside web.xml to deploy in Jetty, like in tomcat

I try to move some services from my Tomcat Server to Jetty, just to make some comparisons. Obviously I don't want to change my services, but I experiment some issues to deploy them with JDBC.
My services all use the same database to access datas, so I wrote my own library to make my requests. The services don't have any informations about the database, they just know they have to use the library. In this library I make connections with the database using this kind of code:
InitialContext ictx = new InitialContext();
Context envCtx = (Context) ictx.lookup("java:comp/env");
DataSource ds = (DataSource) envCtx.lookup("jdbc/mysql");
In Tomcat my services work well just adding a line in context.xml:
<Resource auth="Container" driverClassName="com.mysql.jdbc.Driver" maxActive="100" maxIdle="30" maxWait="10000" name="jdbc/mysql" username="login" password="password" type="javax.sql.DataSource" url="jdbc:mysql://localhost:3306/mysql" />
So I just want to do the same in Jetty. I added the following block in my jetty.xml:
<New id="mysql" class="org.eclipse.jetty.plus.jndi.Resource">
<Arg></Arg>
<Arg>jdbc/mysql</Arg>
<Arg>
<New class="org.apache.commons.dbcp.BasicDataSource">
<Set name="driverClassName">com.mysql.jdbc.Driver</Set>
<Set name="url">jdbc:mysql://localhost:3306/mysql</Set>
<Set name="username">login</Set>
<Set name="password">password</Set>
</New>
</Arg>
</New>
The server starts well and seem to work, but I get an error when I try to access to my services. In jetty's manual I found it's explictly written that I have to add some informations in web.xml like:
<resource-ref>
<description>My DataSource Reference</description>
<res-ref-name>jdbc/DSTest</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
So I wonder if there is any other solution, than write the same lines in all my web.xml services' files? Like adding a common xml file for all my server with the same informations ?
You can add it to the webdefault.xml that is used as the foundation for processing web.xml.
This file is typically located in etc/webdefault.xml of the distribution.
cheers

Content of a web-directory

I've got a site with some administrative pages in its root directory. Question: Is there any possible way for a visitor to see all the pages and/or subdirectories in the root directory of this (or any other) site?
If yes, what has to be done to conceal the directory's content?
Thank you!
You can set this permission in the web server. For IIS there is a enable directory browsing property that can be set on website, virtual directories and applications.
For Apache:
In httpd.conf search for Options, if Indexes is present Directory browsing is enabled else it is disabled
Options All Indexes FollowSymLinks MultiViews (Directory browsing Enabled)
Options All FollowSymLinks MultiViews (Directory browsing Disabled)
It depends on what server you are using. For example, for jetty, you can use a context file (in xml) to conceal/hide a directory's content...
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">
<Configure class="org.eclipse.jetty.server.handler.ContextHandler">
<Call class="org.eclipse.jetty.util.log.Log" name="debug">
<Arg>Configure sarbot_files.xml</Arg>
</Call>
<Set name="contextPath">/some_context</Set>
<Set name="resourceBase">/path/to/files</Set>
<Set name="handler">
<New class="org.eclipse.jetty.server.handler.ResourceHandler">
<!-- Set to true or false here -->
<Set name="directoriesListed">true</Set>
<Set name="welcomeFiles">
<Array type="String">
<Item>index.html</Item>
</Array>
</Set>
<Set name="cacheControl">max-age=3600,public</Set>
</New>
</Set>
</Configure>