Search in sources :

Example 1 with WebService

use of org.apache.hop.www.service.WebService in project hop by apache.

the class WebServiceServlet method doGet.

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    if (isJettyMode() && !request.getContextPath().startsWith(CONTEXT_PATH)) {
        return;
    }
    if (log.isDebug()) {
        logDebug(BaseMessages.getString(PKG, "WebServiceServlet.Log.WebServiceRequested"));
    }
    IVariables variables = pipelineMap.getHopServerConfig().getVariables();
    IHopMetadataProvider metadataProvider = pipelineMap.getHopServerConfig().getMetadataProvider();
    String webServiceName = request.getParameter("service");
    if (StringUtils.isEmpty(webServiceName)) {
        throw new ServletException("Please specify a service parameter pointing to the name of the web service object");
    }
    try {
        IHopMetadataSerializer<WebService> serializer = metadataProvider.getSerializer(WebService.class);
        WebService webService = serializer.load(webServiceName);
        if (webService == null) {
            throw new HopException("Unable to find web service '" + webServiceName + "'.  You can set the metadata_folder in the Hop server XML configuration");
        }
        if (!webService.isEnabled()) {
            throw new HopException("Web service '" + webServiceName + "' is disabled.");
        }
        String filename = variables.resolve(webService.getFilename());
        String transformName = variables.resolve(webService.getTransformName());
        String fieldName = variables.resolve(webService.getFieldName());
        String contentType = variables.resolve(webService.getContentType());
        if (StringUtils.isEmpty(contentType)) {
            response.setContentType("text/plain");
        } else {
            response.setContentType(contentType);
        }
        response.setCharacterEncoding(Const.XML_ENCODING);
        String serverObjectId = UUID.randomUUID().toString();
        SimpleLoggingObject servletLoggingObject = new SimpleLoggingObject(CONTEXT_PATH, LoggingObjectType.HOP_SERVER, null);
        servletLoggingObject.setContainerObjectId(serverObjectId);
        // Load and start the pipeline
        // Output the data to the response output stream...
        // 
        PipelineMeta pipelineMeta = new PipelineMeta(filename, metadataProvider, true, variables);
        LocalPipelineEngine pipeline = new LocalPipelineEngine(pipelineMeta, variables, servletLoggingObject);
        pipeline.setContainerId(serverObjectId);
        // Set all the other parameters as variables/parameters...
        // 
        String[] pipelineParameters = pipelineMeta.listParameters();
        pipeline.copyParametersFromDefinitions(pipelineMeta);
        for (String requestParameter : request.getParameterMap().keySet()) {
            if ("service".equals(requestParameter)) {
                continue;
            }
            String requestParameterValue = request.getParameter(requestParameter);
            if (Const.indexOfString(requestParameter, pipelineParameters) < 0) {
                pipeline.setVariable(requestParameter, Const.NVL(requestParameterValue, ""));
            } else {
                pipeline.setParameterValue(requestParameter, Const.NVL(requestParameterValue, ""));
            }
        }
        pipeline.activateParameters(pipeline);
        // 
        if (webService.isListingStatus()) {
            PipelineExecutionConfiguration pipelineExecutionConfiguration = new PipelineExecutionConfiguration();
            PipelineConfiguration pipelineConfiguration = new PipelineConfiguration(pipelineMeta, pipelineExecutionConfiguration, new SerializableMetadataProvider(metadataProvider));
            getPipelineMap().addPipeline(pipelineMeta.getName(), serverObjectId, pipeline, pipelineConfiguration);
        }
        // Allocate the threads...
        pipeline.prepareExecution();
        final OutputStream outputStream = response.getOutputStream();
        // Add the row listener to the transform/field...
        // TODO: add to all copies
        // 
        IEngineComponent component = pipeline.findComponent(transformName, 0);
        component.addRowListener(new RowAdapter() {

            @Override
            public void rowWrittenEvent(IRowMeta rowMeta, Object[] row) throws HopTransformException {
                try {
                    String outputString = rowMeta.getString(row, fieldName, "");
                    outputStream.write(outputString.getBytes(StandardCharsets.UTF_8));
                    outputStream.flush();
                } catch (HopValueException e) {
                    throw new HopTransformException("Error getting output field '" + fieldName + " from row: " + rowMeta.toStringMeta(), e);
                } catch (IOException e) {
                    throw new HopTransformException("Error writing output of '" + fieldName + "'", e);
                }
            }
        });
        pipeline.startThreads();
        pipeline.waitUntilFinished();
        response.setStatus(HttpServletResponse.SC_OK);
    } catch (Exception e) {
        throw new ServletException("Error producing web service output", e);
    }
}
Also used : WebService(org.apache.hop.www.service.WebService) OutputStream(java.io.OutputStream) PipelineConfiguration(org.apache.hop.pipeline.PipelineConfiguration) HopTransformException(org.apache.hop.core.exception.HopTransformException) IEngineComponent(org.apache.hop.pipeline.engine.IEngineComponent) ServletException(javax.servlet.ServletException) LocalPipelineEngine(org.apache.hop.pipeline.engines.local.LocalPipelineEngine) IVariables(org.apache.hop.core.variables.IVariables) IHopMetadataProvider(org.apache.hop.metadata.api.IHopMetadataProvider) HopException(org.apache.hop.core.exception.HopException) IRowMeta(org.apache.hop.core.row.IRowMeta) IOException(java.io.IOException) SimpleLoggingObject(org.apache.hop.core.logging.SimpleLoggingObject) ServletException(javax.servlet.ServletException) HopException(org.apache.hop.core.exception.HopException) IOException(java.io.IOException) HopTransformException(org.apache.hop.core.exception.HopTransformException) HopValueException(org.apache.hop.core.exception.HopValueException) PipelineMeta(org.apache.hop.pipeline.PipelineMeta) SerializableMetadataProvider(org.apache.hop.core.metadata.SerializableMetadataProvider) RowAdapter(org.apache.hop.pipeline.transform.RowAdapter) HopValueException(org.apache.hop.core.exception.HopValueException) SimpleLoggingObject(org.apache.hop.core.logging.SimpleLoggingObject) PipelineExecutionConfiguration(org.apache.hop.pipeline.PipelineExecutionConfiguration)

