Skip to content

feat(zip): Adding handler to return a zip object to the client. #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 18, 2018
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 @@ -5,13 +5,15 @@
import com.networknt.colfer.ColferRpc;
import com.networknt.config.Config;
import com.networknt.exception.ExpiredTokenException;
import com.networknt.rpc.Handler;
import com.networknt.security.JwtHelper;
import com.networknt.status.Status;
import com.networknt.utility.Constants;
import io.undertow.server.HttpHandler;
import io.undertow.server.HttpServerExchange;
import io.undertow.server.handlers.form.FormData;
import io.undertow.util.HeaderMap;
import io.undertow.util.StatusCodes;
import org.jose4j.jwt.JwtClaims;
import org.jose4j.jwt.MalformedClaimException;
import org.jose4j.jwt.consumer.InvalidJwtException;
Expand All @@ -24,8 +26,11 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.ByteBuffer;
import java.util.*;

import static com.networknt.rpc.router.JsonHandler.STATUS_HANDLER_NOT_FOUND;

/**
* Created by steve on 19/02/17.
*/
Expand Down Expand Up @@ -74,7 +79,7 @@ public String getServiceId(ColferRpc cf) {
(cf.version == null? "" : cf.version);
}

public String getServiceId(FormData formData) {
String getServiceId(FormData formData) {
return (formData.contains("host") ? formData.get("host").peek().getValue() + "/" : "") +
(formData.contains("service") ? formData.get("service").peek().getValue() + "/" : "") +
(formData.contains("action") ? formData.get("action").peek().getValue() + "/" : "") +
Expand Down Expand Up @@ -166,4 +171,39 @@ private boolean matchedScopes(List<String> jwtScopes, List<String> specScopes) {
}
return matched;
}

/**
* Helper method to get handler from config and act accordingly in error scenarios.
* @param serviceId The service id of the handler to receive.
* @param httpServerExchange The exchange object with the client.
* @return A handler if it is found. null otherwise.
*/
Handler getHandlerOrPopulateExchange(String serviceId, HttpServerExchange httpServerExchange) {
Handler handler = RpcStartupHookProvider.serviceMap.get(serviceId);
if (handler == null) {
Status status = new Status(STATUS_HANDLER_NOT_FOUND, serviceId);
httpServerExchange.setStatusCode(status.getStatusCode());
httpServerExchange.getResponseSender().send(status.toString());
return null;
}
return handler;
}

/**
* Helper method to send requests containing FormData to the handler and process return status' appropriately.
* @param handler The handler who will be running business logic.
* @param formData The data that will be passed into the handler.
* @param httpServerExchange The exchange object with the client.
*/
void handleFormDataRequest(Handler handler, FormData formData, HttpServerExchange httpServerExchange) {
ByteBuffer result = handler.handle(formData);
if (result == null) {
// there is nothing returned from the handler.
httpServerExchange.setStatusCode(StatusCodes.OK);
httpServerExchange.endExchange();
} else {
httpServerExchange.setStatusCode(StatusCodes.OK);
httpServerExchange.getResponseSender().send(result);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
import static com.networknt.rpc.router.JsonHandler.STATUS_HANDLER_NOT_FOUND;

/**
* Created by Nicholas Azar on July 10, 2017.
* @author Nicholas Azar
* Created on July 10, 2017
*/
public class MultipartHandler extends AbstractRpcHandler {

Expand All @@ -40,28 +41,15 @@ public void handleRequest(HttpServerExchange httpServerExchange) throws Exceptio
try {
FormData data = parser.parseBlocking();
String serviceId = getServiceId(data);
Handler handler = RpcStartupHookProvider.serviceMap.get(serviceId);
if (handler == null) {
Status status = new Status(STATUS_HANDLER_NOT_FOUND, serviceId);
httpServerExchange.setStatusCode(status.getStatusCode());
httpServerExchange.getResponseSender().send(status.toString());
Handler handler = getHandlerOrPopulateExchange(serviceId, httpServerExchange);
if (handler == null) { // exchange has been populated
return;
}

// calling jwt scope verification here. token signature and expiration are done
verifyJwt(config, serviceId, httpServerExchange);

ByteBuffer result = handler.handle(data);

logger.exit(result);
if (result == null) {
// there is nothing returned from the handler.
httpServerExchange.setStatusCode(StatusCodes.OK);
httpServerExchange.endExchange();
} else {
httpServerExchange.setStatusCode(StatusCodes.OK);
httpServerExchange.getResponseSender().send(result);
}
handleFormDataRequest(handler, data, httpServerExchange);

} catch (IOException e) {
e.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public HttpHandler getHandler() {
return Handlers.path()
.addPrefixPath("/api/colfer", new ColferHandler())
.addPrefixPath("/api/json", new JsonHandler())
.addPrefixPath("/api/multipart", new MultipartHandler());
.addPrefixPath("/api/multipart", new MultipartHandler())
.addPrefixPath("/api/zip", new ZipRespHandler());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.networknt.rpc.router;

import com.networknt.config.Config;
import com.networknt.rpc.Handler;
import com.networknt.security.JwtHelper;
import io.undertow.server.HttpServerExchange;
import io.undertow.server.handlers.form.FormData;
import io.undertow.server.handlers.form.FormDataParser;
import io.undertow.server.handlers.form.FormParserFactory;
import io.undertow.util.HttpString;
import io.undertow.util.StatusCodes;
import org.slf4j.ext.XLogger;
import org.slf4j.ext.XLoggerFactory;

import java.util.Map;

/**
* @author Nicholas Azar
* Created on March 17, 2018
*/
public class ZipRespHandler extends AbstractRpcHandler {

static private final XLogger logger = XLoggerFactory.getXLogger(MultipartHandler.class);
static final Map<String, Object> config = Config.getInstance().getJsonMapConfig(JwtHelper.SECURITY_CONFIG);

@Override
public void handleRequest(HttpServerExchange httpServerExchange) throws Exception {

// Not providing a filename. In this case the browser should show something like file.zip and let the user name it.
httpServerExchange.getResponseHeaders()
.add(new HttpString("Content-Type"), "application/zip")
.add(new HttpString("Content-Disposition"), "attachment");

// A form submission is how the request for the file should come in. Downloads from ajax is not simple.
FormParserFactory.Builder builder = FormParserFactory.builder();
FormDataParser parser = builder.build().createParser(httpServerExchange);

if (parser != null) {
httpServerExchange.startBlocking();

FormData formData = parser.parseBlocking();
String serviceId = getServiceId(formData);
Handler handler = getHandlerOrPopulateExchange(serviceId, httpServerExchange);
if (handler == null) { // exchange has been populated.
return;
}
verifyJwt(config, serviceId, httpServerExchange);

// Calculate the response and return it to the client.
handleFormDataRequest(handler, formData, httpServerExchange);
} else {
logger.error("Form could not be retrieved from request.");
httpServerExchange.setStatusCode(StatusCodes.BAD_REQUEST);
httpServerExchange.getResponseSender().send(StatusCodes.BAD_REQUEST_STRING);
}
}
}