From 6dd8918d8639ca69d1ba132f58b4decf7bcf7885 Mon Sep 17 00:00:00 2001 From: "Carlos E. Feria Vila" Date: Mon, 10 Jan 2022 22:53:52 +0100 Subject: [PATCH] Remove WS (#104) --- pom.xml | 11 -- .../ublhub/events/BroadcasterEvent.java | 25 --- .../events/BroadcasterEventManager.java | 46 ----- .../events/BroadcasterEventProvider.java | 33 ---- .../BroadcasterEventProviderLiteral.java | 34 ---- .../events/vertx/VertxBroadcasterEvent.java | 41 ---- .../vertx/VertxBroadcasterEventConsumer.java | 39 ---- .../jpa/entities/UBLDocumentEntity.java | 2 - .../listeners/UBLDocumentEntityListener.java | 39 ---- .../ublhub/resources/DocumentResource.java | 8 - .../websockets/AuthorizationException.java | 38 ---- .../ublhub/websockets/DocumentsEndpoint.java | 84 --------- .../websockets/keycloak/Authentication.java | 46 ----- .../KeycloakAuthenticationException.java | 38 ---- .../keycloak/KeycloakAuthenticator.java | 175 ------------------ .../ublhub/websockets/keycloak/UserInfo.java | 45 ----- .../websockets/DocumentsEndpointTest.java | 124 ------------- .../V9999.0.1__namespaces.sql | 16 -- .../V9999.0.1__documents.sql | 34 ---- 19 files changed, 878 deletions(-) delete mode 100644 src/main/java/io/github/project/openubl/ublhub/events/BroadcasterEvent.java delete mode 100644 src/main/java/io/github/project/openubl/ublhub/events/BroadcasterEventManager.java delete mode 100644 src/main/java/io/github/project/openubl/ublhub/events/BroadcasterEventProvider.java delete mode 100644 src/main/java/io/github/project/openubl/ublhub/events/BroadcasterEventProviderLiteral.java delete mode 100644 src/main/java/io/github/project/openubl/ublhub/events/vertx/VertxBroadcasterEvent.java delete mode 100644 src/main/java/io/github/project/openubl/ublhub/events/vertx/VertxBroadcasterEventConsumer.java delete mode 100644 src/main/java/io/github/project/openubl/ublhub/models/jpa/entities/listeners/UBLDocumentEntityListener.java delete mode 100644 src/main/java/io/github/project/openubl/ublhub/websockets/AuthorizationException.java delete mode 100644 src/main/java/io/github/project/openubl/ublhub/websockets/DocumentsEndpoint.java delete mode 100644 src/main/java/io/github/project/openubl/ublhub/websockets/keycloak/Authentication.java delete mode 100644 src/main/java/io/github/project/openubl/ublhub/websockets/keycloak/KeycloakAuthenticationException.java delete mode 100644 src/main/java/io/github/project/openubl/ublhub/websockets/keycloak/KeycloakAuthenticator.java delete mode 100644 src/main/java/io/github/project/openubl/ublhub/websockets/keycloak/UserInfo.java delete mode 100644 src/test/java/io/github/project/openubl/ublhub/websockets/DocumentsEndpointTest.java delete mode 100644 src/test/resources/db/io/github/project/openubl/ublhub/resources/DocumentsEndpointTest/V9999.0.1__namespaces.sql delete mode 100644 src/test/resources/db/io/github/project/openubl/ublhub/websockets/DocumentsEndpointTest/V9999.0.1__documents.sql diff --git a/pom.xml b/pom.xml index 0c90002a..5c373252 100644 --- a/pom.xml +++ b/pom.xml @@ -202,17 +202,6 @@ quarkus-container-image-jib - - - org.apache.commons - commons-lang3 - 3.8.1 - - - io.quarkus - quarkus-websockets - - org.apache.camel.quarkus diff --git a/src/main/java/io/github/project/openubl/ublhub/events/BroadcasterEvent.java b/src/main/java/io/github/project/openubl/ublhub/events/BroadcasterEvent.java deleted file mode 100644 index ec4c755e..00000000 --- a/src/main/java/io/github/project/openubl/ublhub/events/BroadcasterEvent.java +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright 2019 Project OpenUBL, Inc. and/or its affiliates - * and other contributors as indicated by the @author tags. - * - * 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. - */ -package io.github.project.openubl.ublhub.events; - -import io.github.project.openubl.ublhub.idm.DocumentRepresentation; - -public interface BroadcasterEvent { - - void broadcast(DocumentRepresentation rep); - -} diff --git a/src/main/java/io/github/project/openubl/ublhub/events/BroadcasterEventManager.java b/src/main/java/io/github/project/openubl/ublhub/events/BroadcasterEventManager.java deleted file mode 100644 index 3b33ebfe..00000000 --- a/src/main/java/io/github/project/openubl/ublhub/events/BroadcasterEventManager.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright 2019 Project OpenUBL, Inc. and/or its affiliates - * and other contributors as indicated by the @author tags. - * - * 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. - */ -package io.github.project.openubl.ublhub.events; - -import io.github.project.openubl.ublhub.idm.DocumentRepresentation; -import org.eclipse.microprofile.config.inject.ConfigProperty; - -import javax.enterprise.context.ApplicationScoped; -import javax.enterprise.inject.Any; -import javax.enterprise.inject.Instance; -import javax.inject.Inject; -import java.lang.annotation.Annotation; - -@ApplicationScoped -public class BroadcasterEventManager { - - @ConfigProperty(name = "openubl.scheduler.type") - String schedulerType; - - @Inject - @Any - Instance broadcasters; - - public void broadcast(DocumentRepresentation rep) { - BroadcasterEventProvider.Type providerType = BroadcasterEventProvider.Type.valueOf(schedulerType.toUpperCase()); - Annotation annotation = new BroadcasterEventProviderLiteral(providerType); - - BroadcasterEvent broadcaster = broadcasters.select(annotation).get(); - broadcaster.broadcast(rep); - } - -} diff --git a/src/main/java/io/github/project/openubl/ublhub/events/BroadcasterEventProvider.java b/src/main/java/io/github/project/openubl/ublhub/events/BroadcasterEventProvider.java deleted file mode 100644 index 5b3a4452..00000000 --- a/src/main/java/io/github/project/openubl/ublhub/events/BroadcasterEventProvider.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright 2019 Project OpenUBL, Inc. and/or its affiliates - * and other contributors as indicated by the @author tags. - * - * 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. - */ -package io.github.project.openubl.ublhub.events; - -import javax.inject.Qualifier; -import java.lang.annotation.*; - -@Qualifier -@Retention(RetentionPolicy.RUNTIME) -@Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER, ElementType.TYPE}) -@Documented -public @interface BroadcasterEventProvider { - Type value(); - - enum Type { - VERTX, - AMQP - } -} diff --git a/src/main/java/io/github/project/openubl/ublhub/events/BroadcasterEventProviderLiteral.java b/src/main/java/io/github/project/openubl/ublhub/events/BroadcasterEventProviderLiteral.java deleted file mode 100644 index 4a29855d..00000000 --- a/src/main/java/io/github/project/openubl/ublhub/events/BroadcasterEventProviderLiteral.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright 2019 Project OpenUBL, Inc. and/or its affiliates - * and other contributors as indicated by the @author tags. - * - * 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. - */ -package io.github.project.openubl.ublhub.events; - -import javax.enterprise.util.AnnotationLiteral; - -public class BroadcasterEventProviderLiteral extends AnnotationLiteral implements BroadcasterEventProvider { - - private final Type providerType; - - public BroadcasterEventProviderLiteral(Type providerType) { - this.providerType = providerType; - } - - @Override - public Type value() { - return providerType; - } - -} diff --git a/src/main/java/io/github/project/openubl/ublhub/events/vertx/VertxBroadcasterEvent.java b/src/main/java/io/github/project/openubl/ublhub/events/vertx/VertxBroadcasterEvent.java deleted file mode 100644 index fdfaafe3..00000000 --- a/src/main/java/io/github/project/openubl/ublhub/events/vertx/VertxBroadcasterEvent.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright 2019 Project OpenUBL, Inc. and/or its affiliates - * and other contributors as indicated by the @author tags. - * - * 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. - */ -package io.github.project.openubl.ublhub.events.vertx; - -import io.github.project.openubl.ublhub.events.BroadcasterEvent; -import io.github.project.openubl.ublhub.events.BroadcasterEventProvider; -import io.github.project.openubl.ublhub.idm.DocumentRepresentation; -import io.vertx.core.eventbus.EventBus; - -import javax.enterprise.context.ApplicationScoped; -import javax.inject.Inject; - -@ApplicationScoped -@BroadcasterEventProvider(BroadcasterEventProvider.Type.VERTX) -public class VertxBroadcasterEvent implements BroadcasterEvent { - - public static final String VERTX_SEND_DOCUMENT_EVENT = "vertx-document-event"; - - @Inject - EventBus eventBus; - - @Override - public void broadcast(DocumentRepresentation rep) { - eventBus.send(VERTX_SEND_DOCUMENT_EVENT, rep); - } - -} diff --git a/src/main/java/io/github/project/openubl/ublhub/events/vertx/VertxBroadcasterEventConsumer.java b/src/main/java/io/github/project/openubl/ublhub/events/vertx/VertxBroadcasterEventConsumer.java deleted file mode 100644 index bdb9b3bc..00000000 --- a/src/main/java/io/github/project/openubl/ublhub/events/vertx/VertxBroadcasterEventConsumer.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright 2019 Project OpenUBL, Inc. and/or its affiliates - * and other contributors as indicated by the @author tags. - * - * 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. - */ -package io.github.project.openubl.ublhub.events.vertx; - -import io.github.project.openubl.ublhub.idm.DocumentRepresentation; -import io.quarkus.vertx.ConsumeEvent; -import io.smallrye.mutiny.Uni; - -import javax.enterprise.context.ApplicationScoped; -import javax.enterprise.event.Event; -import javax.inject.Inject; - -@ApplicationScoped -public class VertxBroadcasterEventConsumer { - - @Inject - Event event; - - @ConsumeEvent(VertxBroadcasterEvent.VERTX_SEND_DOCUMENT_EVENT) - public Uni sendFile(DocumentRepresentation rep) { - event.fire(rep); - return Uni.createFrom().voidItem(); - } - -} diff --git a/src/main/java/io/github/project/openubl/ublhub/models/jpa/entities/UBLDocumentEntity.java b/src/main/java/io/github/project/openubl/ublhub/models/jpa/entities/UBLDocumentEntity.java index d37e8516..062a96da 100644 --- a/src/main/java/io/github/project/openubl/ublhub/models/jpa/entities/UBLDocumentEntity.java +++ b/src/main/java/io/github/project/openubl/ublhub/models/jpa/entities/UBLDocumentEntity.java @@ -17,7 +17,6 @@ package io.github.project.openubl.ublhub.models.jpa.entities; import io.github.project.openubl.ublhub.models.ErrorType; -import io.github.project.openubl.ublhub.models.jpa.entities.listeners.UBLDocumentEntityListener; import org.hibernate.annotations.Type; import javax.persistence.*; @@ -29,7 +28,6 @@ @Entity @Table(name = "ubl_document") -@EntityListeners(UBLDocumentEntityListener.class) public class UBLDocumentEntity extends BaseEntity { @Id diff --git a/src/main/java/io/github/project/openubl/ublhub/models/jpa/entities/listeners/UBLDocumentEntityListener.java b/src/main/java/io/github/project/openubl/ublhub/models/jpa/entities/listeners/UBLDocumentEntityListener.java deleted file mode 100644 index a125c101..00000000 --- a/src/main/java/io/github/project/openubl/ublhub/models/jpa/entities/listeners/UBLDocumentEntityListener.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright 2019 Project OpenUBL, Inc. and/or its affiliates - * and other contributors as indicated by the @author tags. - * - * 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. - */ -package io.github.project.openubl.ublhub.models.jpa.entities.listeners; - -import io.github.project.openubl.ublhub.events.BroadcasterEventManager; -import io.github.project.openubl.ublhub.idm.DocumentRepresentation; -import io.github.project.openubl.ublhub.models.jpa.entities.UBLDocumentEntity; -import io.github.project.openubl.ublhub.models.utils.EntityToRepresentation; - -import javax.enterprise.inject.spi.CDI; -import javax.persistence.PostPersist; -import javax.persistence.PostUpdate; - -public class UBLDocumentEntityListener { - - @PostPersist - @PostUpdate - private void afterAnyUpdate(UBLDocumentEntity document) { - DocumentRepresentation documentRepresentation = EntityToRepresentation.toRepresentation(document); - - BroadcasterEventManager broadcasterEventManager = CDI.current().select(BroadcasterEventManager.class).get(); - broadcasterEventManager.broadcast(documentRepresentation); - } - -} diff --git a/src/main/java/io/github/project/openubl/ublhub/resources/DocumentResource.java b/src/main/java/io/github/project/openubl/ublhub/resources/DocumentResource.java index 6ee923b0..03c2bddd 100644 --- a/src/main/java/io/github/project/openubl/ublhub/resources/DocumentResource.java +++ b/src/main/java/io/github/project/openubl/ublhub/resources/DocumentResource.java @@ -34,10 +34,8 @@ */ import io.github.project.openubl.ublhub.builder.XMLBuilderManager; -import io.github.project.openubl.ublhub.events.BroadcasterEventManager; import io.github.project.openubl.ublhub.exceptions.NoNamespaceException; import io.github.project.openubl.ublhub.files.FilesMutiny; -import io.github.project.openubl.ublhub.idm.input.InputTemplateRepresentation; import io.github.project.openubl.ublhub.keys.KeyManager; import io.github.project.openubl.ublhub.models.DocumentFilterModel; import io.github.project.openubl.ublhub.models.PageBean; @@ -63,7 +61,6 @@ import javax.enterprise.context.ApplicationScoped; import javax.inject.Inject; -import javax.validation.ConstraintViolationException; import javax.validation.constraints.NotNull; import javax.ws.rs.*; import javax.ws.rs.core.MediaType; @@ -88,11 +85,6 @@ public class DocumentResource { @Inject SchedulerManager schedulerManager; - // Needed to not remove BroadcasterEventManager at build-time - // https://github.com/quarkusio/quarkus/issues/6948 - @Inject - BroadcasterEventManager broadcasterEventManager; - @Inject NamespaceRepository namespaceRepository; diff --git a/src/main/java/io/github/project/openubl/ublhub/websockets/AuthorizationException.java b/src/main/java/io/github/project/openubl/ublhub/websockets/AuthorizationException.java deleted file mode 100644 index e103433c..00000000 --- a/src/main/java/io/github/project/openubl/ublhub/websockets/AuthorizationException.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright 2019 Project OpenUBL, Inc. and/or its affiliates - * and other contributors as indicated by the @author tags. - * - * 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. - */ -package io.github.project.openubl.ublhub.websockets; - -public class AuthorizationException extends Exception { - public AuthorizationException() { - } - - public AuthorizationException(String message) { - super(message); - } - - public AuthorizationException(String message, Throwable cause) { - super(message, cause); - } - - public AuthorizationException(Throwable cause) { - super(cause); - } - - public AuthorizationException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { - super(message, cause, enableSuppression, writableStackTrace); - } -} diff --git a/src/main/java/io/github/project/openubl/ublhub/websockets/DocumentsEndpoint.java b/src/main/java/io/github/project/openubl/ublhub/websockets/DocumentsEndpoint.java deleted file mode 100644 index defa804c..00000000 --- a/src/main/java/io/github/project/openubl/ublhub/websockets/DocumentsEndpoint.java +++ /dev/null @@ -1,84 +0,0 @@ -/* - * Copyright 2019 Project OpenUBL, Inc. and/or its affiliates - * and other contributors as indicated by the @author tags. - * - * 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. - */ -package io.github.project.openubl.ublhub.websockets; - -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import io.github.project.openubl.ublhub.idm.DocumentRepresentation; -import io.vertx.core.impl.ConcurrentHashSet; -import org.jboss.logging.Logger; - -import javax.enterprise.context.ApplicationScoped; -import javax.enterprise.event.Observes; -import javax.inject.Inject; -import javax.websocket.OnClose; -import javax.websocket.OnMessage; -import javax.websocket.Session; -import javax.websocket.server.PathParam; -import javax.websocket.server.ServerEndpoint; -import java.util.Collections; -import java.util.Map; -import java.util.Set; -import java.util.concurrent.ConcurrentHashMap; - -@ApplicationScoped -@ServerEndpoint("/namespaces/{namespaceId}/documents") -public class DocumentsEndpoint { - - private static final Logger LOG = Logger.getLogger(DocumentsEndpoint.class); - - protected static final Map sessions = new ConcurrentHashMap<>(); - protected static final Map> namespacesSessions = new ConcurrentHashMap<>(); - - @Inject - ObjectMapper objectMapper; - - @OnMessage - public void onMessage(String message, Session session, @PathParam("namespaceId") String namespaceId) { - sessions.put(session, namespaceId); - - Set newValue = namespacesSessions.getOrDefault(namespaceId, new ConcurrentHashSet<>()); - newValue.add(session); - namespacesSessions.put(namespaceId, newValue); - } - - @OnClose - public void onClose(Session session) { - String companyId = sessions.get(session); - if (companyId != null) { - sessions.remove(session); - namespacesSessions.getOrDefault(companyId, Collections.emptySet()).remove(session); - } - } - - public void documentEvent(@Observes DocumentRepresentation documentRepresentation) { - namespacesSessions - .getOrDefault(documentRepresentation.getNamespaceId(), Collections.emptySet()) - .forEach(session -> { - try { - session.getAsyncRemote().sendObject(objectMapper.writeValueAsString(documentRepresentation), result -> { - if (result.getException() != null) { - LOG.error("Unable to send message ", result.getException()); - } - }); - } catch (JsonProcessingException e) { - LOG.error("Unable encode JSON", e); - } - }); - } - -} diff --git a/src/main/java/io/github/project/openubl/ublhub/websockets/keycloak/Authentication.java b/src/main/java/io/github/project/openubl/ublhub/websockets/keycloak/Authentication.java deleted file mode 100644 index d6f53f8a..00000000 --- a/src/main/java/io/github/project/openubl/ublhub/websockets/keycloak/Authentication.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright 2019 Project OpenUBL, Inc. and/or its affiliates - * and other contributors as indicated by the @author tags. - * - * 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. - */ -package io.github.project.openubl.ublhub.websockets.keycloak; - -import io.quarkus.runtime.annotations.RegisterForReflection; - -@RegisterForReflection -public class Authentication { - - private Body authentication; - - public Body getAuthentication() { - return authentication; - } - - public void setAuthentication(Body authentication) { - this.authentication = authentication; - } - - @RegisterForReflection - public static class Body { - private String token; - - public String getToken() { - return token; - } - - public void setToken(String token) { - this.token = token; - } - } -} diff --git a/src/main/java/io/github/project/openubl/ublhub/websockets/keycloak/KeycloakAuthenticationException.java b/src/main/java/io/github/project/openubl/ublhub/websockets/keycloak/KeycloakAuthenticationException.java deleted file mode 100644 index 8fa6fb5c..00000000 --- a/src/main/java/io/github/project/openubl/ublhub/websockets/keycloak/KeycloakAuthenticationException.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright 2019 Project OpenUBL, Inc. and/or its affiliates - * and other contributors as indicated by the @author tags. - * - * 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. - */ -package io.github.project.openubl.ublhub.websockets.keycloak; - -public class KeycloakAuthenticationException extends Exception { - public KeycloakAuthenticationException() { - } - - public KeycloakAuthenticationException(String message) { - super(message); - } - - public KeycloakAuthenticationException(String message, Throwable cause) { - super(message, cause); - } - - public KeycloakAuthenticationException(Throwable cause) { - super(cause); - } - - public KeycloakAuthenticationException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { - super(message, cause, enableSuppression, writableStackTrace); - } -} diff --git a/src/main/java/io/github/project/openubl/ublhub/websockets/keycloak/KeycloakAuthenticator.java b/src/main/java/io/github/project/openubl/ublhub/websockets/keycloak/KeycloakAuthenticator.java deleted file mode 100644 index 63d378a2..00000000 --- a/src/main/java/io/github/project/openubl/ublhub/websockets/keycloak/KeycloakAuthenticator.java +++ /dev/null @@ -1,175 +0,0 @@ -/* - * Copyright 2019 Project OpenUBL, Inc. and/or its affiliates - * and other contributors as indicated by the @author tags. - * - * 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. - */ -package io.github.project.openubl.ublhub.websockets.keycloak; - -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; -import org.apache.commons.lang3.StringUtils; -import org.eclipse.microprofile.config.inject.ConfigProperty; -import org.jboss.logging.Logger; - -import javax.enterprise.context.ApplicationScoped; -import javax.inject.Inject; -import javax.net.ssl.*; -import javax.websocket.CloseReason; -import javax.websocket.Session; -import java.io.IOException; -import java.io.InputStream; -import java.net.HttpURLConnection; -import java.net.Socket; -import java.net.URL; -import java.net.URLConnection; -import java.security.KeyManagementException; -import java.security.NoSuchAlgorithmException; -import java.security.SecureRandom; -import java.security.cert.CertificateException; -import java.security.cert.X509Certificate; -import java.util.Optional; - -@ApplicationScoped -public class KeycloakAuthenticator { - - private static final Logger LOG = Logger.getLogger(KeycloakAuthenticator.class); - - @ConfigProperty(name = "quarkus.oidc.auth-server-url") - String authServerUrl; - - @Inject - ObjectMapper objectMapper; - - public Optional authenticate(String message, Session session) { - try { - String tokenValue; - try { - JsonNode jsonNode = objectMapper.readTree(message); - - JsonNode authentication = jsonNode.get("authentication"); - if (authentication == null) { - throw new KeycloakAuthenticationException("Authentication message did not contain a token"); - } - - JsonNode token = authentication.get("token"); - if (token == null) { - throw new KeycloakAuthenticationException("Authentication message did not contain a token"); - } - - tokenValue = token.asText(); - } catch (JsonProcessingException e) { - throw new KeycloakAuthenticationException("Unable to parse message due to: " + e.getMessage()); - } - - JsonNode jsonNode = validateToken(tokenValue); - String username = jsonNode.get("preferred_username").asText(); - return Optional.of(username); - } catch (KeycloakAuthenticationException e) { - LOG.warn("Received a request with an invalid token", e); - try { - String errorMessage = e.getLocalizedMessage(); - - // Shorten the message to meet the standards for "CloseReason" (only allows 122 chars) - if (StringUtils.isNotBlank(errorMessage) && errorMessage.length() >= 122) { - errorMessage = errorMessage.substring(0, 122); - } - - session.close(new CloseReason(CloseReason.CloseCodes.UNEXPECTED_CONDITION, errorMessage)); - } catch (IOException e1) { - LOG.warn(e.getMessage()); - } - - return Optional.empty(); - } - } - - public JsonNode validateToken(String token) throws KeycloakAuthenticationException { - StringBuilder keycloakUrl = new StringBuilder(authServerUrl); - - if (!authServerUrl.endsWith("/")) { - keycloakUrl.append("/"); - } - - String fullUrl = keycloakUrl.append("protocol/openid-connect/userinfo").toString(); - try { - URLConnection urlConnection = new URL(fullUrl).openConnection(); - if (urlConnection instanceof HttpsURLConnection) { - HttpsURLConnection httpsConnection = (HttpsURLConnection) urlConnection; - trustAll(httpsConnection); - } - - urlConnection.setRequestProperty("Authorization", "Bearer " + token); - - try (InputStream inputStream = urlConnection.getInputStream()) { - int responseCode = ((HttpURLConnection) urlConnection).getResponseCode(); - if (responseCode != 200) { - throw new KeycloakAuthenticationException("Failed to authenticate request (" + responseCode + "!"); - } - - // Just consume it... the main thing is that it must be a 200 code - return objectMapper.readTree(inputStream); - } - } catch (Exception e) { - throw new KeycloakAuthenticationException("Could not authenticate due to: " + e.getMessage(), e); - } - } - - private static void trustAll(HttpsURLConnection httpsConnection) throws KeyManagementException, NoSuchAlgorithmException { - httpsConnection.setHostnameVerifier((s, sslSession) -> true); - SSLContext sslContext = SSLContext.getInstance("SSL"); - TrustManager[] trustAllCerts = new TrustManager[]{ - new X509ExtendedTrustManager() { - @Override - public void checkClientTrusted(X509Certificate[] x509Certificates, String s, Socket socket) throws CertificateException { - - } - - @Override - public void checkServerTrusted(X509Certificate[] x509Certificates, String s, Socket socket) throws CertificateException { - - } - - @Override - public void checkClientTrusted(X509Certificate[] x509Certificates, String s, SSLEngine sslEngine) throws CertificateException { - - } - - @Override - public void checkServerTrusted(X509Certificate[] x509Certificates, String s, SSLEngine sslEngine) throws CertificateException { - - } - - @Override - public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException { - - } - - @Override - public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException { - - } - - @Override - public X509Certificate[] getAcceptedIssuers() { - return null; - } - } - }; - sslContext.init(null, trustAllCerts, new SecureRandom()); - - httpsConnection.setSSLSocketFactory(sslContext.getSocketFactory()); - } - -} diff --git a/src/main/java/io/github/project/openubl/ublhub/websockets/keycloak/UserInfo.java b/src/main/java/io/github/project/openubl/ublhub/websockets/keycloak/UserInfo.java deleted file mode 100644 index 5a0114f2..00000000 --- a/src/main/java/io/github/project/openubl/ublhub/websockets/keycloak/UserInfo.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright 2019 Project OpenUBL, Inc. and/or its affiliates - * and other contributors as indicated by the @author tags. - * - * 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. - */ -package io.github.project.openubl.ublhub.websockets.keycloak; - -import java.util.Objects; - -public class UserInfo { - - private String preferred_username; - - public String getPreferred_username() { - return preferred_username; - } - - public void setPreferred_username(String preferred_username) { - this.preferred_username = preferred_username; - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - UserInfo userInfo = (UserInfo) o; - return preferred_username.equals(userInfo.preferred_username); - } - - @Override - public int hashCode() { - return Objects.hash(preferred_username); - } -} diff --git a/src/test/java/io/github/project/openubl/ublhub/websockets/DocumentsEndpointTest.java b/src/test/java/io/github/project/openubl/ublhub/websockets/DocumentsEndpointTest.java deleted file mode 100644 index 883a68c7..00000000 --- a/src/test/java/io/github/project/openubl/ublhub/websockets/DocumentsEndpointTest.java +++ /dev/null @@ -1,124 +0,0 @@ -/* - * Copyright 2019 Project OpenUBL, Inc. and/or its affiliates - * and other contributors as indicated by the @author tags. - * - * 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. - */ -package io.github.project.openubl.ublhub.websockets; - -import com.fasterxml.jackson.databind.ObjectMapper; -import io.github.project.openubl.ublhub.AbstractBaseTest; -import io.github.project.openubl.ublhub.ProfileManager; -import io.github.project.openubl.ublhub.idm.DocumentRepresentation; -import io.github.project.openubl.ublhub.resources.DocumentResource; -import io.quarkus.test.common.http.TestHTTPEndpoint; -import io.quarkus.test.common.http.TestHTTPResource; -import io.quarkus.test.junit.QuarkusTest; -import io.quarkus.test.junit.TestProfile; -import io.restassured.http.ContentType; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Disabled; -import org.junit.jupiter.api.Test; - -import javax.inject.Inject; -import javax.websocket.*; -import java.io.File; -import java.net.URI; -import java.util.concurrent.LinkedBlockingDeque; -import java.util.concurrent.TimeUnit; - -import static io.restassured.RestAssured.given; -import static org.awaitility.Awaitility.await; -import static org.hamcrest.CoreMatchers.is; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; - -@Disabled -@QuarkusTest -@TestProfile(ProfileManager.class) -public class DocumentsEndpointTest extends AbstractBaseTest { - - static final int TIMEOUT = 60; - static final String nsId = "1"; - - @TestHTTPResource("/namespaces/" + nsId + "/documents") - URI uri; - - @Inject - ObjectMapper objectMapper; - - static final LinkedBlockingDeque WS_MESSAGES = new LinkedBlockingDeque<>(); - - @Override - public Class getTestClass() { - return DocumentsEndpointTest.class; - } - - @ClientEndpoint - public static class Client { - @OnOpen - public void open(Session session) { - session.getAsyncRemote().sendText("CONNECT"); // Instead of "CONNECT" we need to send a token or BASIC auth once the websocket allows authentication - } - - @OnMessage - void message(String msg) { - WS_MESSAGES.add(msg); - } - } - - @BeforeEach - public void beforeEach() { - WS_MESSAGES.clear(); - } - - @Test - public void testWebsocketChat() throws Exception { - try (Session session = ContainerProvider.getWebSocketContainer().connectToServer(Client.class, uri)) { - // Given - URI fileURI = DocumentsEndpointTest.class.getClassLoader().getResource("xml/voided-document_12345678912.xml").toURI(); - File file = new File(fileURI); - - // When - DocumentRepresentation response = given() - .accept(ContentType.JSON) - .multiPart("file", file, "application/xml") - .when() - .post("/api/namespaces/" + nsId + "/documents/upload") - .then() - .statusCode(200) - .body("inProgress", is(true)) - .extract().body().as(DocumentRepresentation.class); - - // Then - await().atMost(TIMEOUT, TimeUnit.SECONDS).until(() -> { - DocumentRepresentation watchResponse = given() - .contentType(ContentType.JSON) - .when() - - .get("/api/namespaces/" + nsId + "/documents/" + response.getId()) - .then() - .statusCode(200) - .extract().body().as(DocumentRepresentation.class); - return !watchResponse.isInProgress(); - }); - - DocumentRepresentation wsEvent = objectMapper.readValue(WS_MESSAGES.getLast(), DocumentRepresentation.class); - - assertEquals(3, WS_MESSAGES.size()); - assertFalse(wsEvent.isInProgress()); - assertEquals(wsEvent.getFileContent().getDocumentID(), "RA-20200328-1"); - assertEquals(wsEvent.getSunat().getStatus(), "ACEPTADO"); - } - } -} diff --git a/src/test/resources/db/io/github/project/openubl/ublhub/resources/DocumentsEndpointTest/V9999.0.1__namespaces.sql b/src/test/resources/db/io/github/project/openubl/ublhub/resources/DocumentsEndpointTest/V9999.0.1__namespaces.sql deleted file mode 100644 index fefa3886..00000000 --- a/src/test/resources/db/io/github/project/openubl/ublhub/resources/DocumentsEndpointTest/V9999.0.1__namespaces.sql +++ /dev/null @@ -1,16 +0,0 @@ -insert into namespace(id, name, description, created_on, sunat_username, sunat_password, sunat_url_factura, - sunat_url_guia_remision, - sunat_url_percepcion_retencion, version) -values ('1', 'my-namespace1', 'description1', CURRENT_TIMESTAMP + INTERVAL '1 day', 'username1', 'password1', - 'http://url1', 'http://url11', 'http://url111', 1), - ('2', 'my-namespace2', 'description2', CURRENT_TIMESTAMP + INTERVAL '2 day', 'username2', 'password2', - 'http://url2', 'http://url22', 'http://url222', 1), - ('3', 'my-namespace3', 'description3', CURRENT_TIMESTAMP + INTERVAL '3 day', 'username3', 'password3', - 'http://url3', 'http://url33', 'http://url333', 1); - -insert into company(id, ruc, name, created_on, namespace_id, version, sunat_username, sunat_password, - sunat_url_factura, sunat_url_guia_remision, sunat_url_percepcion_retencion) -values ('11', '11111111111', 'company1', CURRENT_TIMESTAMP + INTERVAL '1 day', '1', 1, 'username_company1', - 'password_company1', 'http://url1_company1', 'http://url2_company1', 'http://url3_company1'), - ('22', '22222222222', 'company2', CURRENT_TIMESTAMP + INTERVAL '2 day', '1', 1, 'username_company2', - 'password_company2', 'http://url1_company2', 'http://url2_company2', 'http://url3_company2'); diff --git a/src/test/resources/db/io/github/project/openubl/ublhub/websockets/DocumentsEndpointTest/V9999.0.1__documents.sql b/src/test/resources/db/io/github/project/openubl/ublhub/websockets/DocumentsEndpointTest/V9999.0.1__documents.sql deleted file mode 100644 index 608ec2b0..00000000 --- a/src/test/resources/db/io/github/project/openubl/ublhub/websockets/DocumentsEndpointTest/V9999.0.1__documents.sql +++ /dev/null @@ -1,34 +0,0 @@ --- NAMESPACES -insert into namespace(id, name, description, created_on, version) -values ('1', 'my-namespace1', 'description1', CURRENT_TIMESTAMP + INTERVAL '1 day', 1), - ('2', 'my-namespace2', 'description2', CURRENT_TIMESTAMP + INTERVAL '2 day', 1), - ('3', 'my-namespace3', 'description3', CURRENT_TIMESTAMP + INTERVAL '3 day', 1); - --- COMPANIES -insert into company(id, ruc, name, created_on, namespace_id, version, sunat_username, sunat_password, - sunat_url_factura, sunat_url_guia_remision, sunat_url_percepcion_retencion) -values --- Companies in namespace 1 -('11', '11111111111', 'company1', CURRENT_TIMESTAMP + INTERVAL '1 day', '1', 1, 'username1', 'password1', - 'http://urlFactura1', 'http://urlGuia1', 'http://urlPercepcionRetencion1'), -('22', '12345678912', 'company2', CURRENT_TIMESTAMP + INTERVAL '2 day', '1', 1, '12345678912MODDATOS', 'MODDATOS', - 'https://e-beta.sunat.gob.pe/ol-ti-itcpfegem-beta/billService', - 'https://e-beta.sunat.gob.pe/ol-ti-itemision-guia-gem-beta/billService', - 'https://e-beta.sunat.gob.pe/ol-ti-itemision-otroscpe-gem-beta/billService'), --- Companies in namespace 2 -('33', '11111111111', 'company3', CURRENT_TIMESTAMP + INTERVAL '3 day', '2', 1, 'username3', 'password3', - 'http://urlFactura3', 'http://urlGuia3', 'http://urlPercepcionRetencion3'), --- Companies in namespace 3 -('44', '44444444444', 'company4', CURRENT_TIMESTAMP + INTERVAL '4 day', '3', 1, 'username4', 'password4', - 'http://urlFactura4', 'http://urlGuia4', 'http://urlPercepcionRetencion4'); - --- DOCUMENTS -insert into ubl_document(id, namespace_id, document_id, in_progress, created_on, retries, version) -values --- Documents in namespace 1 -('11', '1', 'F-11', 'N', CURRENT_TIMESTAMP + INTERVAL '1 day', 0, 1), -('22', '1', 'F-22', 'N', CURRENT_TIMESTAMP + INTERVAL '2 day', 0, 1), --- Companies in namespace 2 -('33', '2', 'F-33', 'N', CURRENT_TIMESTAMP + INTERVAL '3 day', 0, 1), --- Companies in namespace 3 -('44', '3', 'F-44', 'N', CURRENT_TIMESTAMP + INTERVAL '4 day', 0, 1);