Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<!-- just rename the version and copy over -->
<!-- follow semver, with suggested guidance for versioning fork of OSS -->
<!-- see https://gofore.com/en/best-practices-for-forking-a-git-repo/ -->
<version>0.3+worklytics.9</version>
<version>0.3+worklytics.10</version>
<packaging>jar</packaging>
<licenses>
<license>
Expand Down
2 changes: 1 addition & 1 deletion java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<url>https://github.com/Worklytics/appengine-pipelines/</url>
<!-- follow semver, with suggested guidance for versioning fork of OSS -->
<!-- see https://gofore.com/en/best-practices-for-forking-a-git-repo/ -->
<version>0.3+worklytics.9</version>
<version>0.3+worklytics.10</version>
<packaging>jar</packaging>
<licenses>
<license>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,38 +1,26 @@
// Copyright 2011 Google Inc. All Rights Reserved.
package com.google.appengine.tools.mapreduce.impl.handlers;

import static com.google.appengine.tools.mapreduce.MapSettings.CONTROLLER_PATH;
import static com.google.appengine.tools.mapreduce.MapSettings.WORKER_PATH;
import static com.google.appengine.tools.mapreduce.impl.shardedjob.ShardedJobHandler.JOB_ID_PARAM;
import static com.google.appengine.tools.mapreduce.impl.shardedjob.ShardedJobHandler.SEQUENCE_NUMBER_PARAM;
import static com.google.appengine.tools.mapreduce.impl.shardedjob.ShardedJobHandler.TASK_ID_PARAM;
import static com.google.common.base.Preconditions.checkNotNull;

import com.google.appengine.tools.mapreduce.MapReduceJob;
import com.google.appengine.tools.mapreduce.MapReduceServlet;
import com.google.appengine.tools.mapreduce.impl.shardedjob.IncrementalTaskId;
import com.google.appengine.tools.mapreduce.impl.shardedjob.ShardedJobRunId;
import com.google.appengine.tools.mapreduce.impl.shardedjob.ShardedJobRunner;
import com.google.appengine.tools.mapreduce.impl.util.RequestUtils;
import com.google.appengine.tools.pipeline.di.JobRunServiceComponent;
import com.google.appengine.tools.pipeline.di.StepExecutionComponent;
import com.google.appengine.tools.pipeline.di.StepExecutionModule;
import com.google.appengine.tools.pipeline.impl.servlets.StaticContentHandler;
import com.google.common.collect.ImmutableMap;
import lombok.AllArgsConstructor;
import lombok.extern.java.Log;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.logging.Level;

import javax.inject.Inject;
import static com.google.appengine.tools.mapreduce.MapSettings.CONTROLLER_PATH;
import static com.google.appengine.tools.mapreduce.MapSettings.WORKER_PATH;
import static com.google.appengine.tools.mapreduce.impl.shardedjob.ShardedJobHandler.*;
import static com.google.common.base.Preconditions.checkNotNull;

//TODO: not actually a servlet
@Log
Expand All @@ -43,48 +31,8 @@ public class MapReduceServletImpl {
StatusHandler statusHandler;
RequestUtils requestUtils;

private static final Map<String, Resource> RESOURCES = ImmutableMap.<String, Resource>builder()
.put("status", new Resource("/_ah/pipeline/list?class_path=" + MapReduceJob.class.getName()))
.put("detail", new Resource("detail.html", "text/html"))
.put("base.css", new Resource("base.css", "text/css"))
.put("jquery.js", new Resource("jquery-1.6.1.min.js", "text/javascript"))
.put("jquery-json.js", new Resource("jquery.json-2.2.min.js", "text/javascript"))
.put("jquery-url.js", new Resource("jquery.url.js", "text/javascript"))
.put("mapreduce-status.js", new Resource("mapreduce-status.js", "text/javascript"))
.build();

static final String COMMAND_PATH = "command";

private static class Resource {
private final String filename;
private final String contentType;
private final String redirect;

Resource(String filename, String contentType) {
this.filename = filename;
this.contentType = contentType;
this.redirect = null;
}

Resource(String redirect) {
this.redirect = redirect;
filename = null;
contentType = null;
}

String getRedirect() {
return redirect;
}

String getFilename() {
return filename;
}

String getContentType() {
return contentType;
}
}

/**
* Handle GET http requests.
*/
Expand All @@ -97,7 +45,7 @@ public void doGet(HttpServletRequest request, HttpServletResponse response)
}
statusHandler.handleCommand(handler.substring(COMMAND_PATH.length() + 1), request, response);
} else {
handleStaticResources(handler, response);
log.log(Level.SEVERE, String.format("Unknown MapReduce request handler: %s", handler));
}
}

Expand Down Expand Up @@ -144,8 +92,6 @@ private ShardedJobRunId getJobId(HttpServletRequest request) {
.orElseThrow(() -> new IllegalArgumentException("Missing " + JOB_ID_PARAM + " parameter"));
}



