Skip to content

Commit

Permalink
FORGE-1929: Created command to generate CrossOriginResourceSharingFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
gastaldi committed Jul 10, 2014
1 parent caf6e52 commit eaaec5f
Show file tree
Hide file tree
Showing 2 changed files with 234 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/**
* Copyright 2014 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Eclipse Public License version 1.0, available at
* http://www.eclipse.org/legal/epl-v10.html
*/

package org.jboss.forge.addon.javaee.rest.ui;

import java.util.Arrays;
import java.util.List;

import javax.inject.Inject;
import javax.ws.rs.HttpMethod;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.container.ContainerResponseFilter;
import javax.ws.rs.ext.Provider;

import org.jboss.forge.addon.javaee.rest.RestFacet;
import org.jboss.forge.addon.parser.java.facets.JavaSourceFacet;
import org.jboss.forge.addon.parser.java.ui.AbstractJavaSourceCommand;
import org.jboss.forge.addon.projects.Project;
import org.jboss.forge.addon.ui.command.PrerequisiteCommandsProvider;
import org.jboss.forge.addon.ui.context.UIBuilder;
import org.jboss.forge.addon.ui.context.UIContext;
import org.jboss.forge.addon.ui.context.UIExecutionContext;
import org.jboss.forge.addon.ui.input.UIInput;
import org.jboss.forge.addon.ui.input.UISelectMany;
import org.jboss.forge.addon.ui.metadata.WithAttributes;
import org.jboss.forge.addon.ui.result.NavigationResult;
import org.jboss.forge.addon.ui.result.navigation.NavigationResultBuilder;
import org.jboss.forge.furnace.util.Lists;
import org.jboss.forge.furnace.util.OperatingSystemUtils;
import org.jboss.forge.furnace.util.Strings;
import org.jboss.forge.roaster.model.source.JavaClassSource;
import org.jboss.forge.roaster.model.source.MethodSource;

