Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Conflicts:
	src/main/java/org/takes/rs/RsWithHeaders.java
  • Loading branch information
super132 committed Apr 18, 2015
2 parents b666115 + 7737387 commit adf8469
Show file tree
Hide file tree
Showing 38 changed files with 1,112 additions and 369 deletions.
3 changes: 1 addition & 2 deletions .rultor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ merge:
mvn clean
pdd --source=$(pwd) --verbose --file=/dev/null
commanders:
- alevohin
- carlosmiranda
- darkled
- ggajos
Expand Down Expand Up @@ -66,4 +65,4 @@ release:
mvn clean deploy -Ptakes -Psonatype --errors --settings ../settings.xml
mvn clean site-deploy -Ptakes -Psite --errors --settings ../settings.xml
commanders:
- yegor256
- yegor256
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ cache:
- $HOME/.m2
script:
- mvn clean install -Pqulice --errors --batch-mode
env:
global:
- MAVEN_OPTS="-Xmx256m -Dfile.encoding=UTF-8"
- JAVA_OPTS="-Xmx256m -Dfile.encoding=UTF-8"
jdk:
- oraclejdk8
- oraclejdk7
Expand Down
73 changes: 73 additions & 0 deletions src/main/java/org/takes/facets/auth/codecs/CcBase64.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* 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.facets.auth.codecs;

import java.io.IOException;
import lombok.EqualsAndHashCode;
import org.takes.facets.auth.Identity;

