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

[WEJBHTTP-34] HTTP discovery implementation #29

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions ejb/pom.xml
Expand Up @@ -100,6 +100,11 @@
<artifactId>jboss-marshalling-river</artifactId>
</dependency>

<dependency>
<groupId>org.kohsuke.metainf-services</groupId>
<artifactId>metainf-services</artifactId>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
3 changes: 3 additions & 0 deletions ejb/src/main/java/org/wildfly/httpclient/ejb/EjbHeaders.java
Expand Up @@ -28,12 +28,15 @@ interface EjbHeaders {
//request headers
String INVOCATION_VERSION_ONE = "application/x-wf-ejb-jbmar-invocation;version=1";
String SESSION_OPEN_VERSION_ONE = "application/x-wf-jbmar-sess-open;version=1";
String DISCOVERY_VERSION_ONE = "application/x-wf-ejb-jbmar-discovery;version=1";
String SESSION_OPEN = "application/x-wf-jbmar-sess-open";
String INVOCATION = "application/x-wf-ejb-jbmar-invocation";
String DISCOVERY = "application/x-wf-ejb-jbmar-discovery";

//response headers
ContentType EJB_RESPONSE_VERSION_ONE = new ContentType("application/x-wf-ejb-jbmar-response", 1);
ContentType EJB_RESPONSE_NEW_SESSION = new ContentType("application/x-wf-ejb-jbmar-new-session", 1);
ContentType EJB_DISCOVERY_RESPONSE_VERSION_ONE = new ContentType("application/x-wf-ejb-jbmar-discovery-response", 1);

HttpString EJB_SESSION_ID = new HttpString("x-wf-ejb-jbmar-session-id");
HttpString INVOCATION_ID = new HttpString("X-wf-invocation-id");
Expand Down
Expand Up @@ -24,6 +24,8 @@
import org.jboss.ejb.client.EJBLocator;
import org.jboss.logging.BasicLogger;
import org.jboss.logging.Logger;
import org.jboss.logging.annotations.Cause;
import org.jboss.logging.annotations.LogMessage;
import org.jboss.logging.annotations.Message;
import org.jboss.logging.annotations.MessageLogger;

Expand Down Expand Up @@ -63,4 +65,12 @@ interface EjbHttpClientMessages extends BasicLogger {

@Message(id = 11, value = "Invalid transaction type %s")
IOException invalidTransactionType(int type);

@LogMessage(level = Logger.Level.ERROR)
@Message(id = 12, value = "Unable to perform EJB discovery")
void unableToPerformEjbDiscovery(@Cause Throwable e);

@LogMessage(level = Logger.Level.INFO)
@Message(id = 13, value = "HTTP discovery has been interrupted")
void httpDiscoveryInterrupted(@Cause InterruptedException e);
}
Expand Up @@ -59,7 +59,8 @@ public HttpHandler createHttpHandler() {
PathHandler pathHandler = new PathHandler();
pathHandler.addPrefixPath("/v1/invoke", new AllowedMethodsHandler(new HttpInvocationHandler(association, executorService, localTransactionContext, cancellationFlags), Methods.POST))
.addPrefixPath("/v1/open", new AllowedMethodsHandler(new HttpSessionOpenHandler(association, executorService, localTransactionContext), Methods.POST))
.addPrefixPath("/v1/cancel", new AllowedMethodsHandler(new HttpCancelHandler(association, executorService, localTransactionContext, cancellationFlags), Methods.DELETE));
.addPrefixPath("/v1/cancel", new AllowedMethodsHandler(new HttpCancelHandler(association, executorService, localTransactionContext, cancellationFlags), Methods.DELETE))
.addPrefixPath("/v1/discover", new AllowedMethodsHandler(new HttpDiscoveryHandler(executorService, association), Methods.GET));
EncodingHandler encodingHandler = new EncodingHandler(pathHandler, new ContentEncodingRepository().addEncodingHandler(Headers.GZIP.toString(), new GzipEncodingProvider(), 1));
RequestEncodingHandler requestEncodingHandler = new RequestEncodingHandler(encodingHandler);
requestEncodingHandler.addEncoding(Headers.GZIP.toString(), GzipStreamSourceConduit.WRAPPER);
Expand Down
Expand Up @@ -60,4 +60,8 @@ public EJBReceiver getReceiver(EJBReceiverContext ejbReceiverContext, String s)
throw EjbHttpClientMessages.MESSAGES.couldNotCreateHttpEjbReceiverFor(s);
}

@Override
public void close(EJBReceiverContext receiverContext) throws Exception {

}
}
@@ -0,0 +1,40 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2020 Red Hat, Inc., and individual 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 org.wildfly.httpclient.ejb;

