Skip to content

Commit

Permalink
Merge remote-tracking branch 'fork/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed May 26, 2015
2 parents fa53b8b + cf233ea commit 44ff800
Show file tree
Hide file tree
Showing 21 changed files with 77 additions and 84 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ target/
.idea/
*.iml
.DS_Store
.classpath
.project
.settings
4 changes: 2 additions & 2 deletions src/main/java/org/takes/facets/auth/Pass.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
package org.takes.facets.auth;

import java.io.IOException;
import java.util.Iterator;
import org.takes.Request;
import org.takes.Response;
import org.takes.misc.Opt;

/**
* Pass to enter a user and let him exit.
Expand All @@ -45,7 +45,7 @@ public interface Pass {
* @return Identity of the user found
* @throws IOException If fails
*/
Iterator<Identity> enter(Request request) throws IOException;
Opt<Identity> enter(Request request) throws IOException;

/**
* Wrap the response with the user.
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/org/takes/facets/auth/PsByFlag.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@

import java.io.IOException;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.Map;
Expand All @@ -36,6 +34,7 @@
import lombok.EqualsAndHashCode;
import org.takes.Request;
import org.takes.Response;
import org.takes.misc.Opt;
import org.takes.rq.RqHref;

/**
Expand Down Expand Up @@ -98,19 +97,20 @@ public PsByFlag(final String flg, final Map<Pattern, Pass> map) {
}

@Override
public Iterator<Identity> enter(final Request req) throws IOException {
public Opt<Identity> enter(final Request req) throws IOException {
final Iterator<String> flg = new RqHref.Base(req).href()
.param(this.flag).iterator();
final Collection<Identity> users = new ArrayList<Identity>(1);
Opt<Identity> user = new Opt.Empty<Identity>();
if (flg.hasNext()) {
final String value = flg.next();
for (final Map.Entry<Pattern, Pass> ent : this.passes.entrySet()) {
if (ent.getKey().matcher(value).matches()) {
users.add(ent.getValue().enter(req).next());
user = ent.getValue().enter(req);
break;
}
}
}
return users.iterator();
return user;
}

@Override
Expand Down
15 changes: 6 additions & 9 deletions src/main/java/org/takes/facets/auth/PsChain.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,11 @@
package org.takes.facets.auth;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import lombok.EqualsAndHashCode;
import org.takes.Request;
import org.takes.Response;
import org.takes.misc.Opt;

/**
* Chain of passes.
Expand Down Expand Up @@ -66,16 +64,15 @@ public PsChain(final Iterable<Pass> list) {
}

@Override
public Iterator<Identity> enter(final Request req) throws IOException {
final Collection<Identity> users = new ArrayList<Identity>(1);
public Opt<Identity> enter(final Request req) throws IOException {
Opt<Identity> user = new Opt.Empty<Identity>();
for (final Pass pass : this.passes) {
final Iterator<Identity> identities = pass.enter(req);
if (identities.hasNext()) {
users.add(identities.next());
user = pass.enter(req);
if (user.has()) {
break;
}
}
return users.iterator();
return user;
}

@Override
Expand Down
13 changes: 7 additions & 6 deletions src/main/java/org/takes/facets/auth/PsCookie.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
package org.takes.facets.auth;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.Locale;
Expand All @@ -34,6 +32,7 @@
import org.takes.Request;
import org.takes.Response;
import org.takes.facets.auth.codecs.Codec;
import org.takes.misc.Opt;
import org.takes.rq.RqCookies;
import org.takes.rs.RsWithCookie;

Expand Down Expand Up @@ -96,14 +95,16 @@ public PsCookie(final Codec cdc, final String name, final long sec) {
}

@Override
public Iterator<Identity> enter(final Request req) throws IOException {
public Opt<Identity> enter(final Request req) throws IOException {
final Iterator<String> cookies = new RqCookies.Base(req)
.cookie(this.cookie).iterator();
final Collection<Identity> users = new ArrayList<Identity>(1);
Opt<Identity> user = new Opt.Empty<Identity>();
if (cookies.hasNext()) {
users.add(this.codec.decode(cookies.next().getBytes()));
user = new Opt.Single<Identity>(
this.codec.decode(cookies.next().getBytes())
);
}
return users.iterator();
return user;
}

@Override
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/org/takes/facets/auth/PsEmpty.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@
*/
package org.takes.facets.auth;

import java.util.Collections;
import java.util.Iterator;
import lombok.EqualsAndHashCode;
import org.takes.Request;
import org.takes.Response;
import org.takes.misc.Opt;

/**
* Pass that doesn't do anything.
Expand All @@ -42,8 +41,8 @@
public final class PsEmpty implements Pass {

@Override
public Iterator<Identity> enter(final Request request) {
return Collections.<Identity>emptyList().iterator();
public Opt<Identity> enter(final Request request) {
return new Opt.Empty<Identity>();
}

@Override
Expand Down
16 changes: 9 additions & 7 deletions src/main/java/org/takes/facets/auth/PsFake.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,10 @@
*/
package org.takes.facets.auth;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import lombok.EqualsAndHashCode;
import org.takes.Request;
import org.takes.Response;
import org.takes.misc.Opt;

/**
* Fake pass.
Expand Down Expand Up @@ -56,12 +54,16 @@ public PsFake(final boolean cond) {
}

@Override
public Iterator<Identity> enter(final Request request) {
final Collection<Identity> users = new ArrayList<Identity>(1);
public Opt<Identity> enter(final Request request) {
final Opt<Identity> user;
if (this.condition) {
users.add(new Identity.Simple("urn:test:1"));
user = new Opt.Single<Identity>(
new Identity.Simple("urn:test:1")
);
} else {
user = new Opt.Empty<Identity>();
}
return users.iterator();
return user;
}

@Override
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/org/takes/facets/auth/PsFixed.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@
*/
package org.takes.facets.auth;

