// ------------------------------------------------------------------------- @SuppressWarnings({"rawtypes", "unchecked"}) @Test public void testSearchBonds() { BondSecurity target = new GovernmentBondSecurity( "US TREASURY N/B", "Government", "US", "Treasury", Currency.USD, YieldConventionFactory.INSTANCE.getYieldConvention("US Treasury equivalent"), new Expiry(zdt(2011, 2, 1, 12, 0, 0, 0, ZoneOffset.UTC)), "", 200, SimpleFrequencyFactory.INSTANCE.getFrequency(SimpleFrequency.SEMI_ANNUAL_NAME), DayCountFactory.INSTANCE.getDayCount("Actual/Actual"), zdt(2011, 2, 1, 12, 0, 0, 0, ZoneOffset.UTC), zdt(2011, 2, 1, 12, 0, 0, 0, ZoneOffset.UTC), zdt(2011, 2, 1, 12, 0, 0, 0, ZoneOffset.UTC), 100d, 100000000, 5000, 1000, 100, 100); target.setExternalIdBundle(ExternalIdBundle.of(ExternalId.of("A", "B"))); Collection targetColl = ImmutableList.<Security>of(target); when(_underlying.getBondsWithIssuerName(eq("US TREASURY N/B"))).thenReturn(targetColl); Response test = _resource.searchBonds("US TREASURY N/B"); assertEquals(Status.OK.getStatusCode(), test.getStatus()); assertEquals(FudgeListWrapper.of(targetColl), test.getEntity()); }
private boolean updateIssueInJira(Issue issue) { ClientResponse postResponse = restAccess.put("/issue/" + issue.getKey(), issue); if (postResponse.getStatus() >= Status.OK.getStatusCode() && postResponse.getStatus() <= Status.PARTIAL_CONTENT.getStatusCode()) { return true; } else { System.err.println("Problems while updating issue: " + postResponse.getEntity(String.class)); return false; } }
// ------------------------------------------------------------------------- @Test public void testGetConfigByUid() { final SimpleExchange target = new SimpleExchange(); target.setName("Test"); when(_underlying.getConfig(eq(SimpleExchange.class), eq(UID))).thenReturn(target); Response test = _resource.get(OID.toString(), SimpleExchange.class.getName(), UID.getVersion(), "", ""); assertEquals(Status.OK.getStatusCode(), test.getStatus()); assertSame(target, test.getEntity()); }
@Override public void initialize(Settings settings, StorageEngine store) throws Exception { this.settings = settings; this.store = store; restAccess = new JiraRestAccess( settings.getJiraUrl(), settings.getJiraUser(), settings.getJiraPassword()); ClientResponse response = restAccess.get("/myself"); if (response.getStatus() != Status.OK.getStatusCode()) { throw new RuntimeException("Unable to connect to jira repository: " + response.toString()); } this.projectOptional = getProject(); }
@Test public void testValidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { RunJob lc = new RunJob(JobTest.createMockJob(new Date())); Job j = JobTest.createMockJob(new Date()); String json = new ObjectMapper().writeValueAsString(j); j = lc.getResponse( new ClientResponse( Status.OK.getStatusCode(), new InBoundHeaders(), new ByteArrayInputStream(json.getBytes("UTF-8")), Client.create().getMessageBodyWorkers())); assertNotNull(j); }
public void updateVM(String service_id, ServiceComponent sc, int index) throws ServiceInstantiationException { WebResource r = client.resource(base_url + "/vms/" + service_id + "/" + ID_PREFIX + index); try { ClientResponse res = r.type(MediaType.APPLICATION_JSON_TYPE) .put(ClientResponse.class, renderCompute(service_id, sc, index)); if (res.getStatus() != Status.OK.getStatusCode()) { throw new ServiceInstantiationException( "There was a problem while processing the request: " + res.getStatus(), new Exception()); } } catch (JSONEncodingException e) { throw new ServiceInstantiationException( "There was a problem when transforming the service component to JSON format", e); } }
@SuppressWarnings({"rawtypes", "unchecked"}) @Test public void testSearch() { final SimpleExchange target = new SimpleExchange(); target.setName("Test"); Collection targetColl = ImmutableList.of(target); when(_underlying.getConfigs(eq(SimpleExchange.class), eq(NAME), eq(VC))).thenReturn(targetColl); Response test = _resource.search( SimpleExchange.class.getName(), VC.getVersionAsOfString(), VC.getCorrectedToString(), NAME); assertEquals(Status.OK.getStatusCode(), test.getStatus()); assertEquals(FudgeListWrapper.of(targetColl), test.getEntity()); }
@Test public void testInvalidResponseHandling() throws JsonProcessingException, UnsupportedEncodingException { RunJob lc = new RunJob(JobTest.createMockJob(new Date())); Job j = JobTest.createMockJob(new Date()); String json = new ObjectMapper().writeValueAsString(j).replace("7", "seven"); try { j = lc.getResponse( new ClientResponse( Status.OK.getStatusCode(), new InBoundHeaders(), new ByteArrayInputStream(json.getBytes("UTF-8")), Client.create().getMessageBodyWorkers())); fail(); } catch (XplentyAPIException e) { assertEquals( Xplenty.Resource.RunJob.name + ": error parsing response object", e.getMessage()); } }
@Test public void auditServiceTestXML() throws WebApplicationException, IOException, JAXBException { deleteIfExists(XmlTestOutputFile); DummyAuditLogRetriever dbAuditLogRetriever = new DummyAuditLogRetriever(); AuditService auditResource = new AuditService(); auditResource.setAuditLogRetriever(dbAuditLogRetriever); DummyHttpHeaders header = new DummyHttpHeaders(MediaType.APPLICATION_XML_TYPE); Response r = auditResource.getAuditLogs("2012-08-08T00:00", "en_US", header); Assert.assertNotNull(r); Assert.assertEquals(Status.OK.getStatusCode(), r.getStatus()); Assert.assertTrue(r.getEntity() instanceof StreamingOutput); StreamingOutput so = (StreamingOutput) r.getEntity(); File of = new File(XmlTestOutputFile); OutputStream os = new FileOutputStream(of); so.write(os); os.close(); JAXBContext context = null; Unmarshaller unmarshaller = null; context = JAXBContext.newInstance(AuditLogs.class); unmarshaller = context.createUnmarshaller(); Object o = unmarshaller.unmarshal(new File(XmlTestOutputFile)); Assert.assertTrue(o instanceof AuditLogs); AuditLogs auditLogs = (AuditLogs) o; // expected number of auditLogs unmarshaled Assert.assertEquals(100, auditLogs.auditLogs.size()); deleteIfExists(XmlTestOutputFile); }
@Test public void auditServiceTestJSON() throws WebApplicationException, IOException, JsonParseException { deleteIfExists(JsonTestOutputFile); DummyAuditLogRetriever dbAuditLogRetriever = new DummyAuditLogRetriever(); AuditService auditResource = new AuditService(); auditResource.setAuditLogRetriever(dbAuditLogRetriever); DummyHttpHeaders header = new DummyHttpHeaders(MediaType.APPLICATION_JSON_TYPE); Response r = auditResource.getAuditLogs("2012-08-08T00", "en_US", header); Assert.assertNotNull(r); Assert.assertEquals(Status.OK.getStatusCode(), r.getStatus()); Assert.assertTrue(r.getEntity() instanceof StreamingOutput); StreamingOutput so = (StreamingOutput) r.getEntity(); File of = new File(JsonTestOutputFile); OutputStream os = new FileOutputStream(of); so.write(os); os.close(); ObjectMapper mapper = null; mapper = new ObjectMapper(); AnnotationIntrospector introspector = new JaxbAnnotationIntrospector(); mapper.getDeserializationConfig().withAnnotationIntrospector(introspector); AuditLogs auditLogs = mapper.readValue(new File(JsonTestOutputFile), AuditLogs.class); Assert.assertEquals(100, auditLogs.auditLogs.size()); deleteIfExists(JsonTestOutputFile); }
private DestinationAccepts getDestinationAcceptance( final HttpClient client, final String uri, final ProcessorLog logger, final String transactionId) throws IOException { final HttpHead head = new HttpHead(uri); head.addHeader(TRANSACTION_ID_HEADER, transactionId); final HttpResponse response = client.execute(head); final int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == Status.METHOD_NOT_ALLOWED.getStatusCode()) { // we assume that the destination can support FlowFile v1 always. return new DestinationAccepts(false, false, true, false, null); } else if (statusCode == Status.OK.getStatusCode()) { boolean acceptsFlowFileV3 = false; boolean acceptsFlowFileV2 = false; boolean acceptsFlowFileV1 = true; boolean acceptsGzip = false; Integer protocolVersion = null; Header[] headers = response.getHeaders(ACCEPT); if (headers != null) { for (final Header header : headers) { for (final String accepted : header.getValue().split(",")) { final String trimmed = accepted.trim(); if (trimmed.equals(APPLICATION_FLOW_FILE_V3)) { acceptsFlowFileV3 = true; } else if (trimmed.equals(APPLICATION_FLOW_FILE_V2)) { acceptsFlowFileV2 = true; } else { // we assume that the destination accepts FlowFile V1 because legacy versions // of NiFi that accepted V1 did not use an Accept header to indicate it... or // any other header. So the bets thing we can do is just assume that V1 is // accepted, if we're going to send as FlowFile. acceptsFlowFileV1 = true; } } } } final Header destinationVersion = response.getFirstHeader(PROTOCOL_VERSION_HEADER); if (destinationVersion != null) { try { protocolVersion = Integer.valueOf(destinationVersion.getValue()); } catch (final NumberFormatException e) { // nothing to do here really.... it's an invalid value, so treat the same as if not // specified } } if (acceptsFlowFileV3) { logger.debug( "Connection to URI " + uri + " will be using Content Type " + APPLICATION_FLOW_FILE_V3 + " if sending data as FlowFile"); } else if (acceptsFlowFileV2) { logger.debug( "Connection to URI " + uri + " will be using Content Type " + APPLICATION_FLOW_FILE_V2 + " if sending data as FlowFile"); } else if (acceptsFlowFileV1) { logger.debug( "Connection to URI " + uri + " will be using Content Type " + APPLICATION_FLOW_FILE_V1 + " if sending data as FlowFile"); } headers = response.getHeaders(ACCEPT_ENCODING); if (headers != null) { for (final Header header : headers) { for (final String accepted : header.getValue().split(",")) { if (accepted.equalsIgnoreCase("gzip")) { acceptsGzip = true; } } } } if (acceptsGzip) { logger.debug( "Connection to URI " + uri + " indicates that inline GZIP compression is supported"); } else { logger.debug( "Connection to URI " + uri + " indicates that it does NOT support inline GZIP compression"); } return new DestinationAccepts( acceptsFlowFileV3, acceptsFlowFileV2, acceptsFlowFileV1, acceptsGzip, protocolVersion); } else { logger.warn( "Unable to communicate with destination; when attempting to perform an HTTP HEAD, got unexpected response code of " + statusCode + ": " + response.getStatusLine().getReasonPhrase()); return new DestinationAccepts(false, false, false, false, null); } }
@Override public DownloadableContent getContent(final ContentRequestContext request) { // if clustered, send request to cluster manager if (properties.isClustered() && clusterCoordinator != null && clusterCoordinator.isConnected()) { // get the URI URI dataUri; try { dataUri = new URI(request.getDataUri()); } catch (final URISyntaxException use) { throw new ClusterRequestException(use); } // set the request parameters final MultivaluedMap<String, String> parameters = new MultivaluedMapImpl(); parameters.add(CLIENT_ID_PARAM, request.getClientId()); // set the headers final Map<String, String> headers = new HashMap<>(); if (StringUtils.isNotBlank(request.getProxiedEntitiesChain())) { headers.put("X-ProxiedEntitiesChain", request.getProxiedEntitiesChain()); } // add the user's authorities (if any) to the headers final Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); if (authentication != null) { final Object userDetailsObj = authentication.getPrincipal(); if (userDetailsObj instanceof NiFiUserDetails) { // serialize user details object final String hexEncodedUserDetails = WebUtils.serializeObjectToHex((Serializable) userDetailsObj); // put serialized user details in header headers.put("X-ProxiedEntityUserDetails", hexEncodedUserDetails); } } // ensure we were able to detect the cluster node id if (request.getClusterNodeId() == null) { throw new IllegalArgumentException("Unable to determine the which node has the content."); } // get the target node and ensure it exists final NodeIdentifier nodeId = clusterCoordinator.getNodeIdentifier(request.getClusterNodeId()); final Set<NodeIdentifier> targetNodes = Collections.singleton(nodeId); // replicate the request to the specific node NodeResponse nodeResponse; try { nodeResponse = requestReplicator .replicate(targetNodes, HttpMethod.GET, dataUri, parameters, headers) .awaitMergedResponse(); } catch (InterruptedException e) { throw new IllegalClusterStateException( "Interrupted while waiting for a response from node"); } final ClientResponse clientResponse = nodeResponse.getClientResponse(); final MultivaluedMap<String, String> responseHeaders = clientResponse.getHeaders(); // ensure an appropriate response if (Status.NOT_FOUND.getStatusCode() == clientResponse.getStatusInfo().getStatusCode()) { throw new ResourceNotFoundException(clientResponse.getEntity(String.class)); } else if (Status.FORBIDDEN.getStatusCode() == clientResponse.getStatusInfo().getStatusCode() || Status.UNAUTHORIZED.getStatusCode() == clientResponse.getStatusInfo().getStatusCode()) { throw new AccessDeniedException(clientResponse.getEntity(String.class)); } else if (Status.OK.getStatusCode() != clientResponse.getStatusInfo().getStatusCode()) { throw new IllegalStateException(clientResponse.getEntity(String.class)); } // get the file name final String contentDisposition = responseHeaders.getFirst("Content-Disposition"); final String filename = StringUtils.substringBetween(contentDisposition, "filename=\"", "\""); // get the content type final String contentType = responseHeaders.getFirst("Content-Type"); // create the downloadable content return new DownloadableContent(filename, contentType, clientResponse.getEntityInputStream()); } else { // example URIs: // http://localhost:8080/nifi-api/provenance/events/{id}/content/{input|output} // http://localhost:8080/nifi-api/flowfile-queues/{uuid}/flowfiles/{uuid}/content // get just the context path for comparison final String dataUri = StringUtils.substringAfter(request.getDataUri(), "/nifi-api"); if (StringUtils.isBlank(dataUri)) { throw new IllegalArgumentException("The specified data reference URI is not valid."); } // flowfile listing content final Matcher flowFileMatcher = FLOWFILE_CONTENT_URI_PATTERN.matcher(dataUri); if (flowFileMatcher.matches()) { final String connectionId = flowFileMatcher.group(1); final String flowfileId = flowFileMatcher.group(2); return getFlowFileContent(connectionId, flowfileId, dataUri); } // provenance event content final Matcher provenanceMatcher = PROVENANCE_CONTENT_URI_PATTERN.matcher(dataUri); if (provenanceMatcher.matches()) { try { final Long eventId = Long.parseLong(provenanceMatcher.group(1)); final ContentDirection direction = ContentDirection.valueOf(provenanceMatcher.group(2).toUpperCase()); return getProvenanceEventContent(eventId, dataUri, direction); } catch (final IllegalArgumentException iae) { throw new IllegalArgumentException("The specified data reference URI is not valid."); } } // invalid uri throw new IllegalArgumentException("The specified data reference URI is not valid."); } }