HttpRequestHandler extends EndpointHandler<ChannelHandlerContext, FullHttpRequest> { @Override protected IObject getEnvironment(final ChannelHandlerContext ctx, final FullHttpRequest request) throws RequestHandlerDataException, RequestHandlerInternalException, ReadValueException { try { IObject message = IOC.resolve(Keys.getOrAdd("EmptyIObject")); if (!request.method().toString().equals("GET")) { IDeserializeStrategy deserializeStrategy = IOC.resolve( Keys.getOrAdd("info.smart_tools.smartactors.endpoint.interfaces.ideserialize_strategy.IDeserializeStrategy"), IOC.resolve(Keys.getOrAdd("http_request_key_for_deserialize"), request), name ); try { message = deserializeStrategy.deserialize(request); } catch (DeserializationException e) { IObject exception = IOC.resolve(Keys.getOrAdd("HttpPostParametersToIObjectException")); ctx.writeAndFlush(formExceptionalResponse(exception)); throw new RequestHandlerDataException(e); } } IAddRequestParametersToIObject requestParametersToIObject = IOC.resolve( Keys.getOrAdd("info.smart_tools.smartactors.endpoint.interfaces.ideserialize_strategy.IDeserializeStrategy"), "HTTP_GET", name ); try { requestParametersToIObject.extract(message, request); } catch (Exception e) { IObject exceptionalResponse = IOC.resolve(Keys.getOrAdd("HttpRequestParametersToIObjectException")); throw new RequestHandlerDataException(e); } IObject environment = IOC.resolve(Keys.getOrAdd("EmptyIObject")); IChannelHandler channelHandler = IOC.resolve(Keys.getOrAdd("info.smart_tools.smartactors.http_endpoint.channel_handler_netty.ChannelHandlerNetty"), ctx); IObject context = IOC.resolve(Keys.getOrAdd("EmptyIObject")); context.setValue(channelFieldName, channelHandler); context.setValue(cookiesFieldName, new ArrayList<IObject>()); context.setValue(headersFieldName, new ArrayList<IObject>()); context.setValue(requestFieldName, request); context.setValue(endpointName, name); context.setValue(responseStrategyName, IOC.resolve(Keys.getOrAdd("endpoint response strategy"))); context.setValue(httpResponseIsSentFieldName, false); IAction<IObject> httpFinalAction = new IAction<IObject>() { @Override public void execute(final IObject environment) throws ActionExecuteException, InvalidArgumentException { try { IObject context = (IObject) environment.getValue(contextFieldName); if (null != context) { if ((boolean) context.getValue(httpResponseIsSentFieldName)) { return; } } IFieldName channelFieldName = IOC.resolve(Keys.getOrAdd("info.smart_tools.smartactors.iobject.ifield_name.IFieldName"), "channel"); IChannelHandler channelHandler = (IChannelHandler) context.getValue(channelFieldName); IResponse response = IOC.resolve(Keys.getOrAdd(IResponse.class.getCanonicalName())); response.setContent("".getBytes()); IResponseSender sender = IOC.resolve(Keys.getOrAdd(IResponseSender.class.getCanonicalName()), IOC.resolve(Keys.getOrAdd("http_request_key_for_response_sender"), environment), name); Boolean accessForbidden = (Boolean) context.getValue(accessForbiddenFieldName); if (accessForbidden != null && accessForbidden) { context.setValue(httpResponseStatusCodeFieldName, NOT_FOUND_ERROR_STATUS_CODE); } else { context.setValue(httpResponseStatusCodeFieldName, INTERNAL_SERVER_ERROR_STATUS_CODE); } sender.send(response, environment, channelHandler); } catch (ResolutionException | ReadValueException | ResponseSendingException | ChangeValueException e) { throw new ActionExecuteException("Could not execute final http action."); } } }; ArrayList<IAction<IObject>> finalActions = new ArrayList<>(); finalActions.add(httpFinalAction); context.setValue(finalActionsFieldName, finalActions); environment.setValue(messageFieldName, message); environment.setValue(contextFieldName, context); return environment; } catch (InvalidArgumentException | SerializeException | ChangeValueException | ResolutionException e) { try { IObject exception = IOC.resolve(Keys.getOrAdd("HttpInternalException"), e); ctx.writeAndFlush(formExceptionalResponse(exception)); throw new RequestHandlerInternalException(e); } catch (SerializeException | ResolutionException | InvalidArgumentException e1) { throw new RequestHandlerInternalException("Failed to send response", e); } } finally { request.release(); } } HttpRequestHandler(
            final IScope scope, final IEnvironmentHandler environmentHandler, final IReceiverChain receiver,
            final String name, final IUpCounter upCounter); @Override void handle(final ChannelHandlerContext ctx, final FullHttpRequest request);  }
@Test public void testDeserialization() throws Exception { ChannelHandlerContext ctx = mock(ChannelHandlerContext.class); FullHttpRequest request = mock(FullHttpRequest.class); IObject message = new DSObject("{\"hello\": \"world\"}"); when(deserializeStrategy.deserialize(request)).thenReturn(message); when(request.method()).thenReturn(HttpMethod.POST); HttpRequestHandler requestHandler = new HttpRequestHandler(ScopeProvider.getCurrentScope(), null, null, null, mock(IUpCounter.class)); IObject environment = requestHandler.getEnvironment(ctx, request); assertEquals(environment.getValue(new FieldName("message")), message); }
@Test(expected = RequestHandlerDataException.class) public void testBadRequestBodyException() throws Exception { ChannelHandlerContext ctx = mock(ChannelHandlerContext.class); FullHttpRequest request = mock(FullHttpRequest.class); IObject message = new DSObject("{\"hello\": \"world\"}"); when(deserializeStrategy.deserialize(request)).thenThrow(DeserializationException.class); when(request.method()).thenReturn(HttpMethod.POST); HttpRequestHandler requestHandler = new HttpRequestHandler(ScopeProvider.getCurrentScope(), null, null, null, mock(IUpCounter.class)); IOC.register(Keys.getOrAdd("HttpPostParametersToIObjectException"), new SingletonStrategy( new DSObject("{\"statusCode\": 200}") ) ); IObject environment = requestHandler.getEnvironment(ctx, request); verify(ctx.writeAndFlush(any())); }
@Test(expected = RequestHandlerDataException.class) public void testBadRequestUriException() throws Exception { ChannelHandlerContext ctx = mock(ChannelHandlerContext.class); FullHttpRequest request = mock(FullHttpRequest.class); IObject message = new DSObject("{\"hello\": \"world\"}"); when(request.method()).thenReturn(HttpMethod.GET); doThrow(new AddRequestParametersToIObjectException("exception")).when(requestParametersToIObject).extract(any(), any()); HttpRequestHandler requestHandler = new HttpRequestHandler(ScopeProvider.getCurrentScope(), null, null, null, mock(IUpCounter.class)); IOC.register(Keys.getOrAdd("HttpRequestParametersToIObjectException"), new SingletonStrategy( new DSObject("{\"statusCode\": 200}") ) ); IObject environment = requestHandler.getEnvironment(ctx, request); }