use of org.wso2.carbon.apimgt.core.models.HttpResponse in project carbon-apimgt by wso2.
the class RestCallUtilImpl method getRequest.
/**
* {@inheritDoc}
*/
@Override
public HttpResponse getRequest(URI uri, MediaType acceptContentType, List<String> cookies) throws APIManagementException {
if (uri == null) {
throw new IllegalArgumentException("The URI must not be null");
}
HttpURLConnection httpConnection = null;
try {
httpConnection = (HttpURLConnection) uri.toURL().openConnection();
httpConnection.setRequestMethod(APIMgtConstants.FunctionsConstants.GET);
httpConnection.setDoOutput(true);
if (acceptContentType != null) {
httpConnection.setRequestProperty(APIMgtConstants.FunctionsConstants.ACCEPT, acceptContentType.toString());
}
if (cookies != null && !cookies.isEmpty()) {
for (String cookie : cookies) {
httpConnection.addRequestProperty(APIMgtConstants.FunctionsConstants.COOKIE, cookie.split(";", 2)[0]);
}
}
return getResponse(httpConnection);
} catch (IOException e) {
throw new APIManagementException("Connection not established properly ", e);
} finally {
if (httpConnection != null) {
httpConnection.disconnect();
}
}
}
use of org.wso2.carbon.apimgt.core.models.HttpResponse in project carbon-apimgt by wso2.
the class RestCallUtilImpl method loginRequest.
/**
* {@inheritDoc}
*/
@Override
public HttpResponse loginRequest(URI uri, String username, String password, MediaType acceptContentType) throws APIManagementException {
if (uri == null) {
throw new IllegalArgumentException("The URI must not be null");
}
if (username == null) {
throw new IllegalArgumentException("Username must not be null");
}
if (password == null) {
throw new IllegalArgumentException("Password must not be null");
}
HttpURLConnection httpConnection = null;
try {
httpConnection = (HttpURLConnection) uri.toURL().openConnection();
httpConnection.setRequestMethod(APIMgtConstants.FunctionsConstants.POST);
httpConnection.setRequestProperty(APIMgtConstants.FunctionsConstants.CONTENT_TYPE, MediaType.APPLICATION_JSON);
httpConnection.setDoOutput(true);
if (acceptContentType != null) {
httpConnection.setRequestProperty(APIMgtConstants.FunctionsConstants.ACCEPT, acceptContentType.toString());
}
JSONObject loginInfoJsonObj = new JSONObject();
loginInfoJsonObj.put(APIMgtConstants.FunctionsConstants.USERNAME, username);
loginInfoJsonObj.put(APIMgtConstants.FunctionsConstants.PASSWORD, password);
OutputStream outputStream = httpConnection.getOutputStream();
outputStream.write(loginInfoJsonObj.toString().getBytes(StandardCharsets.UTF_8));
outputStream.flush();
outputStream.close();
return getResponse(httpConnection);
} catch (IOException e) {
throw new APIManagementException("Connection not established properly ", e);
} finally {
if (httpConnection != null) {
httpConnection.disconnect();
}
}
}
use of org.wso2.carbon.apimgt.core.models.HttpResponse in project carbon-apimgt by wso2.
the class RestCallUtilImpl method deleteRequest.
/**
* {@inheritDoc}
*/
@Override
public HttpResponse deleteRequest(URI uri, MediaType acceptContentType, List<String> cookies) throws APIManagementException {
if (uri == null) {
throw new IllegalArgumentException("The URI must not be null");
}
HttpURLConnection httpConnection = null;
try {
httpConnection = (HttpURLConnection) uri.toURL().openConnection();
httpConnection.setRequestMethod(APIMgtConstants.FunctionsConstants.DELETE);
httpConnection.setDoOutput(true);
if (acceptContentType != null) {
httpConnection.setRequestProperty(APIMgtConstants.FunctionsConstants.ACCEPT, acceptContentType.toString());
}
if (cookies != null && !cookies.isEmpty()) {
for (String cookie : cookies) {
httpConnection.addRequestProperty(APIMgtConstants.FunctionsConstants.COOKIE, cookie.split(";", 2)[0]);
}
}
return getResponse(httpConnection);
} catch (IOException e) {
throw new APIManagementException("Connection not established properly ", e);
} finally {
if (httpConnection != null) {
httpConnection.disconnect();
}
}
}
use of org.wso2.carbon.apimgt.core.models.HttpResponse in project carbon-apimgt by wso2.
the class FunctionTriggerTestCase method testCaptureEvent.
@Test(description = "Test method for capturing event")
public void testCaptureEvent() throws APIManagementException, URISyntaxException {
FunctionDAO functionDAO = Mockito.mock(FunctionDAO.class);
RestCallUtil restCallUtil = Mockito.mock(RestCallUtil.class);
FunctionTrigger functionTrigger = new FunctionTrigger(functionDAO, restCallUtil);
URI testUri = new URI("http://testEndpointUri");
List<Function> functions = new ArrayList<>();
Function function = new Function(FUNCTION_NAME, testUri);
functions.add(function);
HttpResponse response = new HttpResponse();
response.setResponseCode(200);
Event event = Event.API_CREATION;
ZonedDateTime eventTime = ZonedDateTime.now();
Mockito.when(functionDAO.getUserFunctionsForEvent(USER_NAME, event)).thenReturn(functions);
Mockito.when(restCallUtil.postRequest(Mockito.eq(function.getEndpointURI()), Mockito.eq(null), Mockito.eq(null), Mockito.any(), Mockito.eq(MediaType.APPLICATION_JSON_TYPE), Mockito.eq(Collections.emptyMap()))).thenReturn(response);
functionTrigger.captureEvent(event, USER_NAME, eventTime, new HashMap<>());
// When the event parameter is null
try {
functionTrigger.captureEvent(null, USER_NAME, eventTime, new HashMap<>());
} catch (IllegalArgumentException e) {
Assert.assertEquals(e.getMessage(), "Event must not be null");
}
// When the username parameter is null
try {
functionTrigger.captureEvent(event, null, eventTime, new HashMap<>());
} catch (IllegalArgumentException e) {
Assert.assertEquals(e.getMessage(), "Username must not be null");
}
// When the eventTime parameter is null
try {
functionTrigger.captureEvent(event, USER_NAME, null, new HashMap<>());
} catch (IllegalArgumentException e) {
Assert.assertEquals(e.getMessage(), "Event_time must not be null");
}
// When the metadata parameter is null
try {
functionTrigger.captureEvent(event, USER_NAME, eventTime, null);
} catch (IllegalArgumentException e) {
Assert.assertEquals(e.getMessage(), "Payload must not be null");
}
}
Aggregations