Example 2 with WebService

use of org.apache.hop.www.service.WebService in project hop by apache.

the class WebServiceEditor method setWidgetsContent.

@Override
public void setWidgetsContent() {
    WebService ws = getMetadata();
    wName.setText(Const.NVL(ws.getName(), ""));
    wEnabled.setSelection(ws.isEnabled());
    wFilename.setText(Const.NVL(ws.getFilename(), ""));
    wTransform.setText(Const.NVL(ws.getTransformName(), ""));
    wField.setText(Const.NVL(ws.getFieldName(), ""));
    wContentType.setText(Const.NVL(ws.getContentType(), ""));
    wListStatus.setSelection(ws.isListingStatus());
}
Also used : WebService(org.apache.hop.www.service.WebService)

Example 3 with WebService

use of org.apache.hop.www.service.WebService in project hop by apache.

the class WebServiceGuiPlugin method addWebServiceForTransform.

@GuiContextAction(id = "pipeline-graph-transform-9000-add-web-serviec", parentId = HopGuiPipelineTransformContext.CONTEXT_ID, type = GuiActionType.Info, name = "Add web service", tooltip = "Use the output of this transform as a web service with Hop Server", image = "ui/images/webservice.svg", category = "Data routing", categoryOrder = "2")
public void addWebServiceForTransform(HopGuiPipelineTransformContext context) {
    PipelineMeta pipelineMeta = context.getPipelineMeta();
    TransformMeta transformMeta = context.getTransformMeta();
    IVariables variables = context.getPipelineGraph().getVariables();
    HopGui hopGui = HopGui.getInstance();
    try {
        // Ask which field should be used...
        // 
        IRowMeta fields = pipelineMeta.getTransformFields(variables, transformMeta);
        EnterSelectionDialog fieldSelectionDialog = new EnterSelectionDialog(hopGui.getShell(), fields.getFieldNames(), "Select the output field", "Please select the field to output when the service is called");
        String fieldName = fieldSelectionDialog.open();
        if (fieldName == null) {
            return;
        }
        // Present the user with a list of pipeline probes...
        // 
        IHopMetadataProvider metadataProvider = hopGui.getMetadataProvider();
        IHopMetadataSerializer<WebService> serializer = metadataProvider.getSerializer(WebService.class);
        MetadataManager<WebService> manager = new MetadataManager<>(hopGui.getVariables(), metadataProvider, WebService.class);
        WebService webService = null;
        List<String> serviceNames = serializer.listObjectNames();
        if (serviceNames.isEmpty()) {
            MessageBox box = new MessageBox(hopGui.getShell(), SWT.YES | SWT.NO | SWT.ICON_QUESTION);
            box.setText("No web services available");
            box.setMessage("There are no web service objects defined yet.  Do you want to create one?");
            int answer = box.open();
            if ((answer & SWT.YES) != 0) {
                // Create a new web service...
                // 
                webService = new WebService(pipelineMeta.getName(), true, pipelineMeta.getFilename(), transformMeta.getName(), fieldName, "text/plain", false);
                manager.newMetadata(webService);
                return;
            } else {
                return;
            }
        } else {
            EnterSelectionDialog dialog = new EnterSelectionDialog(hopGui.getShell(), serviceNames.toArray(new String[0]), "Select a web service", "Select the web service to update");
            String pipelineProbeName = dialog.open();
            if (pipelineProbeName != null) {
                webService = serializer.load(pipelineProbeName);
            }
        }
        if (webService != null) {
            // See if it's open in the metadata perspective...
            // 
            MetadataPerspective perspective = (MetadataPerspective) hopGui.getPerspectiveManager().findPerspective(MetadataPerspective.class);
            String key = WebService.class.getAnnotation(HopMetadata.class).key();
            WebServiceEditor editor = (WebServiceEditor) perspective.findEditor(key, webService.getName());
            if (editor != null) {
                // We're going to change the current metadata and flag it as changed...
                // 
                webService = new WebService();
                editor.getWidgetsContent(webService);
                // Update the web service details
                // 
                webService.setFilename(pipelineMeta.getFilename());
                webService.setTransformName(transformMeta.getName());
                webService.setFieldName(fieldName);
                // Replace and refresh the dialog
                // 
                editor.setMetadata(webService);
                editor.setWidgetsContent();
                // Set changed...
                // 
                editor.setChanged();
                // Switch to the editor...
                // 
                perspective.activate();
                perspective.setActiveEditor(editor);
                return;
            } else {
                // Not opened in the perspective, simply set the web service details
                // 
                webService.setFilename(pipelineMeta.getFilename());
                webService.setTransformName(transformMeta.getName());
                webService.setFieldName(fieldName);
                // ... and save the pipeline probe
                // 
                serializer.save(webService);
            }
        }
    } catch (Exception e) {
        new ErrorDialog(hopGui.getShell(), "Error", "Error adding web service for transform '" + transformMeta.getName() + "'", e);
    }
}
Also used : WebService(org.apache.hop.www.service.WebService) IRowMeta(org.apache.hop.core.row.IRowMeta) ErrorDialog(org.apache.hop.ui.core.dialog.ErrorDialog) MetadataPerspective(org.apache.hop.ui.hopgui.perspective.metadata.MetadataPerspective) PipelineMeta(org.apache.hop.pipeline.PipelineMeta) MessageBox(org.eclipse.swt.widgets.MessageBox) MetadataManager(org.apache.hop.ui.core.metadata.MetadataManager) IVariables(org.apache.hop.core.variables.IVariables) TransformMeta(org.apache.hop.pipeline.transform.TransformMeta) IHopMetadataProvider(org.apache.hop.metadata.api.IHopMetadataProvider) HopMetadata(org.apache.hop.metadata.api.HopMetadata) EnterSelectionDialog(org.apache.hop.ui.core.dialog.EnterSelectionDialog) HopGui(org.apache.hop.ui.hopgui.HopGui) GuiContextAction(org.apache.hop.core.action.GuiContextAction)

