-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f3fcdc1
commit f54946c
Showing
8 changed files
with
206 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
backend/src/main/java/eu/clarin/switchboard/resources/UrlMatchResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package eu.clarin.switchboard.resources; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import eu.clarin.switchboard.app.FileAsset; | ||
import eu.clarin.switchboard.app.config.ToolConfig; | ||
import eu.clarin.switchboard.core.MediaLibrary; | ||
import eu.clarin.switchboard.core.ToolRegistry; | ||
import eu.clarin.switchboard.core.xc.CommonException; | ||
import eu.clarin.switchboard.profiler.api.Profile; | ||
import eu.clarin.switchboard.profiler.api.ProfilingException; | ||
import eu.clarin.switchboard.tool.Tool; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import javax.ws.rs.*; | ||
import javax.ws.rs.core.Context; | ||
import javax.ws.rs.core.MediaType; | ||
import javax.ws.rs.core.Request; | ||
import javax.ws.rs.core.Response; | ||
import java.nio.file.Paths; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.function.Predicate; | ||
import java.util.stream.Collectors; | ||
|
||
@Path("/api/urlmatch") | ||
public class UrlMatchResource { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(UrlMatchResource.class); | ||
|
||
MediaLibrary mediaLibrary; | ||
ToolRegistry toolRegistry; | ||
ToolConfig toolConfig; | ||
|
||
public UrlMatchResource(MediaLibrary mediaLibrary, ToolRegistry toolRegistry, ToolConfig toolConfig) { | ||
this.mediaLibrary = mediaLibrary; | ||
this.toolRegistry = toolRegistry; | ||
this.toolConfig = toolConfig; | ||
} | ||
|
||
/** | ||
* PUBLIC API | ||
*/ | ||
@POST | ||
@Consumes(MediaType.APPLICATION_JSON) | ||
@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8") | ||
public Response doUrlsMatch(@QueryParam("onlyProductionTools") String onlyProductionTools, List<String> urlList) throws CommonException, ProfilingException { | ||
long startTime = System.nanoTime(); | ||
List<Profile> profiles = urlList.stream().map(url -> { | ||
try { | ||
return mediaLibrary.addByUrlPreflight(url, null).getProfile().toProfile(); | ||
} catch (Exception e) { | ||
LOGGER.info("Exception in urlmatch addByUrlPreflight: "+e.getMessage()); | ||
return null; | ||
} | ||
}).filter(Objects::nonNull).collect(Collectors.toList()); | ||
|
||
Predicate<Tool> filter = toolConfig.getShowOnlyProductionTools() ? ToolRegistry.ONLY_PRODUCTION_TOOLS : ToolRegistry.ALL_TOOLS; | ||
if (onlyProductionTools != null && !onlyProductionTools.isEmpty()) { | ||
filter = Boolean.parseBoolean(onlyProductionTools) ? ToolRegistry.ONLY_PRODUCTION_TOOLS : ToolRegistry.ALL_TOOLS; | ||
} | ||
|
||
boolean timeout = profiles.isEmpty(); | ||
int matches = timeout ? 0 : toolRegistry.filterTools(profiles, filter).size(); | ||
LOGGER.debug("urlmatch finished in " + ((System.nanoTime() - startTime) / 1000000) + "ms"); | ||
|
||
return Response.ok(new JsonResponse(timeout, matches, profiles)).build(); | ||
} | ||
|
||
public static class JsonResponse { | ||
public final boolean timeout; | ||
public final int matches; | ||
public final List<Profile.Flat> detectedProfiles; | ||
|
||
public JsonResponse(boolean timeout, int matches, List<Profile> profiles) { | ||
this.timeout = timeout; | ||
this.matches = matches; | ||
this.detectedProfiles = profiles.stream().map(Profile::flat).collect(Collectors.toList()); | ||
} | ||
} | ||
} |
Oops, something went wrong.