import java.util.function.Consumer;

import org.kohsuke.MetaInfServices;
import org.wildfly.discovery.spi.DiscoveryProvider;
import org.wildfly.discovery.spi.ExternalDiscoveryConfigurator;
import org.wildfly.discovery.spi.RegistryProvider;

/**
* @author <a href="mailto:tadamski@redhat.com">Tomasz Adamski</a>
*/

@MetaInfServices
public final class HttpDiscoveryConfigurator implements ExternalDiscoveryConfigurator {

private static final HttpEJBDiscoveryProvider discoveryProvider = new HttpEJBDiscoveryProvider();

public void configure(final Consumer<DiscoveryProvider> discoveryProviderConsumer, final Consumer<RegistryProvider> registryProviderConsumer) {
discoveryProviderConsumer.accept(discoveryProvider);
}
}
@@ -0,0 +1,83 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2020 Red Hat, Inc., and individual 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 org.wildfly.httpclient.ejb;

import io.undertow.server.HttpServerExchange;
import io.undertow.util.Headers;
import org.jboss.ejb.client.EJBModuleIdentifier;
import org.jboss.ejb.server.Association;
import org.jboss.ejb.server.ModuleAvailabilityListener;
import org.jboss.marshalling.Marshaller;
import org.jboss.marshalling.Marshalling;
import org.jboss.marshalling.MarshallingConfiguration;
import org.jboss.marshalling.river.RiverMarshallerFactory;
import org.wildfly.httpclient.common.NoFlushByteOutput;

import java.io.ByteArrayOutputStream;
import java.nio.ByteBuffer;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ExecutorService;

/**
* @author <a href="mailto:tadamski@redhat.com">Tomasz Adamski</a>
*/

public class HttpDiscoveryHandler extends RemoteHTTPHandler {

private final Set<EJBModuleIdentifier> availableModules = new HashSet<>();

public HttpDiscoveryHandler(ExecutorService executorService, Association association) {
super(executorService);
association.registerModuleAvailabilityListener(new ModuleAvailabilityListener() {
@Override
public void moduleAvailable(List<EJBModuleIdentifier> modules) {
availableModules.addAll(modules);
}

@Override
public void moduleUnavailable(List<EJBModuleIdentifier> modules) {
availableModules.removeAll(modules);
}
});
}

@Override
protected void handleInternal(HttpServerExchange exchange) throws Exception {
exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, EjbHeaders.EJB_DISCOVERY_RESPONSE_VERSION_ONE.toString());
final ByteArrayOutputStream out = new ByteArrayOutputStream();
Marshaller marshaller = new RiverMarshallerFactory().createMarshaller(createMarshallingConfig());
marshaller.start(new NoFlushByteOutput(Marshalling.createByteOutput(out)));
marshaller.writeInt(availableModules.size());
for (EJBModuleIdentifier ejbModuleIdentifier : availableModules) {
marshaller.writeObject(ejbModuleIdentifier);
}
marshaller.finish();
marshaller.flush();
exchange.getResponseSender().send(ByteBuffer.wrap(out.toByteArray()));
}

private MarshallingConfiguration createMarshallingConfig() {
final MarshallingConfiguration marshallingConfiguration = new MarshallingConfiguration();
marshallingConfiguration.setObjectTable(HttpProtocolV1ObjectTable.INSTANCE);
marshallingConfiguration.setVersion(2);
return marshallingConfiguration;
}
}