Skip to content

Commit

Permalink
TsVersioned
Browse files Browse the repository at this point in the history
  • Loading branch information
Yegor Bugayenko committed Mar 4, 2015
1 parent 34cf8fb commit 90796fd
Show file tree
Hide file tree
Showing 6 changed files with 246 additions and 0 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
</site>
</distributionManagement>
<properties>
<timestamp>${maven.build.timestamp}</timestamp>
<saxon.version>9.1.0.8</saxon.version>
</properties>
<dependencies>
Expand Down
47 changes: 47 additions & 0 deletions src/main/java/org/takes/ts/TsEmpty.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2015 Yegor Bugayenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.takes.ts;

import lombok.EqualsAndHashCode;
import org.takes.Request;
import org.takes.Take;
import org.takes.Takes;
import org.takes.tk.TkEmpty;

/**
* Empty.
*
* @author Yegor Bugayenko (yegor@teamed.io)
* @version $Id$
* @since 0.4
*/
@EqualsAndHashCode
public class TsEmpty implements Takes {

@Override
public final Take route(final Request request) {
return new TkEmpty();
}

}
78 changes: 78 additions & 0 deletions src/main/java/org/takes/ts/TsVersioned.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2015 Yegor Bugayenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.takes.ts;

import java.util.ResourceBundle;
import lombok.EqualsAndHashCode;
import org.takes.Takes;

/**
* Takes that adds an HTTP header to each response with a version
* of Takes framework.
*
* @author Yegor Bugayenko (yegor@teamed.io)
* @version $Id$
* @since 0.4
*/
@EqualsAndHashCode(callSuper = true)
public final class TsVersioned extends TsWrap {

/**
* Version of the library.
*/
private static final String VERSION = TsVersioned.make();

/**
* Ctor.
* @param takes Original takes
*/
public TsVersioned(final Takes takes) {
this(takes, "X-Takes-Version");
}

/**
* Ctor.
* @param takes Original takes
* @param header Header to add
*/
public TsVersioned(final Takes takes, final String header) {
super(new TsWithHeaders(takes).with(header, TsVersioned.VERSION));
}

/**
* Make a version.
* @return Version
*/
private static String make() {
final ResourceBundle res =
ResourceBundle.getBundle("org.takes.version");
return String.format(
"%s %s built on %s",
res.getString("version"),
res.getString("revision"),
res.getString("date")
);
}

}
60 changes: 60 additions & 0 deletions src/main/java/org/takes/ts/TsWrap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2015 Yegor Bugayenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.takes.ts;

import java.io.IOException;
import lombok.EqualsAndHashCode;
import org.takes.Request;
import org.takes.Take;
import org.takes.Takes;

/**
* Wrap of takes.
*
* @author Yegor Bugayenko (yegor@teamed.io)
* @version $Id$
* @since 0.4
*/
@EqualsAndHashCode(of = "origin")
public class TsWrap implements Takes {

/**
* Original takes.
*/
private final transient Takes origin;

/**
* Ctor.
* @param takes Original takes
*/
public TsWrap(final Takes takes) {
this.origin = takes;
}

@Override
public final Take route(final Request request) throws IOException {
return this.origin.route(request);
}

}
3 changes: 3 additions & 0 deletions src/main/resources/org/takes/version.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
version=${project.version}
revision=${buildNumber}
date=${timestamp}
57 changes: 57 additions & 0 deletions src/test/java/org/takes/ts/TsVersionedTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2015 Yegor Bugayenko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.takes.ts;

import java.io.IOException;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.takes.rq.RqFake;
import org.takes.rs.RsPrint;

/**
* Test case for {@link TsVersioned}.
* @author Yegor Bugayenko (yegor@teamed.io)
* @version $Id$
* @since 0.4
*/
public final class TsVersionedTest {

/**
* TsVersioned can add a header with version name.
* @throws IOException If some problem inside
*/
@Test
public void attachesHeader() throws IOException {
MatcherAssert.assertThat(
new RsPrint(
new TsVersioned(new TsEmpty()).route(
new RqFake()
).act()
).print(),
Matchers.containsString("X-Takes-Version")
);
}

}

0 comments on commit 90796fd

Please sign in to comment.