@Path("/events/{eventName}/icons/levels/{level}.png") @GET @Produces("image/png") public Response getLevelIcon( @PathParam("eventName") final String eventName, @PathParam("level") final String level) { OperationResult<Event> eventResult = incogito.getEventByName(eventName); if (!eventResult.isOk()) { return toJsr311(eventResult); } Option<LevelId> levelOption = some(level).bind(Level.LevelId.valueOf); if (!levelOption.isSome()) { return toJsr311(OperationResult.<Object>notFound("Level '" + level + "' not known.")); } Option<File> fileOption = eventResult.value().levels.get(levelOption.some()).map(Level.iconFile_); if (!fileOption.isSome()) { return toJsr311(OperationResult.<Object>notFound("No icon for level '" + level + "'.")); } // TODO: How about some caching here? Option<byte[]> bytes = join( fileOption .map(IO.<byte[]>runFileInputStream_().f(IO.ByteArrays.streamToByteArray)) .map(compose(P1.<Option<byte[]>>__1(), Callables.<byte[]>option()))); return toJsr311(fromOption(bytes, "Unable to read level file."), cacheForOneHourCacheControl); }
public static <T> boolean contains(List<T> l, F<T, Boolean> f) { Option<T> o = l.find(f); return o.isSome(); }