/**
* Checks to ensure that the current request was sent via an AJAX request.
*
Expand Down Expand Up @@ -198,44 +144,4 @@ private static String getHandler(HttpServletRequest request) {
String pathInfo = request.getPathInfo();
return pathInfo == null ? "" : pathInfo.substring(1);
}

/**
* Handle serving of static resources (which we do dynamically so users
* only have to add one entry to their web.xml).
*/
@SuppressWarnings("resource")
static void handleStaticResources(String handler, HttpServletResponse response)
throws IOException {
Resource resource = RESOURCES.get(handler);
if (resource == null) {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
if (resource.getRedirect() != null) {
response.sendRedirect(resource.getRedirect());
return;
}
response.setContentType(resource.getContentType());
response.setHeader("Cache-Control", "public; max-age=300");
try {
String localPath = "ui/" + resource.getFilename();
//InputStream resourceStream = MapReduceServlet.class.getResourceAsStream(localPath);
InputStream resourceStream = StaticContentHandler.class.getResourceAsStream(localPath);
if (resourceStream == null) {
throw new RuntimeException("Missing MapReduce static file " + resource.getFilename());
}
OutputStream responseStream = response.getOutputStream();
byte[] buffer = new byte[1024];
while (true) {
int bytesRead = resourceStream.read(buffer);
if (bytesRead < 0) {
break;
}
responseStream.write(buffer, 0, bytesRead);
}
responseStream.flush();
} catch (IOException e) {
throw new RuntimeException("Couldn't read static file for MapReduce library", e);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,24 @@

import com.google.appengine.tools.mapreduce.impl.shardedjob.ShardedJobRunId;
import com.google.appengine.tools.pipeline.JobRunId;
import com.google.appengine.tools.pipeline.di.DaggerJobRunServiceComponent;
import com.google.appengine.tools.pipeline.di.JobRunServiceComponent;
import com.google.appengine.tools.pipeline.di.JobRunServiceComponentContainer;
import com.google.cloud.datastore.Key;
import com.google.appengine.tools.pipeline.util.Pair;
import com.google.common.annotations.VisibleForTesting;

import java.io.IOException;
import lombok.Setter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import lombok.Setter;
import java.io.IOException;
import java.util.Arrays;
import java.util.Optional;


/**
* Servlet that handles all requests for the Pipeline framework.
* Dispatches all requests to {@link TaskHandler}, {@link JsonTreeHandler} or
* {@link StaticContentHandler} as appropriate
* Dispatches all requests to {@link TaskHandler} or {@link JsonTreeHandler} as appropriate
*
* @author [email protected] (Mitch Rudominer)
*/
Expand Down Expand Up @@ -66,31 +64,15 @@ public void doGet(HttpServletRequest req, HttpServletResponse resp)

Pair<String, RequestType> pair = parseRequestType(req);
RequestType requestType = pair.getSecond();
String path = pair.getFirst();

switch (requestType) {
case HANDLE_TASK:
component.taskHandler().doPost(req);
break;
case GET_JSON:
component.jsonTreeHandler().doGet(req, resp);
break;
case GET_JSON_LIST:
component.jsonListHandler().doGet(req, resp);
break;
case GET_JSON_CLASS_FILTER:
component.jsonClassFilterHandler().doGet(req, resp);
break;
case ABORT_JOB:
component.abortJobHandler().doGet(req, resp);
break;
case DELETE_JOB:
component.deleteJobHandler().doGet(req, resp);
break;
case HANDLE_STATIC:
StaticContentHandler.doGet(resp, path);
break;
default:
throw new ServletException("Unknown request type: " + requestType);
case HANDLE_TASK -> component.taskHandler().doPost(req);
case GET_JSON -> component.jsonTreeHandler().doGet(req, resp);
case GET_JSON_LIST -> component.jsonListHandler().doGet(req, resp);
case GET_JSON_CLASS_FILTER -> component.jsonClassFilterHandler().doGet(req, resp);
case ABORT_JOB -> component.abortJobHandler().doGet(req, resp);
case DELETE_JOB -> component.deleteJobHandler().doGet(req, resp);
default -> throw new ServletException("Unknown request type: " + requestType);
}
}

Expand Down Expand Up @@ -122,8 +104,7 @@ private enum RequestType {
GET_JSON_LIST(JsonListHandler.PATH_COMPONENT),
GET_JSON_CLASS_FILTER(JsonClassFilterHandler.PATH_COMPONENT),
ABORT_JOB(AbortJobHandler.PATH_COMPONENT),
DELETE_JOB(DeleteJobHandler.PATH_COMPONENT),
HANDLE_STATIC("");
DELETE_JOB(DeleteJobHandler.PATH_COMPONENT);

private final String pathComponent;

Expand All @@ -137,15 +118,13 @@ public boolean matches(String path) {
}

private Pair<String, RequestType> parseRequestType(HttpServletRequest req) {
String path = req.getPathInfo();
path = path == null ? "" : path.substring(1); // Take off the leading '/'
RequestType requestType = RequestType.HANDLE_STATIC;
for (RequestType rt : RequestType.values()) {
if (rt.matches(path)) {
requestType = rt;
break;
}
}
String path = Optional.ofNullable(req.getPathInfo()).map( p -> p.substring(1)).orElse("");

RequestType requestType = Arrays.stream(RequestType.values())
.filter( rt -> rt.matches(path))
.findFirst()
.orElseThrow(() -> new RuntimeException("Unknown RequestType"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prob include the path to aid debug?


return Pair.of(path, requestType);
}
}
}
Loading
Loading