Skip to content

Commit

Permalink
Brian/Helen: Adding filtered landscape (allows filtering builds by us…
Browse files Browse the repository at this point in the history
…er) (#40)

* Brian/Helen: Adding filtered landscape (allows filtering builds by user)

* Brian/Helen: Consolidate filtered and non filtered LandscapeObservationResponder. Lowercased url.

* Brian/Helen: Return NotFound if user can't be found (with message)
  • Loading branch information
helenwilliamson authored and scarytom committed Mar 14, 2017
1 parent c4434f7 commit 2888555
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package org.netmelody.cieye.core.domain;

import static com.google.common.collect.Lists.newArrayList;
import com.google.common.base.Predicate;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static com.google.common.collect.Collections2.filter;
import static com.google.common.collect.Lists.newArrayList;

public final class LandscapeObservation {

private final List<TargetDetail> targets = new ArrayList<TargetDetail>();
Expand Down Expand Up @@ -46,4 +49,18 @@ public Set<Sponsor> dohGroup() {
public LandscapeObservation withDoh(Set<Sponsor> dohGroup) {
return new LandscapeObservation(this.targets, dohGroup);
}

public LandscapeObservation forSponsor(final Sponsor sponsor) {
return new LandscapeObservation(filter(targets, onSponsor(sponsor)), dohGroup);
}

private Predicate<TargetDetail> onSponsor(final Sponsor sponsor) {
return new Predicate<TargetDetail>() {
@Override
public boolean apply(TargetDetail input) {
return input.sponsors().contains(sponsor);
}
};
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package org.netmelody.cieye.server.response;


import com.google.common.base.Function;
import org.netmelody.cieye.core.domain.LandscapeObservation;
import org.netmelody.cieye.core.domain.Sponsor;
import org.netmelody.cieye.server.CiEyeNewVersionChecker;
import org.netmelody.cieye.server.CiEyeServerInformationFetcher;
import org.netmelody.cieye.server.CiSpyIntermediary;
Expand Down Expand Up @@ -94,6 +97,17 @@ private CiEyeResponder route(Address target) {
}

if (path.length == 3) {
if ("filteredlandscapes".equals(path[0])) {
Sponsor sponsor = tracker.sponsorWith(path[2]);
if (sponsor == null) {
return new NotFoundResponder("Cannot find user " + path[2]);
}
if (!target.getPath().getPath().endsWith("/")) {
return new RedirectResponder(target.getPath().getPath() + "/");
}
return new FileResponder("/resources/cieye.html");
}

if ("landscapes".equals(path[0]) && "landscapeobservation.json".equals(path[2])) {
return new LandscapeObservationResponder(landscapeFetcher.landscapeNamed(path[1]), spyIntermediary, prison);
}
Expand All @@ -107,6 +121,25 @@ private CiEyeResponder route(Address target) {
}
}

if (path.length == 4) {
if ("filteredlandscapes".equals(path[0]) && "landscapeobservation.json".equals(path[3])) {
Sponsor sponsor = tracker.sponsorWith(path[2]);
if (sponsor == null) {
return new NotFoundResponder("Cannot find user " + path[2]);
}
return new LandscapeObservationResponder(landscapeFetcher.landscapeNamed(path[1]), spyIntermediary, prison, filteringOnSponsor(sponsor));
}
}

return new NotFoundResponder();
}

private Function<LandscapeObservation, LandscapeObservation> filteringOnSponsor(final Sponsor sponsor) {
return new Function<LandscapeObservation, LandscapeObservation>() {
@Override
public LandscapeObservation apply(LandscapeObservation input) {
return input.forSponsor(sponsor);
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import java.io.IOException;

import com.google.common.base.Function;
import com.google.common.base.Functions;
import org.netmelody.cieye.core.domain.Feature;
import org.netmelody.cieye.core.domain.Landscape;
import org.netmelody.cieye.core.domain.LandscapeObservation;
Expand All @@ -20,14 +22,19 @@ public final class LandscapeObservationResponder implements CiEyeResponder {
private final CiSpyIntermediary spyIntermediary;
private final Landscape landscape;
private final Prison prison;
private final Function<LandscapeObservation, LandscapeObservation> converter;

public LandscapeObservationResponder(Landscape landscape, CiSpyIntermediary spyIntermediary, Prison prison) {
this(landscape, spyIntermediary, prison, Functions.<LandscapeObservation>identity());
}

public LandscapeObservationResponder(Landscape landscape, CiSpyIntermediary spyIntermediary, Prison prison, Function<LandscapeObservation, LandscapeObservation> converter) {
this.landscape = landscape;
this.spyIntermediary = spyIntermediary;
this.prison = prison;
this.converter = converter;
}

@Override
public CiEyeResponse respond(Request request) throws IOException {
LandscapeObservation result = new LandscapeObservation();
long timeToLiveMillis = Long.MAX_VALUE;
Expand All @@ -36,11 +43,11 @@ public CiEyeResponse respond(Request request) throws IOException {
result = result.add(briefing.status);
timeToLiveMillis = min(timeToLiveMillis, briefing.millisecondsUntilNextUpdate);
}

if (prison.crimeReported(landscape)) {
result = result.withDoh(prison.prisonersFor(landscape));
}
return CiEyeResponse.withJson(new JsonTranslator().toJson(result)).expiringInMillis(timeToLiveMillis);

return CiEyeResponse.withJson(new JsonTranslator().toJson(converter.apply(result))).expiringInMillis(timeToLiveMillis);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,22 @@ public final class NotFoundResponder implements CiEyeResponder {
private static final String CONTENT =
"<!DOCTYPE html>" +
"<html>" +
"<body>Page Not Found. Try <a href=\"/\">starting from the top<a></body>" +
"<body>%s</body>" +
"</html>";

private static final String BODY = "Page Not Found. Try <a href=\"/\">starting from the top<a>";
private final String content;

public NotFoundResponder() {
this(BODY);
}

public NotFoundResponder(String body) {
this.content = String.format(CONTENT, body);
}

@Override
public CiEyeResponse respond(Request request) throws IOException {
return CiEyeResponse.withHtml(CONTENT).withStatus(Status.NOT_FOUND);
return CiEyeResponse.withHtml(content).withStatus(Status.NOT_FOUND);
}
}

0 comments on commit 2888555

Please sign in to comment.