use of org.graylog.shaded.elasticsearch7.org.apache.http.message.BasicHttpResponse in project JSCover by tntim96.
the class PersistentStaticHttpServer method sendOkContent.
protected void sendOkContent(HttpServerConnection conn) throws IOException, HttpException {
// send a 200 OK with the static content
BasicHttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK");
BasicHttpEntity entity = new BasicHttpEntity();
byte[] message = content.getBytes(Charset.forName("UTF-8"));
entity.setContent(new ByteArrayInputStream(message));
entity.setContentLength(message.length);
response.setEntity(entity);
// force Content-Length header so the client doesn't expect us to close the connection to end the response
response.addHeader("Content-Length", String.valueOf(message.length));
conn.sendResponseHeader(response);
conn.sendResponseEntity(response);
conn.flush();
logger.log(FINE, "Sent 200 OK");
}
use of org.graylog.shaded.elasticsearch7.org.apache.http.message.BasicHttpResponse in project opencast by opencast.
the class HttpNotificationWorkflowOperationHandlerTest method testNotificationFailedAfterOneTry.
@Test
public void testNotificationFailedAfterOneTry() throws Exception {
client = EasyMock.createNiceMock(TrustedHttpClient.class);
HttpResponse response = new BasicHttpResponse(new BasicStatusLine(new ProtocolVersion("HTTP", 1, 1), HttpStatus.SC_NOT_FOUND, ""));
EasyMock.expect(client.execute((HttpPut) EasyMock.anyObject(), EasyMock.anyInt(), EasyMock.anyInt())).andReturn(response);
EasyMock.replay(client);
// set up service
operationHandler = new HttpNotificationWorkflowOperationHandler();
// operation configuration
Map<String, String> configurations = new HashMap<String, String>();
configurations.put(HttpNotificationWorkflowOperationHandler.OPT_URL_PATH, "http://127.0.0.1:9");
configurations.put(HttpNotificationWorkflowOperationHandler.OPT_NOTIFICATION_SUBJECT, "test");
configurations.put(HttpNotificationWorkflowOperationHandler.OPT_MAX_RETRY, "0");
configurations.put(HttpNotificationWorkflowOperationHandler.OPT_TIMEOUT, Integer.toString(10));
// run the operation handler
try {
getWorkflowOperationResult(mp, configurations);
Assert.fail("Operation handler should have thrown an exception!");
} catch (WorkflowOperationException e) {
Assert.assertTrue("Exception thrown as expected by the operation handler", true);
}
}
use of org.graylog.shaded.elasticsearch7.org.apache.http.message.BasicHttpResponse in project opencast by opencast.
the class ServiceRegistryJpaImplTest method setUpServiceRegistryJpaImpl.
public void setUpServiceRegistryJpaImpl() throws PropertyVetoException, NotFoundException, TrustedHttpClientException {
serviceRegistryJpaImpl = new ServiceRegistryJpaImpl();
serviceRegistryJpaImpl.setEntityManagerFactory(emf);
Organization organization = new DefaultOrganization();
OrganizationDirectoryService organizationDirectoryService = EasyMock.createMock(OrganizationDirectoryService.class);
EasyMock.expect(organizationDirectoryService.getOrganization((String) EasyMock.anyObject())).andReturn(organization).anyTimes();
EasyMock.replay(organizationDirectoryService);
serviceRegistryJpaImpl.setOrganizationDirectoryService(organizationDirectoryService);
JaxbOrganization jaxbOrganization = JaxbOrganization.fromOrganization(organization);
User anonymous = new JaxbUser("anonymous", "test", jaxbOrganization, new JaxbRole(jaxbOrganization.getAnonymousRole(), jaxbOrganization));
SecurityService securityService = EasyMock.createNiceMock(SecurityService.class);
EasyMock.expect(securityService.getUser()).andReturn(anonymous).anyTimes();
EasyMock.expect(securityService.getOrganization()).andReturn(organization).anyTimes();
EasyMock.replay(securityService);
serviceRegistryJpaImpl.setSecurityService(securityService);
UserDirectoryService userDirectoryService = EasyMock.createNiceMock(UserDirectoryService.class);
EasyMock.expect(userDirectoryService.loadUser(EasyMock.anyString())).andReturn(anonymous).anyTimes();
EasyMock.replay(userDirectoryService);
serviceRegistryJpaImpl.setUserDirectoryService(userDirectoryService);
final Capture<HttpUriRequest> request = EasyMock.newCapture();
final BasicHttpResponse successRespone = new BasicHttpResponse(new BasicStatusLine(new HttpVersion(1, 1), HttpStatus.SC_NO_CONTENT, "No message"));
final BasicHttpResponse unavailableResponse = new BasicHttpResponse(new BasicStatusLine(new HttpVersion(1, 1), HttpStatus.SC_SERVICE_UNAVAILABLE, "No message"));
TrustedHttpClient trustedHttpClient = EasyMock.createNiceMock(TrustedHttpClient.class);
EasyMock.expect(trustedHttpClient.execute(EasyMock.capture(request))).andAnswer(new IAnswer<HttpResponse>() {
@Override
public HttpResponse answer() throws Throwable {
if (!request.hasCaptured())
return unavailableResponse;
if (request.getValue().getURI().toString().contains(TEST_PATH))
return unavailableResponse;
if (request.getValue().getURI().toString().contains(TEST_PATH_3))
return unavailableResponse;
return successRespone;
}
}).anyTimes();
EasyMock.replay(trustedHttpClient);
serviceRegistryJpaImpl.setTrustedHttpClient(trustedHttpClient);
}
use of org.graylog.shaded.elasticsearch7.org.apache.http.message.BasicHttpResponse in project opencast by opencast.
the class TrustedHttpClientImplTest method testAlreadySignedUrlIgnoredByUrlSigningService.
@Test
public void testAlreadySignedUrlIgnoredByUrlSigningService() throws IOException, UrlSigningException {
String acceptsUrl = "http://alreadysigned.com?signature=thesig&policy=thepolicy&keyId=thekey";
HttpHead headRequest = new HttpHead(acceptsUrl);
// Setup signing service
UrlSigningService urlSigningService = EasyMock.createMock(UrlSigningService.class);
EasyMock.expect(urlSigningService.accepts(acceptsUrl)).andReturn(true);
EasyMock.replay(urlSigningService);
CredentialsProvider cp = EasyMock.createNiceMock(CredentialsProvider.class);
Capture<HttpUriRequest> request = EasyMock.newCapture();
// Setup Http Client
HttpClient httpClient = createMock("Request", HttpClient.class);
HttpParams httpParams = createNiceMock(HttpParams.class);
expect(httpParams.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 60000)).andReturn(httpParams);
expect(httpParams.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 60000)).andReturn(httpParams);
replay(httpParams);
expect(httpClient.getParams()).andReturn(httpParams).anyTimes();
expect(httpClient.getCredentialsProvider()).andReturn(cp);
expect(httpClient.execute(EasyMock.capture(request))).andReturn(new BasicHttpResponse(new BasicStatusLine(HttpVersion.HTTP_1_1, 200, "ok")));
EasyMock.replay(httpClient);
// Setup DefaultHttpClientFactory
HttpClientFactory httpClientFactory = createMock(HttpClientFactory.class);
expect(httpClientFactory.makeHttpClient()).andReturn(httpClient).atLeastOnce();
replay(httpClientFactory);
client = new TrustedHttpClientImpl("user", "pass");
client.setHttpClientFactory(httpClientFactory);
client.setSecurityService(securityService);
client.setUrlSigningService(urlSigningService);
client.execute(headRequest);
assertTrue(request.hasCaptured());
assertEquals(acceptsUrl, request.getValue().getURI().toString());
}
use of org.graylog.shaded.elasticsearch7.org.apache.http.message.BasicHttpResponse in project opencast by opencast.
the class TrustedHttpClientImplTest method testAcceptsUrlSigningService.
@Test
public void testAcceptsUrlSigningService() throws IOException, UrlSigningException {
String acceptsUrl = "http://accepts.com";
String signedUrl = "http://accepts.com?policy=testPolicy&signature=testSignature&keyId=testKeyId";
HttpGet get = new HttpGet(acceptsUrl);
// Setup signing service
UrlSigningService urlSigningService = EasyMock.createMock(UrlSigningService.class);
EasyMock.expect(urlSigningService.accepts(acceptsUrl)).andReturn(true);
EasyMock.expect(urlSigningService.sign(EasyMock.anyString(), EasyMock.anyLong(), EasyMock.anyLong(), EasyMock.anyString())).andReturn(signedUrl);
EasyMock.replay(urlSigningService);
CredentialsProvider cp = EasyMock.createNiceMock(CredentialsProvider.class);
Capture<HttpUriRequest> request = EasyMock.newCapture();
// Setup Http Client
HttpClient httpClient = createMock("Request", HttpClient.class);
HttpParams httpParams = createNiceMock(HttpParams.class);
expect(httpParams.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 60000)).andReturn(httpParams);
expect(httpParams.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 60000)).andReturn(httpParams);
replay(httpParams);
expect(httpClient.getParams()).andReturn(httpParams).anyTimes();
expect(httpClient.getCredentialsProvider()).andReturn(cp);
expect(httpClient.execute(EasyMock.capture(request))).andReturn(new BasicHttpResponse(new BasicStatusLine(HttpVersion.HTTP_1_1, 200, "ok")));
EasyMock.replay(httpClient);
// Setup DefaultHttpClientFactory
HttpClientFactory httpClientFactory = createMock(HttpClientFactory.class);
expect(httpClientFactory.makeHttpClient()).andReturn(httpClient).atLeastOnce();
replay(httpClientFactory);
client = new TrustedHttpClientImpl("user", "pass");
client.setHttpClientFactory(httpClientFactory);
client.setSecurityService(securityService);
client.setUrlSigningService(urlSigningService);
client.execute(get);
assertTrue(request.hasCaptured());
assertEquals(signedUrl, request.getValue().getURI().toString());
}
Aggregations