Aggregations

WebService (org.apache.hop.www.service.WebService)3 IRowMeta (org.apache.hop.core.row.IRowMeta)2 IVariables (org.apache.hop.core.variables.IVariables)2 IHopMetadataProvider (org.apache.hop.metadata.api.IHopMetadataProvider)2 PipelineMeta (org.apache.hop.pipeline.PipelineMeta)2 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 ServletException (javax.servlet.ServletException)1 GuiContextAction (org.apache.hop.core.action.GuiContextAction)1 HopException (org.apache.hop.core.exception.HopException)1 HopTransformException (org.apache.hop.core.exception.HopTransformException)1 HopValueException (org.apache.hop.core.exception.HopValueException)1 SimpleLoggingObject (org.apache.hop.core.logging.SimpleLoggingObject)1 SerializableMetadataProvider (org.apache.hop.core.metadata.SerializableMetadataProvider)1 HopMetadata (org.apache.hop.metadata.api.HopMetadata)1 PipelineConfiguration (org.apache.hop.pipeline.PipelineConfiguration)1 PipelineExecutionConfiguration (org.apache.hop.pipeline.PipelineExecutionConfiguration)1 IEngineComponent (org.apache.hop.pipeline.engine.IEngineComponent)1 LocalPipelineEngine (org.apache.hop.pipeline.engines.local.LocalPipelineEngine)1 RowAdapter (org.apache.hop.pipeline.transform.RowAdapter)1