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