Search in sources :

Example 11 with DolphinRuntimeException

use of com.canoo.platform.core.DolphinRuntimeException in project dolphin-platform by canoo.

the class KeycloakSecurity method receiveToken.

private KeycloakOpenidConnectResult receiveToken(final HttpClientConnection connection, final String content) throws IOException {
    Assert.requireNonNull(content, "content");
    LOG.debug("receiving new token from keycloak server");
    connection.setDoOutput(true);
    connection.writeRequestContent(content);
    final int responseCode = connection.readResponseCode();
    if (responseCode == SC_HTTP_UNAUTHORIZED) {
        throw new DolphinRuntimeException("Invalid login!");
    }
    final String input = connection.readUTFResponseContent();
    final Gson gson = PlatformClient.getService(Gson.class);
    final KeycloakOpenidConnectResult result = gson.fromJson(input, KeycloakOpenidConnectResult.class);
    return result;
}
Also used : DolphinRuntimeException(com.canoo.platform.core.DolphinRuntimeException) Gson(com.google.gson.Gson)

Example 12 with DolphinRuntimeException

use of com.canoo.platform.core.DolphinRuntimeException in project dolphin-platform by canoo.

the class AbstractBeanBuilder method createInstanceForClass.

private <T> T createInstanceForClass(final ClassInfo classInfo, final Class<T> beanClass, final PresentationModel model, final UpdateSource source) {
    Assert.requireNonNull(beanClass, "beanClass");
    try {
        final T bean = beanClass.newInstance();
        setupProperties(classInfo, bean, model);
        setupObservableLists(classInfo, bean, model);
        beanRepository.registerBean(bean, model, source);
        return bean;
    } catch (Exception e) {
        throw new DolphinRuntimeException("Cannot create bean of type " + beanClass, e);
    }
}
Also used : DolphinRuntimeException(com.canoo.platform.core.DolphinRuntimeException) DolphinRuntimeException(com.canoo.platform.core.DolphinRuntimeException)

Example 13 with DolphinRuntimeException

use of com.canoo.platform.core.DolphinRuntimeException in project dolphin-platform by canoo.

the class AbstractBeanBuilder method buildPresentationModel.

private PresentationModel buildPresentationModel(final ClassInfo classInfo) {
    try {
        Assert.requireNonNull(classInfo, "classInfo");
        final PresentationModelBuilder builder = builderFactory.createBuilder().withType(classInfo.getModelType());
        classInfo.forEachProperty(new ClassInfo.PropertyIterator() {

            @Override
            public void call(final PropertyInfo propertyInfo) {
                Assert.requireNonNull(propertyInfo, "propertyInfo");
                builder.withAttribute(propertyInfo.getAttributeName());
            }
        });
        return builder.create();
    } catch (Exception e) {
        throw new DolphinRuntimeException("Cannot create presentation model for type " + classInfo.getBeanClass(), e);
    }
}
Also used : DolphinRuntimeException(com.canoo.platform.core.DolphinRuntimeException) PropertyInfo(com.canoo.dp.impl.remoting.info.PropertyInfo) DolphinRuntimeException(com.canoo.platform.core.DolphinRuntimeException) ClassInfo(com.canoo.dp.impl.remoting.info.ClassInfo)

Example 14 with DolphinRuntimeException

use of com.canoo.platform.core.DolphinRuntimeException in project dolphin-platform by canoo.

the class KeycloakTokenServlet method doPost.