import java.util.Collections;
import java.util.Iterator;
import lombok.EqualsAndHashCode;
import org.takes.Request;
import org.takes.Response;
import org.takes.misc.Opt;

/**
* Fixed pass.
Expand Down Expand Up @@ -55,8 +54,8 @@ public PsFixed(final Identity identity) {
}

@Override
public Iterator<Identity> enter(final Request request) {
return Collections.singleton(this.user).iterator();
public Opt<Identity> enter(final Request request) {
return new Opt.Single<Identity>(this.user);
}

@Override
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/org/takes/facets/auth/PsLogout.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@
*/
package org.takes.facets.auth;

import java.util.Collections;
import java.util.Iterator;
import lombok.EqualsAndHashCode;
import org.takes.Request;
import org.takes.Response;
import org.takes.misc.Opt;

/**
* Logout.
Expand All @@ -42,8 +41,8 @@
public final class PsLogout implements Pass {

@Override
public Iterator<Identity> enter(final Request request) {
return Collections.singleton(Identity.ANONYMOUS).iterator();
public Opt<Identity> enter(final Request request) {
return new Opt.Single<Identity>(Identity.ANONYMOUS);
}

@Override
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org/takes/facets/auth/TkAuth.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@
package org.takes.facets.auth;

import java.io.IOException;
import java.util.Iterator;
import lombok.EqualsAndHashCode;
import org.takes.Request;
import org.takes.Response;
import org.takes.Take;
import org.takes.facets.auth.codecs.CcPlain;
import org.takes.misc.Opt;
import org.takes.rq.RqWithHeader;
import org.takes.rq.RqWithoutHeader;

Expand Down Expand Up @@ -83,10 +83,10 @@ public TkAuth(final Take take, final Pass pss, final String hdr) {

@Override
public Response act(final Request request) throws IOException {
final Iterator<Identity> users = this.pass.enter(request);
final Opt<Identity> user = this.pass.enter(request);
final Response response;
if (users.hasNext()) {
response = this.act(request, users.next());
if (user.has()) {
response = this.act(request, user.get());
} else {
response = this.origin.act(request);
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org/takes/facets/auth/social/PsFacebook.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URI;
import java.util.Collections;
import java.util.Iterator;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
Expand All @@ -45,6 +44,7 @@
import org.takes.facets.auth.Identity;
import org.takes.facets.auth.Pass;
import org.takes.misc.Href;
import org.takes.misc.Opt;
import org.takes.rq.RqHref;

/**
Expand Down Expand Up @@ -117,7 +117,7 @@ public PsFacebook(final String fapp, final String fkey) {
}

@Override
public Iterator<Identity> enter(final Request trequest)
public Opt<Identity> enter(final Request trequest)
throws IOException {
final Href href = new RqHref.Base(trequest).href();
final Iterator<String> code = href.param("code").iterator();
Expand All @@ -140,12 +140,12 @@ public Iterator<Identity> enter(final Request trequest)
.path("picture")
.toString()
);
return Collections.<Identity>singleton(
return new Opt.Single<Identity>(
new Identity.Simple(
String.format("urn:facebook:%s", user.getId()),
props
)
).iterator();
);
}

@Override
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org/takes/facets/auth/social/PsGithub.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import com.jcabi.http.response.XmlResponse;
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;
Expand All @@ -41,6 +40,7 @@
import org.takes.facets.auth.Identity;
import org.takes.facets.auth.Pass;
import org.takes.misc.Href;
import org.takes.misc.Opt;
import org.takes.rq.RqHref;

/**
Expand Down Expand Up @@ -102,7 +102,7 @@ public PsGithub(final String gapp, final String gkey) {
}

@Override
public Iterator<Identity> enter(final Request request)
public Opt<Identity> enter(final Request request)
throws IOException {
final Href href = new RqHref.Base(request).href();
final Iterator<String> code = href.param("code").iterator();
Expand All @@ -112,9 +112,9 @@ public Iterator<Identity> enter(final Request request)
"code is not provided by Github"
);
}
return Collections.singleton(
return new Opt.Single<Identity>(
this.fetch(this.token(href.toString(), code.next()))
).iterator();
);
}

@Override
Expand Down
8 changes: 3 additions & 5 deletions src/main/java/org/takes/facets/auth/social/PsGoogle.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import com.jcabi.http.response.RestResponse;
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;
Expand All @@ -40,6 +39,7 @@
import org.takes.facets.auth.Identity;
import org.takes.facets.auth.Pass;
import org.takes.misc.Href;
import org.takes.misc.Opt;
import org.takes.rq.RqHref;

/**
Expand Down Expand Up @@ -116,7 +116,7 @@ public PsGoogle(final String gapp, final String gkey,
}

@Override
public Iterator<Identity> enter(final Request request)
public Opt<Identity> enter(final Request request)
throws IOException {
final Href href = new RqHref.Base(request).href();
final Iterator<String> code = href.param("code").iterator();
Expand All @@ -126,9 +126,7 @@ public Iterator<Identity> enter(final Request request)
"code is not provided by Google, probably some mistake"
);
}
return Collections.singleton(
this.fetch(this.token(code.next()))
).iterator();
return new Opt.Single<Identity>(this.fetch(this.token(code.next())));
}

@Override
Expand Down
Loading

0 comments on commit 44ff800

Please sign in to comment.