KarmaServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { UpdateContainer updateContainer = new UpdateContainer(); String karmaHomeDir = request.getParameter(Arguments.karmaHome.name()); ContextParametersRegistry contextParametersRegistry = ContextParametersRegistry.getInstance(); ServletContextParameterMap contextParameters = contextParametersRegistry.getContextParameters(karmaHomeDir); try { ServerStart.initContextParameters(this.getServletContext(), contextParameters); } catch(Exception e) { logger.error("Unable to initalize parameters using servlet context", e); } KarmaMetadataManager metadataManager = null; try { metadataManager = new KarmaMetadataManager(contextParameters); metadataManager.register(new UserUploadedMetadata(contextParameters), updateContainer); metadataManager.register(new UserPreferencesMetadata(contextParameters), updateContainer); metadataManager.register(new UserConfigMetadata(contextParameters), updateContainer); } catch (KarmaException e) { logger.error("Unable to complete Karma set up: ", e); } boolean hasWorkspaceCookieId = false; String hasPrefId = request.getParameter(Arguments.hasPreferenceId.name()); if(hasPrefId != null && hasPrefId.equals("true")) hasWorkspaceCookieId = true; Workspace workspace = null; VWorkspace vwsp = null; if(hasWorkspaceCookieId) { String cachedWorkspaceId = request.getParameter(Arguments.workspacePreferencesId.name()); workspace = WorkspaceManager.getInstance().createWorkspaceWithPreferencesId(cachedWorkspaceId, contextParameters.getId()); vwsp = new VWorkspace(workspace, cachedWorkspaceId); } else { workspace = WorkspaceManager.getInstance().createWorkspace(contextParameters.getId()); vwsp = new VWorkspace(workspace); } WorkspaceKarmaHomeRegistry.getInstance().register(workspace.getId(), contextParameters.getKarmaHome()); WorkspaceRegistry.getInstance().register(new ExecutionController(workspace)); VWorkspaceRegistry.getInstance().registerVWorkspace(workspace.getId(), vwsp); logger.info("Start Metadata Setup"); try { metadataManager.register(new TextualSemanticTypeModelMetadata(contextParameters), updateContainer); metadataManager.register(new NumericSemanticTypeModelMetadata(contextParameters), updateContainer); metadataManager.register(new SemanticTypeModelMetadata(contextParameters), updateContainer); metadataManager.register(new OntologyMetadata(contextParameters), updateContainer); metadataManager.register(new JSONModelsMetadata(contextParameters), updateContainer); metadataManager.register(new PythonTransformationMetadata(contextParameters), updateContainer); metadataManager.register(new GraphVizMetadata(contextParameters), updateContainer); metadataManager.register(new ModelLearnerMetadata(contextParameters), updateContainer); metadataManager.register(new R2RMLMetadata(contextParameters), updateContainer); metadataManager.register(new R2RMLPublishedMetadata(contextParameters), updateContainer); metadataManager.register(new RDFMetadata(contextParameters), updateContainer); metadataManager.register(new CSVMetadata(contextParameters), updateContainer); metadataManager.register(new JSONMetadata(contextParameters), updateContainer); metadataManager.register(new ReportMetadata(contextParameters), updateContainer); metadataManager.register(new AvroMetadata(contextParameters), updateContainer); metadataManager.register(new KMLPublishedMetadata(contextParameters), updateContainer); PythonRepository pythonRepository = new PythonRepository(true, contextParameters.getParameterValue(ContextParameter.USER_PYTHON_SCRIPTS_DIRECTORY)); PythonRepositoryRegistry.getInstance().register(pythonRepository); String port = String.valueOf(request.getServerPort()); String protocol = request.getProtocol(); if(protocol != null) { protocol = protocol.split("/")[0]; String host = protocol.toLowerCase() + ": contextParameters.setParameterValue(ServletContextParameterMap.ContextParameter.JETTY_PORT, port); logger.info("JETTY_PORT initilized to " + port); contextParameters.setParameterValue(ServletContextParameterMap.ContextParameter.JETTY_HOST, host); logger.info("JETTY_HOST initilized to " + host); contextParameters.setParameterValue(ServletContextParameterMap.ContextParameter.PUBLIC_RDF_ADDRESS, host + ":" + port + "/RDF/"); String cleaningServiceUrl = host + ":" + port + contextParameters.getParameterValue(ServletContextParameterMap.ContextParameter.CLEANING_SERVICE_URL); logger.info("CLEANING SERVICE initialized to " + cleaningServiceUrl); contextParameters.setParameterValue(ServletContextParameterMap.ContextParameter.CLEANING_SERVICE_URL, cleaningServiceUrl); String clusterServiceUrl =host + ":" + port + contextParameters.getParameterValue(ServletContextParameterMap.ContextParameter.CLUSTER_SERVICE_URL); logger.info("CLUSTER SERVICE initialized to " + clusterServiceUrl); contextParameters.setParameterValue(ServletContextParameterMap.ContextParameter.CLUSTER_SERVICE_URL, clusterServiceUrl); } } catch (KarmaException e) { logger.error("Unable to complete Karma set up: ", e); } metadataManager.setup(workspace, updateContainer); CommandHistory.setIsHistoryEnabled(true); CommandHistory.setHistorySaver(workspace.getId(), new R2RMLAlignmentFileSaver(workspace)); Tag outlierTag = new Tag(TagName.Outlier, Color.Red); workspace.getTagsContainer().addTag(outlierTag); updateContainer.add(new WorksheetListUpdate()); for (Worksheet w : vwsp.getWorkspace().getWorksheets()) { updateContainer.append(WorksheetUpdateFactory.createWorksheetHierarchicalUpdates(w.getId(), SuperSelectionManager.DEFAULT_SELECTION, workspace.getContextId())); } updateContainer.add(new AbstractUpdate() { @Override public void generateJson(String prefix, PrintWriter pw, VWorkspace vWorkspace) { UIConfiguration uiConfiguration = UIConfigurationRegistry.getInstance().getUIConfiguration(vWorkspace.getWorkspace().getContextId()); uiConfiguration.loadConfig(); ModelingConfigurationRegistry.getInstance().register(vWorkspace.getWorkspace().getContextId()); pw.println("{"); pw.println("\"updateType\": \"UISettings\", "); pw.println("\"settings\": {"); pw.println(" \"googleEarthEnabled\" : " + uiConfiguration.isGoogleEarthEnabled() + ","); pw.println(" \"maxLoadedClasses\" : " + uiConfiguration.getMaxClassesToLoad() + ","); pw.println(" \"maxLoadedProperties\" : " + uiConfiguration.getMaxPropertiesToLoad()); pw.println(" }"); pw.println("}"); } }); StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); updateContainer.applyUpdates(vwsp); updateContainer.generateJson("", pw, vwsp); response.setContentType("application/json"); response.setStatus(HttpServletResponse.SC_OK); response.getWriter().println(sw.toString()); }    }
@Test public void testDoGetWithoutCookies() throws Exception { Mockito.when(request.getParameter("hasPreferenceId")).thenReturn("false"); this.instance.doGet(request, response); Mockito.verify(response).setContentType("application/json"); Mockito.verify(response).setStatus(HttpServletResponse.SC_OK); }
@Test public void testDoGetWithCookies() throws Exception { Mockito.when(request.getParameter("hasPreferenceId")).thenReturn("true"); Mockito.when(request.getParameter("workspacePreferencesId")).thenReturn(workspaceId); this.instance.doGet(request, response); Mockito.verify(response).setContentType("application/json"); Mockito.verify(response).setStatus(HttpServletResponse.SC_OK); }