Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jetty.xml Rewrite Handler doesnt seem to take effect. #101

Closed
davehancock opened this issue Oct 19, 2014 · 15 comments
Closed

Jetty.xml Rewrite Handler doesnt seem to take effect. #101

davehancock opened this issue Oct 19, 2014 · 15 comments
Assignees
Labels

Comments

@davehancock
Copy link

@davehancock davehancock commented Oct 19, 2014

Im trying to configure a rewrite handler for Jetty (to facilitate redirecting when requesting AngularJs route provider locations without hash tags).

I have the equivalent configuration for jetty-maven-plugin and gretty plugin, and it seems to work perfectly with the maven plugin (inspired by http://stackoverflow.com/questions/4681907/how-do-i-configuration-to-use-rewrite-by-jetty-maven-plugin) but it doesn't seem to take effect when using gretty. No exceptions or warnings are thrown, tried with a few versions Jetty7 - 9 with the same results seen.

The jetty.xml used is as follows:

<?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">

    <Set name="ThreadPool">
        <!-- Default queued blocking threadpool -->
        <New class="org.eclipse.jetty.util.thread.QueuedThreadPool">
            <Set name="minThreads">10</Set>
            <Set name="maxThreads">200</Set>
            <Set name="detailedDump">false</Set>
        </New>
    </Set>

    <Call name="addConnector">
        <Arg>
            <New class="org.eclipse.jetty.server.bio.SocketConnector">
                <Set name="port">8080</Set>
            </New>
        </Arg>
    </Call>

    <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>

    <Set name="stopAtShutdown">true</Set>
    <Set name="sendServerVersion">true</Set>
    <Set name="sendDateHeader">true</Set>
    <Set name="gracefulShutdown">1000</Set>
    <Set name="dumpAfterStart">false</Set>
    <Set name="dumpBeforeStop">false</Set>

    <!-- =========================================================== -->
    <!-- configure rewrite handler                                   -->
    <!-- =========================================================== -->
    <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.RewritePatternRule">
                        <Set name="pattern">/rewrite/</Set>
                        <Set name="replacement">/rewrite/info.html</Set>
                    </New>
                </Arg>
            </Call>
            <!-- replace the entire request URI -->
            <Call name="addRule">
                <Arg>
                    <New class="org.eclipse.jetty.rewrite.handler.RewritePatternRule">
                        <Set name="pattern">/some/old/context</Set>
                        <Set name="replacement">/rewritten/newcontext</Set>
                    </New>
                </Arg>
            </Call>
            <!-- replace the beginning of the request URI -->
            <Call name="addRule">
                <Arg>
                    <New class="org.eclipse.jetty.rewrite.handler.RewritePatternRule">
                        <Set name="pattern">/rewrite/for/*</Set>
                        <Set name="replacement">/rewritten/</Set>
                    </New>
                </Arg>
            </Call>
            <!--  actual redirect, instead of internal rewrite -->
            <Call name="addRule">
                <Arg>
                    <New class="org.eclipse.jetty.rewrite.handler.RedirectPatternRule">
                        <Set name="pattern">/summary</Set>
                        <Set name="location">/</Set>
                    </New>
                </Arg>
            </Call>
        </New>
    </Set>
</Configure>

The gradle configuration used is:

apply plugin: 'war'
apply plugin: 'org.akhikhl.gretty'

project.group = 'com.foo.bar'
project.version = '0.0.1-SNAPSHOT'

project.webAppDirName = "app"

gretty {
    contextPath = "/"
    servletContainer = 'jetty7';
    serverConfigFile = 'jetty.xml'
}

dependencies {
    gretty 'org.eclipse.jetty:jetty-rewrite:7.6.15.v20140411'
}

repositories {
    mavenCentral()
}

buildscript {
    dependencies {
        classpath 'org.akhikhl.gretty:gretty:+'
    }

    repositories {
        mavenCentral()
        jcenter()
    }
}

task wrapper(type: Wrapper) {
    gradleVersion = '2.1'
}

And the (Much more ugly) Maven Configuration:

<?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.foo.bar</groupId>
    <artifactId>fooBar</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>7.6.15.v20140411</version>
                <configuration>
                    <jettyConfig>${project.basedir}/jetty.xml</jettyConfig>
                    <webAppConfig>
                        <contextPath>/</contextPath>
                    </webAppConfig>
                    <scanIntervalSeconds>10</scanIntervalSeconds>
                    <webAppSourceDirectory>${project.basedir}/app</webAppSourceDirectory>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.eclipse.jetty</groupId>
                        <artifactId>jetty-http</artifactId>
                        <version>7.6.15.v20140411</version>
                        <type>jar</type>
                        <scope>runtime</scope>
                    </dependency>
                    <dependency>
                        <groupId>org.eclipse.jetty</groupId>
                        <artifactId>jetty-rewrite</artifactId>
                        <version>7.6.15.v20140411</version>
                        <type>jar</type>
                        <scope>runtime</scope>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>

</project>

Thanks,
Dave.

@akhikhl
Copy link
Owner

@akhikhl akhikhl commented Oct 19, 2014

Hi Dave,
I'll have a look at the problem. Could you, please, provide a simplest project allowing to reproduce a problem?

@davehancock
Copy link
Author

@davehancock davehancock commented Oct 19, 2014

Hi, Thanks for the speedy reply!

Created an example project with a pom, build.gradle, index html and one rule in jetty.xml that should redirect from "localhost:8080/summary" > "localhost:8080/". Running "JettyRun" Maven plugin goal exhibits correct behaviour, running "JettyStart" gradle task loads container and root page etc but doesn't reroute correctly.

https://github.com/daves125125/LocationList/tree/master/RewriteHandlerExample

@akhikhl
Copy link
Owner

@akhikhl akhikhl commented Oct 19, 2014

I cloned the project and tried maven and gradle runs - the problem is well-reproducible, thank you.
I even have ideas from where the problem comes:

  1. "jetty.xml" should reside in one of the directories mentioned in http://akhikhl.github.io/gretty-doc/jetty.xml-support.html or it should be pointed at via gretty.serverConfigFile. If Gretty recognizes "jetty.xml", it should write "Configuring server with /home/user/RewriteHandlerExample/jetty.xml" to the terminal.
  2. It seems that Gretty overrides connectors you define in "jetty.xml".
    First point is yours, please fix it.
    Second point I'll try to fix by swapping lines with applyJettyXml and configureConnectors. It's that simple. If all integration tests are successful, I'll push new snapshot version to jfrog snapshot repository. Release comes at Tuesday, 21.10.2014.
@davehancock
Copy link
Author

@davehancock davehancock commented Oct 19, 2014

Ok great, ill make the change and then check out the snapshot on Tuesday.

Thanks.

@akhikhl
Copy link
Owner

@akhikhl akhikhl commented Oct 19, 2014

Snapshot comes in 20 minutes 😄 It's release that comes on Tuesday.

@akhikhl akhikhl added the bug label Oct 19, 2014
@akhikhl akhikhl self-assigned this Oct 19, 2014
akhikhl added a commit that referenced this issue Oct 19, 2014
@akhikhl
Copy link
Owner

@akhikhl akhikhl commented Oct 19, 2014

Snapshot with the fix is ready and available at jfrog snapshot repository. You can use it via code:

apply from: 'https://raw.githubusercontent.com/akhikhl/gretty/master/pluginScripts/gretty-SNAPSHOT.plugin'

or feel free copying code from that file (gretty-SNAPSHOT.plugin).

@davehancock
Copy link
Author

@davehancock davehancock commented Oct 19, 2014

Hey,

Ive applied the latest snapshot and amended the gradle.build i.e:

apply plugin: 'war'
//apply plugin: 'org.akhikhl.gretty'
apply from: 'https://raw.githubusercontent.com/akhikhl/gretty/master/pluginScripts/gretty-SNAPSHOT.plugin'

project.group = 'com.foo.bar'
project.version = '0.0.1-SNAPSHOT'
project.webAppDirName = "app"

gretty {
    contextPath = "/"
    servletContainer = 'jetty7'
    serverConfigFile = 'jetty.xml'
}

dependencies {
    gretty 'org.eclipse.jetty:jetty-rewrite:7.6.15.v20140411'
}

repositories {
    mavenCentral()
}

//buildscript {
//    dependencies {
//        classpath 'org.akhikhl.gretty:gretty:+'
//    }
//
//    repositories {
//        mavenCentral()
//        jcenter()
//    }
//}

task wrapper(type: Wrapper) {
    gradleVersion = '2.1'
}

And unfortunately it still doesn't seem to be taking effect for me, not sure if im missing some other configuration. Additionally a definition of a connector seems to become a duplication, for example setting the following connector in jetty.xml will result in "Address in use" as gretty also creates a default one (maybe this is intended behaviour):

<Call name="addConnector">
        <Arg>
            <New class="org.eclipse.jetty.server.bio.SocketConnector">
                <Set name="port">8080</Set>
            </New>
        </Arg>
</Call>

I've committed and pushed the above to the previous example project if that helps in testing.

Cheers

@akhikhl
Copy link
Owner

@akhikhl akhikhl commented Oct 19, 2014

I'll have a look.

akhikhl added a commit that referenced this issue Oct 20, 2014
…ndler doesnt seem to take effect.'
@akhikhl
Copy link
Owner

@akhikhl akhikhl commented Oct 20, 2014

New fix is in snapshot repository. Could you, please, test RewriteHandlerExample again?

@davehancock
Copy link
Author

@davehancock davehancock commented Oct 20, 2014

Works great, thank you very much!

@akhikhl
Copy link
Owner

@akhikhl akhikhl commented Oct 20, 2014

I'm happy that it works for you, Dave 😄
Please come back any time!

@akhikhl
Copy link
Owner

@akhikhl akhikhl commented Oct 21, 2014

I have to delay Gretty Release 1.1.5 to the end of this week because of other urgent things that people wait to be released.
I hope temporary use of snapshot is okay with you?

@akhikhl akhikhl added the resolved label Oct 21, 2014
@akhikhl
Copy link
Owner

@akhikhl akhikhl commented Oct 31, 2014

Gretty release 1.1.6 is out, with the abovementioned fix.

@davehancock
Copy link
Author

@davehancock davehancock commented Nov 3, 2014

Thanks! Using now and it works great.

@asifaragi
Copy link

@asifaragi asifaragi commented Feb 24, 2015

My project using Dropwizard framework which inbuilt uses jetty server 8.1.10 maven dependency for running application.
Can you please guide me how to configure for AngularJs route without hash tags.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.