Skip to content
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

Fix issue with optional CDI in BV module #4574

Merged
merged 1 commit into from
Sep 23, 2020
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
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2019 Payara Foundation and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand All @@ -18,7 +18,6 @@
package org.glassfish.jersey.server.validation.internal;

import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.List;
import java.util.WeakHashMap;
Expand Down Expand Up @@ -259,9 +258,8 @@ private ConfiguredValidator getDefaultValidator() {
private ValidatorContext getDefaultValidatorContext(final ValidateOnExecutionHandler handler) {
final ValidatorContext context = factory.usingContext();

// if CDI is available use composite factiry
if (AccessController.doPrivileged(
ReflectionHelper.classForNamePA("javax.enterprise.inject.spi.BeanManager")) != null) {
// if CDI is available use composite factory
if (isCDIAvailable()) {
// Composite Configuration - due to PAYARA-2491
// https://github.com/payara/Payara/issues/2245
context.constraintValidatorFactory(resourceContext.getResource(
Expand All @@ -277,6 +275,15 @@ private ValidatorContext getDefaultValidatorContext(final ValidateOnExecutionHan
return context;
}

private boolean isCDIAvailable() {
// Both CDI & Jersey CDI modules must be available
return AccessController.doPrivileged(
ReflectionHelper.classForNamePA("javax.enterprise.inject.spi.BeanManager")) != null
&&
AccessController.doPrivileged(
ReflectionHelper.classForNamePA("org.glassfish.jersey.ext.cdi1x.internal.CdiUtil")) != null;
}

/**
* Create traversable resolver able to process {@link javax.validation.executable.ValidateOnExecution} annotation on
* beans.
Expand Down
59 changes: 59 additions & 0 deletions tests/integration/jersey-4542/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved.

This program and the accompanying materials are made available under the
terms of the Eclipse Public License v. 2.0, which is available at
http://www.eclipse.org/legal/epl-2.0.

This Source Code may also be made available under the following Secondary
Licenses when the conditions for such availability set forth in the
Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
version 2 with the GNU Classpath Exception, which is available at
https://www.gnu.org/software/classpath/license.html.

SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0

-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>project</artifactId>
<groupId>org.glassfish.jersey.tests.integration</groupId>
<version>2.32.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>jersey-4542</artifactId>

<dependencies>
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-bean-validation</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
<artifactId>jersey-test-framework-provider-grizzly2</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
<artifactId>jersey-test-framework-provider-external</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.test-framework</groupId>
<artifactId>jersey-test-framework-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.weld.se</groupId>
<artifactId>weld-se-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package org.glassfish.jersey.tests.integration.jersey4542;

import java.io.IOException;

import javax.ws.rs.container.ContainerRequestContext;

import javax.validation.constraints.NotNull;

import org.glassfish.jersey.message.internal.ReaderWriter;
import org.glassfish.jersey.process.Inflector;

/**
* @author Michal Gajdos
*/
public class ValidationInflector implements Inflector<ContainerRequestContext, String> {

@NotNull
@Override
public String apply(final ContainerRequestContext requestContext) {
return get(requestContext);
}

@NotNull
public String get(@NotNull final ContainerRequestContext requestContext) {
try {
final String entity = ReaderWriter.readFromAsString(
requestContext.getEntityStream(),
requestContext.getMediaType());

return entity.isEmpty() ? null : entity;
} catch (IOException e) {
return "error";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package org.glassfish.jersey.tests.integration.jersey4542;

import java.util.HashSet;
import java.util.Set;

import javax.ws.rs.client.Entity;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.glassfish.jersey.inject.hk2.Hk2InjectionManagerFactory;
import org.glassfish.jersey.logging.LoggingFeature;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.model.Resource;
import org.glassfish.jersey.test.JerseyTest;

import static org.junit.Assert.assertEquals;

import org.glassfish.jersey.test.external.ExternalTestContainerFactory;
import org.jboss.weld.environment.se.Weld;
import org.junit.Assume;
import org.junit.Before;
import org.junit.Test;

/**
* Bean Validation tests for programmatically created resources.
*
* @author Michal Gajdos
*/
public class ProgrammaticValidationTest extends JerseyTest {

Weld weld;

@Before
public void setup() {
Assume.assumeTrue(Hk2InjectionManagerFactory.isImmediateStrategy());
}

@Override
public void setUp() throws Exception {
if (Hk2InjectionManagerFactory.isImmediateStrategy()) {
if (!ExternalTestContainerFactory.class.isAssignableFrom(getTestContainerFactory().getClass())) {
weld = new Weld();
weld.initialize();
}
super.setUp();
}
}

@Override
public void tearDown() throws Exception {
if (Hk2InjectionManagerFactory.isImmediateStrategy()) {
if (!ExternalTestContainerFactory.class.isAssignableFrom(getTestContainerFactory().getClass())) {
weld.shutdown();
}
super.tearDown();
}
}

@Override
protected Application configure() {
final Set<Resource> resources = new HashSet<>();

Resource.Builder resourceBuilder = Resource.builder("class");
resourceBuilder
.addMethod("POST")
.handledBy(ValidationInflector.class);
resources.add(resourceBuilder.build());

return new ResourceConfig().register(LoggingFeature.class).registerResources(resources);
}

@Test
public void testInflectorClass() throws Exception {
final Response response = target("class").request().post(Entity.entity("value", MediaType.TEXT_PLAIN_TYPE));

assertEquals(200, response.getStatus());
assertEquals("value", response.readEntity(String.class));
}

@Test
public void testInflectorClassNegative() throws Exception {
final Response response = target("class").request().post(Entity.entity(null, MediaType.TEXT_PLAIN_TYPE));

assertEquals(500, response.getStatus());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved.

This program and the accompanying materials are made available under the
terms of the Eclipse Public License v. 2.0, which is available at
http://www.eclipse.org/legal/epl-2.0.

This Source Code may also be made available under the following Secondary
Licenses when the conditions for such availability set forth in the
Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
version 2 with the GNU Classpath Exception, which is available at
https://www.gnu.org/software/classpath/license.html.

SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0

-->

<beans/>
1 change: 1 addition & 0 deletions tests/integration/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
<module>jersey-4099</module>
<module>jersey-4321</module>
<module>jersey-4507</module>
<module>jersey-4542</module>
<module>jetty-response-close</module>
<module>microprofile</module>
<module>portability-jersey-1</module>
Expand Down