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;
}
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);
}
}
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);
}
}
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");
}
}
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");
}
}
Aggregations