/**
* Base64 codec.
*
* <p>The class is immutable and thread-safe.
*
* @author Igor Khvostenkov ([email protected])
* @version $Id$
* @since 0.13
*/
@EqualsAndHashCode
public final class CcBase64 implements Codec {
/**
* Original codec.
*/
private final transient Codec origin;
/**
* Ctor.
* @param codec Original codec
*/
public CcBase64(final Codec codec) {
this.origin = codec;
}

//@todo #19:30min to implement own simple Base64 encode algorithm
// without using 3d-party Base64 encode libraries. Tests for this
// method have been already created, do not forget to remove Ignore
// annotation on it.
@Override
public byte[] encode(final Identity identity) throws IOException {
assert this.origin != null;
throw new UnsupportedOperationException("#encode()");
}

//@todo #19:30min to implement own simple Base64 decode algorithm
// without using 3d-party Base64 decode libraries. Tests for this
// method have been already created, do not forget to remove Ignore
// annotation on it.
@Override
public Identity decode(final byte[] bytes) throws IOException {
assert this.origin != null;
throw new UnsupportedOperationException("#decode()");
}

}
2 changes: 1 addition & 1 deletion src/main/java/org/takes/facets/auth/social/PsGithub.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ private static Identity parse(final JsonObject json) {
final ConcurrentMap<String, String> props =
new ConcurrentHashMap<String, String>(json.size());
// @checkstyle MultipleStringLiteralsCheck (1 line)
props.put("login", json.getString("login", "?"));
props.put("login", json.getString("login", "unknown"));
props.put("avatar", json.getString("avatar_url", "#"));
return new Identity.Simple(
String.format("urn:github:%d", json.getInt("id")), props
Expand Down
32 changes: 21 additions & 11 deletions src/main/java/org/takes/facets/auth/social/PsGoogle.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@
import com.jcabi.http.request.JdkRequest;
import com.jcabi.http.response.JsonResponse;
import com.jcabi.http.response.RestResponse;
import com.jcabi.http.wire.VerboseWire;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.Collections;
import java.util.Iterator;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import javax.json.JsonObject;
import lombok.EqualsAndHashCode;
import org.takes.Request;
Expand All @@ -50,7 +51,7 @@
* @since 0.9
* @checkstyle MultipleStringLiteralsCheck (500 lines)
*/
@EqualsAndHashCode(of = { "app", "key" })
@EqualsAndHashCode(of = { "app", "key", "redir" })
public final class PsGoogle implements Pass {

/**
Expand All @@ -63,14 +64,22 @@ public final class PsGoogle implements Pass {
*/
private final transient String key;

/**
* Redirect URI.
*/
private final transient String redir;

/**
* Ctor.
* @param gapp Google app
* @param gkey Google key
* @param uri Redirect URI (exactly as registered in Google console)
*/
public PsGoogle(final String gapp, final String gkey) {
public PsGoogle(final String gapp, final String gkey,
final String uri) {
this.app = gapp;
this.key = gkey;
this.redir = uri;
}

@Override
Expand All @@ -84,7 +93,7 @@ public Iterator<Identity> enter(final Request request)
);
}
return Collections.singleton(
PsGoogle.fetch(this.token(href, code.next()))
PsGoogle.fetch(this.token(code.next()))
).iterator();
}

Expand Down Expand Up @@ -115,22 +124,19 @@ private static Identity fetch(final String token) throws IOException {

/**
* Retrieve Google access token.
* @param home Home of this page
* @param code Google "authorization code"
* @return The token
* @throws IOException If failed
*/
private String token(final Href home, final String code)
throws IOException {
private String token(final String code) throws IOException {
return new JdkRequest("https://accounts.google.com/o/oauth2/token")
.body()
.formParam("client_id", this.app)
.formParam("redirect_uri", home)
.formParam("redirect_uri", this.redir)
.formParam("client_secret", this.key)
.formParam("grant_type", "authorization_code")
.formParam("code", code)
.back()
.through(VerboseWire.class)
.header("Content-Type", "application/x-www-form-urlencoded")
.method(com.jcabi.http.Request.POST)
.fetch().as(RestResponse.class)
Expand All @@ -146,9 +152,13 @@ private String token(final Href home, final String code)
* @return Identity found
*/
private static Identity parse(final JsonObject json) {
final ConcurrentMap<String, String> props =
new ConcurrentHashMap<String, String>(json.size());
// @checkstyle MultipleStringLiteralsCheck (1 line)
props.put("picture", json.getString("picture", "#"));
props.put("name", json.getString("name", "unknown"));
return new Identity.Simple(
String.format("urn:google:%s", json.getString("id")),
Collections.<String, String>emptyMap()
String.format("urn:google:%s", json.getString("id")), props
);
}

Expand Down
19 changes: 15 additions & 4 deletions src/main/java/org/takes/facets/auth/social/XeGoogleLink.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,20 @@ public final class XeGoogleLink extends XeWrap {
*/
public XeGoogleLink(final Request req, final CharSequence app)
throws IOException {
this(
req, app, "takes:google",
new RqHref.Smart(new RqHref.Base(req)).home()
);
this(req, app, new RqHref.Smart(new RqHref.Base(req)).home());
}

/**
* Ctor.
* @param req Request
* @param app Google application ID
* @param redir Redirect URI
* @throws IOException If fails
* @since 0.14
*/
public XeGoogleLink(final Request req, final CharSequence app,
final CharSequence redir) throws IOException {
this(req, app, "takes:google", redir);
}

/**
Expand All @@ -64,6 +74,7 @@ public XeGoogleLink(final Request req, final CharSequence app)
* @param rel Related
* @param redir Redirect URI
* @throws IOException If fails
* @since 0.14
* @checkstyle ParameterNumberCheck (4 lines)
*/
public XeGoogleLink(final Request req, final CharSequence app,
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/takes/facets/fork/FkMethods.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public FkMethods(final Collection<String> mtds, final Take tke) {

@Override
public Iterator<Response> route(final Request req) throws IOException {
final String mtd = new RqMethod(req).method();
final String mtd = new RqMethod.Base(req).method();
final Collection<Response> list = new ArrayList<Response>(1);
if (this.methods.contains(mtd)) {
list.add(this.take.act(req));
Expand Down
64 changes: 64 additions & 0 deletions src/main/java/org/takes/facets/fork/TkProduces.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* 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.facets.fork;

import java.io.IOException;
import lombok.EqualsAndHashCode;
import org.takes.Request;
import org.takes.Response;
import org.takes.Take;
import org.takes.tk.TkWrap;

/**
* Take that acts on request with specified "Accept" HTTP headers only.
*
* <p>The class is immutable and thread-safe.
*
* @author Eugene Kondrashev ([email protected])
* @version $Id$
* @since 0.14
*/
@EqualsAndHashCode(callSuper = true)
public final class TkProduces extends TkWrap {

/**
* Ctor.
* @param take Original take
* @param types Accept types
*/
public TkProduces(final Take take, final String types) {
super(
new Take() {
@Override
public Response act(final Request req) throws IOException {
return new RsFork(
req,
new FkTypes(types, take.act(req))
);
}
}
);
}

}
37 changes: 36 additions & 1 deletion src/main/java/org/takes/facets/forward/RsForward.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.takes.rs.RsEmpty;
import org.takes.rs.RsWithHeader;
import org.takes.rs.RsWithStatus;
import org.takes.rs.RsWithoutHeader;

/**
* Forwarding response.
Expand Down Expand Up @@ -71,6 +72,15 @@ public RsForward(final Response res) {
this(res, "/");
}

/**
* Ctor.
* @param res Original response
* @since 0.14
*/
public RsForward(final RsForward res) {
this(res.origin);
}

/**
* Ctor.
* @param res Original response
Expand All @@ -80,6 +90,16 @@ public RsForward(final Response res, final CharSequence loc) {
this(res, HttpURLConnection.HTTP_SEE_OTHER, loc);
}

/**
* Ctor.
* @param res Original response
* @param loc Location
* @since 0.14
*/
public RsForward(final RsForward res, final CharSequence loc) {
this(res.origin, loc);
}

/**
* Ctor.
* @param loc Location
Expand All @@ -97,6 +117,18 @@ public RsForward(final int code, final CharSequence loc) {
this(new RsEmpty(), code, loc);
}

/**
* Ctor.
* @param res Original
* @param code HTTP status code
* @param loc Location
* @since 0.14
*/
public RsForward(final RsForward res, final int code,
final CharSequence loc) {
this(res.origin, code, loc);
}

/**
* Ctor.
* @param res Original
Expand All @@ -107,7 +139,10 @@ public RsForward(final Response res, final int code,
final CharSequence loc) {
super(code, res.toString());
this.origin = new RsWithHeader(
new RsWithStatus(res, code),
new RsWithoutHeader(
new RsWithStatus(res, code),
"Location"
),
new Sprintf("Location: %s", loc)
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/takes/facets/forward/package-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
* &#64;Override
* public Response act(final Request req) {
* final InputStream content =
* new RqMultipart(req).part("file").body();
* new RqMultipart.Base(req).part("file").body();
* // save content to whenever you want
* return new RsForward(new RqHref(req).href());
* }
Expand Down
Loading

0 comments on commit adf8469

Please sign in to comment.