@Override
protected void doPost(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
    try {
        LOG.debug("open-id endpoint called");
        final String realmName = Optional.ofNullable(req.getHeader(REALM_NAME_HEADER)).orElse(configuration.getRealmName());
        final String appName = Optional.ofNullable(req.getHeader(APPLICATION_NAME_HEADER)).orElse(configuration.getApplicationName());
        final String authEndPoint = configuration.getAuthEndpoint();
        final String content = ConnectionUtils.readUTF8Content(req.getInputStream()) + "&client_id=" + appName;
        LOG.debug("Calling Keycloak");
        final URI url = new URI(authEndPoint + "/realms/" + realmName + "/protocol/openid-connect/token");
        final HttpClientConnection clientConnection = new HttpClientConnection(url, RequestMethod.POST);
        clientConnection.addRequestHeader(CONTENT_TYPE_HEADER, FORM_MIME_TYPE);
        clientConnection.addRequestHeader(CHARSET_HEADER, CHARSET);
        clientConnection.writeRequestContent(content);
        final int responseCode = clientConnection.readResponseCode();
        if (responseCode == SC_HTTP_UNAUTHORIZED) {
            LOG.debug("Invalid login!");
            throw new DolphinRuntimeException("Invalid login!");
        }
        LOG.debug("sending auth token to client");
        final byte[] responseContent = clientConnection.readResponseContent();
        ConnectionUtils.writeContent(resp.getOutputStream(), responseContent);
    } catch (final Exception e) {
        LOG.error("Error in security token handling", e);
        resp.sendError(SC_HTTP_UNAUTHORIZED, "Can not authorize");
    }
}
Also used : HttpClientConnection(com.canoo.dp.impl.platform.core.http.HttpClientConnection) DolphinRuntimeException(com.canoo.platform.core.DolphinRuntimeException) URI(java.net.URI) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) DolphinRuntimeException(com.canoo.platform.core.DolphinRuntimeException)

Example 15 with DolphinRuntimeException

use of com.canoo.platform.core.DolphinRuntimeException in project dolphin-platform by canoo.

the class PlatformBootstrap method init.

