MediaPackagePostOperationHandler extends AbstractWorkflowOperationHandler { public WorkflowOperationResult start(final WorkflowInstance workflowInstance, JobContext context) throws WorkflowOperationException { WorkflowOperationInstance currentOperation = workflowInstance.getCurrentOperation(); Configuration config = new Configuration(currentOperation); MediaPackage workflowMP = workflowInstance.getMediaPackage(); MediaPackage mp = workflowMP; if (config.mpFromSearch()) { SearchQuery searchQuery = new SearchQuery(); searchQuery.withId(mp.getIdentifier().toString()); SearchResult result = searchService.getByQuery(searchQuery); if (result.size() != 1) { throw new WorkflowOperationException("Received multiple results for identifier" + "\"" + mp.getIdentifier().toString() + "\" from search service. "); } logger.info("Getting Mediapackage from Search Service"); mp = result.getItems()[0].getMediaPackage(); } logger.info("Submitting \"" + mp.getTitle() + "\" (" + mp.getIdentifier().toString() + ") as " + config.getFormat().name() + " to " + config.getUrl().toString()); try { String mpStr; if (config.getFormat() == Configuration.Format.JSON) { mpStr = MediaPackageParser.getAsJSON(mp); } else { mpStr = MediaPackageParser.getAsXml(mp); } if (config.debug()) { logger.info(mpStr); } List<NameValuePair> data = new ArrayList<NameValuePair>(); data.add(new BasicNameValuePair("mediapackage", mpStr)); data.addAll(config.getAdditionalFields()); HttpPost post = new HttpPost(config.getUrl()); post.setEntity(new UrlEncodedFormEntity(data, config.getEncoding())); DefaultHttpClient client = new DefaultHttpClient(); if (config.authenticate()) { URL targetUrl = config.getUrl().toURL(); client.getCredentialsProvider().setCredentials( new AuthScope(targetUrl.getHost(), targetUrl.getPort()), config.getCredentials()); } HttpResponse response = client.execute(post); int status = response.getStatusLine().getStatusCode(); if ((status >= 200) && (status < 300)) { if (config.debug()) { logger.info("Successfully submitted \"" + mp.getTitle() + "\" (" + mp.getIdentifier().toString() + ") to " + config.getUrl().toString() + ": " + status); } } else if (status == 418) { logger.warn("Submitted \"" + mp.getTitle() + "\" (" + mp.getIdentifier().toString() + ") to " + config.getUrl().toString() + ": The target claims to be a teapot. " + "The Reason for this is probably an insane programmer."); } else { throw new WorkflowOperationException("Faild to submit \"" + mp.getTitle() + "\" (" + mp.getIdentifier().toString() + "), " + config.getUrl().toString() + " answered with: " + Integer.toString(status)); } } catch (Exception e) { if (e instanceof WorkflowOperationException) { throw (WorkflowOperationException) e; } else { logger.error("Submitting mediapackage failed: {}", e.toString()); throw new WorkflowOperationException(e); } } return createResult(workflowMP, Action.CONTINUE); }  void setSearchService(SearchService searchService); WorkflowOperationResult start(final WorkflowInstance workflowInstance, JobContext context);  }
@Test public void testHTTPPostXML() throws Exception { MediaPackageBuilderFactory factory = MediaPackageBuilderFactory.newInstance(); MediaPackageBuilder builder = factory.newMediaPackageBuilder(); MediaPackage mp = builder.createNew(new IdImpl("xyz")); mp.setTitle("test"); mp.addContributor("lkiesow"); mp.addContributor("lkiesow"); InstanceAndHandler tuple = createWorkflow("http: tuple.workflowInstance.setMediaPackage(mp); try { tuple.workflowHandler.start(tuple.workflowInstance, null); Assert.fail(); } catch (WorkflowOperationException e) { logger.info(e.toString()); } }
@Test public void testHTTPPostJSON() throws Exception { MediaPackageBuilderFactory factory = MediaPackageBuilderFactory.newInstance(); MediaPackageBuilder builder = factory.newMediaPackageBuilder(); MediaPackage mp = builder.createNew(new IdImpl("xyz")); mp.setTitle("test"); mp.addContributor("lkiesow"); mp.addContributor("lkiesow"); InstanceAndHandler tuple = createWorkflow("http: tuple.workflowInstance.setMediaPackage(mp); try { tuple.workflowHandler.start(tuple.workflowInstance, null); Assert.fail(); } catch (WorkflowOperationException e) { logger.info(e.toString()); } }