Skip to content

Commit

Permalink
Run ViewResponseFilter only if controller was executed successfully
Browse files Browse the repository at this point in the history
This PR should fix eclipse-ee4j#34 and eclipse-ee4j#91
  • Loading branch information
chkal committed Oct 21, 2019
1 parent 987986a commit ae0b5c1
Show file tree
Hide file tree
Showing 10 changed files with 350 additions and 5 deletions.
16 changes: 13 additions & 3 deletions core/src/main/java/org/eclipse/krazo/core/ViewResponseFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import javax.enterprise.event.Event;
import javax.inject.Inject;
import org.eclipse.krazo.engine.Viewable;
import org.eclipse.krazo.lifecycle.RequestLifecycle;

import javax.mvc.Controller;
import javax.mvc.View;
import javax.mvc.event.ControllerRedirectEvent;
Expand Down Expand Up @@ -82,7 +84,7 @@
public class ViewResponseFilter implements ContainerResponseFilter {

private static final String FILTER_EXECUTED_KEY = ViewResponseFilter.class.getName() + ".EXECUTED";

private static final String REDIRECT = "redirect:";

@Context
Expand All @@ -103,6 +105,9 @@ public class ViewResponseFilter implements ContainerResponseFilter {
@Inject
private KrazoConfig krazoConfig;

@Inject
private RequestLifecycle requestLifecycle;

@Override
public void filter(ContainerRequestContext requestContext,
ContainerResponseContext responseContext) throws IOException {
Expand All @@ -114,7 +119,12 @@ public void filter(ContainerRequestContext requestContext,
} else {
request.setAttribute(FILTER_EXECUTED_KEY, true);
}


// the following code should only execute for the controller happy path
if (!requestLifecycle.isControllerExecuted()) {
return;
}

final Method method = resourceInfo.getResourceMethod();
final Class<?> returnType = method.getReturnType();

Expand All @@ -139,7 +149,7 @@ public void filter(ContainerRequestContext requestContext,
if (responseContext.getStatusInfo().getStatusCode() == Response.Status.NO_CONTENT.getStatusCode()) {
responseContext.setStatusInfo(Response.Status.OK);
}

} else if (returnType == Void.TYPE) {
throw new ServerErrorException(messages.get("VoidControllerNoView", resourceInfo.getResourceMethod()), INTERNAL_SERVER_ERROR);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public class RequestLifecycle {
@Inject
private MvcContextImpl mvc;

private boolean controllerExecuted = false;

public void beforeAll(ContainerRequestContext context) {

// initialize request locale
Expand All @@ -54,12 +56,19 @@ public Object aroundController(Callable<Object> controllerMethod) throws Excepti
eventDispatcher.fireBeforeControllerEvent();
try {

return controllerMethod.call();
Object result = controllerMethod.call();
controllerExecuted = true;

return result;

} finally {
eventDispatcher.fireAfterControllerEvent();
}

}

public boolean isControllerExecuted() {
return controllerExecuted;
}

}
1 change: 0 additions & 1 deletion testsuite/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@
<groupId>org.eclipse.krazo</groupId>
<artifactId>krazo-core</artifactId>
<version>${project.version}</version>
<scope>runtime</scope>
</dependency>

<!-- Arquillian -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright © 2019 Eclipse Krazo committers and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/

package org.eclipse.krazo.test.mapper;

import javax.inject.Inject;
import javax.mvc.Controller;
import javax.mvc.Models;
import javax.ws.rs.GET;
import javax.ws.rs.Path;

@Controller
@Path("/mappers")
public class FailingController {

@Inject
private Models models;

@GET
@Path("/success")
public String success() {
models.put("message", "Controller was executed without errors!");
return "success.jsp";
}

@GET
@Path("/fail-plain-text")
public String failPlainText() {
throw new PlainTextException("Error thrown by controller");
}

@GET
@Path("/fail-mvc-view")
public String failMvcView() {
throw new MvcViewException("Error thrown by controller");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright © 2019 Eclipse Krazo committers and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/

package org.eclipse.krazo.test.mapper;

public class MvcViewException extends RuntimeException {

private static final long serialVersionUID = 5452675105452096791L;

public MvcViewException(String message) {
super(message);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright © 2019 Eclipse Krazo committers and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/

package org.eclipse.krazo.test.mapper;

import org.eclipse.krazo.core.ModelsImpl;
import org.eclipse.krazo.engine.Viewable;

import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

@Provider
public class MvcViewExceptionMapper implements ExceptionMapper<MvcViewException> {

@Override
public Response toResponse(MvcViewException exception) {

ModelsImpl models = new ModelsImpl();
models.put("error", exception.getMessage());
Viewable viewable = new Viewable("mvc-error.jsp", models);

return Response.status(492)
.entity(viewable)
.build();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright © 2019 Eclipse Krazo committers and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/

package org.eclipse.krazo.test.mapper;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("mvc")
public class MyApplication extends Application {

// nothing

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright © 2019 Eclipse Krazo committers and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/

package org.eclipse.krazo.test.mapper;

public class PlainTextException extends RuntimeException {

private static final long serialVersionUID = 5452675105452096791L;

public PlainTextException(String message) {
super(message);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright © 2019 Eclipse Krazo committers and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/

package org.eclipse.krazo.test.mapper;


import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

@Provider
public class PlainTextExceptionMapper implements ExceptionMapper<PlainTextException> {

@Override
public Response toResponse(PlainTextException exception) {
return Response.status(491)
.type(MediaType.TEXT_PLAIN_TYPE)
.entity("Exception caught: " + exception.getMessage())
.build();
}

}
Loading

0 comments on commit ae0b5c1

Please sign in to comment.