public void init(final ServletContext servletContext, final ServerConfiguration configuration) {
    Assert.requireNonNull(servletContext, "servletContext");
    Assert.requireNonNull(configuration, "configuration");
    ContextManagerImpl.getInstance().addGlobalContext(APPLICATION_CONTEXT, configuration.getProperty(APPLICATION_NAME_PROPERTY));
    if (configuration.getBooleanProperty(PLATFORM_ACTIVE)) {
        PlatformLogo.printLogo();
        try {
            LOG.info("Will boot Dolphin Plaform now");
            servletContext.setAttribute(CONFIGURATION_ATTRIBUTE_NAME, configuration);
            configuration.log();
            MBeanRegistry.getInstance().setMbeanSupport(configuration.getBooleanProperty(MBEAN_REGISTRATION));
            // TODO: We need to provide a container specific thread factory that contains managed threads
            // See https://github.com/canoo/dolphin-platform/issues/498
            final PlatformThreadFactory threadFactory = new SimpleDolphinPlatformThreadFactory();
            final ManagedBeanFactory beanFactory = getBeanFactory(servletContext);
            final DefaultClasspathScanner classpathScanner = new DefaultClasspathScanner(configuration.getListProperty(ROOT_PACKAGE_FOR_CLASSPATH_SCAN));
            serverCoreComponents = new ServerCoreComponentsImpl(servletContext, configuration, threadFactory, classpathScanner, beanFactory);
            final Set<Class<?>> moduleClasses = classpathScanner.getTypesAnnotatedWith(ModuleDefinition.class);
            final Map<String, ServerModule> modules = new HashMap<>();
            for (final Class<?> moduleClass : moduleClasses) {
                if (!ServerModule.class.isAssignableFrom(moduleClass)) {
                    throw new DolphinRuntimeException("Class " + moduleClass + " is annoated with " + ModuleDefinition.class.getSimpleName() + " but do not implement " + ServerModule.class.getSimpleName());
                }
                ModuleDefinition moduleDefinition = moduleClass.getAnnotation(ModuleDefinition.class);
                ServerModule instance = (ServerModule) moduleClass.newInstance();
                modules.put(instance.getName(), instance);
            }
            LOG.info("Found {} Dolphin Plaform modules", modules.size());
            if (LOG.isTraceEnabled()) {
                for (final String moduleName : modules.keySet()) {
                    LOG.trace("Found Dolphin Plaform module {}", moduleName);
                }
            }
            for (final Map.Entry<String, ServerModule> moduleEntry : modules.entrySet()) {
                LOG.debug("Will initialize Dolphin Plaform module {}", moduleEntry.getKey());
                final ServerModule module = moduleEntry.getValue();
                if (module.shouldBoot(serverCoreComponents.getConfiguration())) {
                    final List<String> neededModules = module.getModuleDependencies();
                    for (final String neededModule : neededModules) {
                        if (!modules.containsKey(neededModule)) {
                            throw new ModuleInitializationException("Module " + moduleEntry.getKey() + " depends on missing module " + neededModule);
                        }
                    }
                    module.initialize(serverCoreComponents);
                }
            }
            LOG.info("Dolphin Plaform booted");
        } catch (Exception e) {
            throw new RuntimeException("Can not boot Dolphin Platform", e);
        }
    } else {
        LOG.info("Dolphin Plaform is deactivated");
    }
}
Also used : HashMap(java.util.HashMap) DefaultClasspathScanner(com.canoo.dp.impl.server.scanner.DefaultClasspathScanner) SimpleDolphinPlatformThreadFactory(com.canoo.dp.impl.platform.core.SimpleDolphinPlatformThreadFactory) ModuleInitializationException(com.canoo.platform.server.spi.ModuleInitializationException) DolphinRuntimeException(com.canoo.platform.core.DolphinRuntimeException) ModuleInitializationException(com.canoo.platform.server.spi.ModuleInitializationException) ServerModule(com.canoo.platform.server.spi.ServerModule) ModuleDefinition(com.canoo.platform.server.spi.ModuleDefinition) DolphinRuntimeException(com.canoo.platform.core.DolphinRuntimeException) ManagedBeanFactory(com.canoo.platform.server.spi.components.ManagedBeanFactory) DolphinRuntimeException(com.canoo.platform.core.DolphinRuntimeException) PlatformThreadFactory(com.canoo.platform.core.PlatformThreadFactory) SimpleDolphinPlatformThreadFactory(com.canoo.dp.impl.platform.core.SimpleDolphinPlatformThreadFactory) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

DolphinRuntimeException (com.canoo.platform.core.DolphinRuntimeException)19 IOException (java.io.IOException)5 URISyntaxException (java.net.URISyntaxException)4 HttpClientConnection (com.canoo.dp.impl.platform.core.http.HttpClientConnection)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 Method (java.lang.reflect.Method)2 MalformedURLException (java.net.MalformedURLException)2 URI (java.net.URI)2 ServletException (javax.servlet.ServletException)2 SimpleDolphinPlatformThreadFactory (com.canoo.dp.impl.platform.core.SimpleDolphinPlatformThreadFactory)1 ACCEPT_CHARSET_HEADER (com.canoo.dp.impl.platform.core.http.HttpHeaderConstants.ACCEPT_CHARSET_HEADER)1 ACCEPT_HEADER (com.canoo.dp.impl.platform.core.http.HttpHeaderConstants.ACCEPT_HEADER)1 ClassInfo (com.canoo.dp.impl.remoting.info.ClassInfo)1 PropertyInfo (com.canoo.dp.impl.remoting.info.PropertyInfo)1 DefaultClasspathScanner (com.canoo.dp.impl.server.scanner.DefaultClasspathScanner)1 PlatformThreadFactory (com.canoo.platform.core.PlatformThreadFactory)1 Subscription (com.canoo.platform.core.functional.Subscription)1 ConnectionException (com.canoo.platform.core.http.ConnectionException)1 HttpException (com.canoo.platform.core.http.HttpException)1 HttpHeader (com.canoo.platform.core.http.HttpHeader)1