/**
* Creates a Cross Origin Resource Sharing Filter
*
* @see https://issues.jboss.org/browse/FORGE-1929
* @author <a href="ggastald@redhat.com">George Gastaldi</a>
*/
public class CrossOriginResourceSharingFilterCommand extends AbstractJavaSourceCommand<JavaClassSource> implements
PrerequisiteCommandsProvider
{

@Inject
@WithAttributes(label = "Access-Control-Allow-Origin", defaultValue = "*")
private UIInput<String> accessControlAllowOrigin;

@Inject
@WithAttributes(label = "Access-Control-Allow-Methods")
private UISelectMany<String> accessControlAllowMethods;

@Inject
@WithAttributes(label = "Access-Control-Allow-Headers", defaultValue = "content-type")
private UIInput<String> accessControlAllowHeaders;

@Override
public void initializeUI(UIBuilder builder) throws Exception
{
super.initializeUI(builder);
getNamed().setDefaultValue("NewCrossOriginResourceSharingFilter");
accessControlAllowMethods.setValueChoices(Arrays.asList(HttpMethod.GET, HttpMethod.POST, HttpMethod.PUT,
HttpMethod.DELETE, HttpMethod.HEAD, HttpMethod.OPTIONS));
accessControlAllowMethods.setDefaultValue(Arrays.asList(HttpMethod.GET, HttpMethod.POST, HttpMethod.PUT,
HttpMethod.DELETE));
builder.add(accessControlAllowOrigin).add(accessControlAllowMethods).add(accessControlAllowHeaders);
}

@Override
protected String getType()
{
return "Cross Origin Resource Sharing Filter";
}

@Override
protected Class<JavaClassSource> getSourceType()
{
return JavaClassSource.class;
}

@Override
protected String calculateDefaultPackage(UIContext context)
{
Project project = getSelectedProject(context);
return project.getFacet(JavaSourceFacet.class).getBasePackage() + ".rest";
}

@Override
public JavaClassSource decorateSource(UIExecutionContext context, Project project, JavaClassSource source)
throws Exception
{
source.addAnnotation(Provider.class);
source.addInterface(ContainerResponseFilter.class);
MethodSource<JavaClassSource> method = source.addMethod().setName("filter").setPublic().setReturnTypeVoid();
method.addAnnotation(Override.class);
// FIXME java.lang.Override shouldn't be imported
source.removeImport(Override.class);
method.addParameter(ContainerRequestContext.class, "request");
method.addParameter(ContainerResponseContext.class, "response");
StringBuilder body = new StringBuilder();
body.append("response.getHeaders().putSingle(\"Access-Control-Allow-Origin\",\"").append(
accessControlAllowOrigin.getValue()).append("\");");
body.append(OperatingSystemUtils.getLineSeparator());
body.append("response.getHeaders().putSingle(\"Access-Control-Allow-Methods\",\"");
List<String> accessMethods = Lists.toList(accessControlAllowMethods.getValue());
body.append(Strings.join(accessMethods.toArray(), ", "));
body.append("\");");
body.append(OperatingSystemUtils.getLineSeparator());
body.append("response.getHeaders().putSingle(\"Access-Control-Allow-Headers\",\"").append(
accessControlAllowHeaders.getValue()).append("\");");
method.setBody(body.toString());
return source;
}

@Override
public NavigationResult getPrerequisiteCommands(UIContext context)
{
NavigationResultBuilder builder = NavigationResultBuilder.create();
Project project = getSelectedProject(context);
if (!project.hasFacet(RestFacet.class))
{
builder.add(RestSetupWizard.class);
}
return builder.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/**
* Copyright 2014 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Eclipse Public License version 1.0, available at
* http://www.eclipse.org/legal/epl-v10.html
*/

package org.jboss.forge.addon.javaee.rest.ui;

import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;

import javax.inject.Inject;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.ext.Provider;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.forge.addon.javaee.ProjectHelper;
import org.jboss.forge.addon.javaee.rest.config.RestConfigurationStrategyFactory;
import org.jboss.forge.addon.parser.java.facets.JavaSourceFacet;
import org.jboss.forge.addon.parser.java.resources.JavaResource;
import org.jboss.forge.addon.projects.Project;
import org.jboss.forge.addon.ui.controller.CommandController;
import org.jboss.forge.addon.ui.result.Failed;
import org.jboss.forge.addon.ui.result.Result;
import org.jboss.forge.addon.ui.test.UITestHarness;
import org.jboss.forge.arquillian.AddonDependency;
import org.jboss.forge.arquillian.Dependencies;
import org.jboss.forge.arquillian.archive.ForgeArchive;
import org.jboss.forge.furnace.repositories.AddonDependencyEntry;
import org.jboss.forge.roaster.model.JavaClass;
import org.jboss.forge.roaster.model.Method;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
*
* @author <a href="ggastald@redhat.com">George Gastaldi</a>
*/
@RunWith(Arquillian.class)
public class CrossOriginResourceSharingFilterCommandTest
{
@Deployment
@Dependencies({
@AddonDependency(name = "org.jboss.forge.addon:ui"),
@AddonDependency(name = "org.jboss.forge.addon:ui-test-harness"),
@AddonDependency(name = "org.jboss.forge.addon:javaee"),
@AddonDependency(name = "org.jboss.forge.addon:maven")
})
public static ForgeArchive getDeployment()
{
return ShrinkWrap
.create(ForgeArchive.class)
.addClass(ProjectHelper.class)
.addBeansXML()
.addAsAddonDependencies(
AddonDependencyEntry.create("org.jboss.forge.furnace.container:cdi"),
AddonDependencyEntry.create("org.jboss.forge.addon:projects"),
AddonDependencyEntry.create("org.jboss.forge.addon:javaee"),
AddonDependencyEntry.create("org.jboss.forge.addon:maven"),
AddonDependencyEntry.create("org.jboss.forge.addon:ui"),
AddonDependencyEntry.create("org.jboss.forge.addon:ui-test-harness")
);
}

@Inject
private UITestHarness testHarness;

@Inject
private ProjectHelper projectHelper;

@Test
public void testCreateCrossOriginResourceSharingFilter() throws Exception
{
Project project = projectHelper.createWebProject();
projectHelper.installJAXRS_2_0(project, RestConfigurationStrategyFactory.createUsingWebXml("/rest"));
project = projectHelper.refreshProject(project);
try (CommandController controller = testHarness.createCommandController(
CrossOriginResourceSharingFilterCommand.class,
project.getRoot()))
{
controller.initialize();
Assert.assertTrue(controller.isValid());
Assert.assertTrue(controller.canExecute());
Result result = controller.execute();
Assert.assertThat(result, is(not(instanceOf(Failed.class))));
}
JavaSourceFacet facet = project.getFacet(JavaSourceFacet.class);
JavaResource filterResource = facet.getJavaResource("unknown.rest.NewCrossOriginResourceSharingFilter");
Assert.assertTrue(filterResource.exists());
Assert.assertThat(filterResource.getJavaType(), is(instanceOf(JavaClass.class)));
JavaClass<?> filterClass = filterResource.getJavaType();
Assert.assertFalse(filterClass.hasSyntaxErrors());
Assert.assertTrue(filterClass.hasAnnotation(Provider.class));
Method<?, ?> method = filterClass
.getMethod("filter", ContainerRequestContext.class, ContainerResponseContext.class);
Assert.assertNotNull(method);
}
}

0 comments on commit eaaec5f

Please sign in to comment.