use of org.wso2.msf4j.formparam.FileInfo in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method apisApiIdDocumentsDocumentIdContentPost.
/**
* Uploads a document's content and attach to particular document
*
* @param apiId UUID of API
* @param documentId UUID of the document
* @param fileInputStream file content stream
* @param fileDetail meta infomation about the file
* @param inlineContent inline documentation content
* @param ifMatch If-Match header value
* @param ifUnmodifiedSince If-Unmodified-Since header value
* @param request msf4j request object
* @return updated document meta information
* @throws NotFoundException When the particular resource does not exist in the system
*/
@Override
public Response apisApiIdDocumentsDocumentIdContentPost(String apiId, String documentId, InputStream fileInputStream, FileInfo fileDetail, String inlineContent, String ifMatch, String ifUnmodifiedSince, Request request) throws NotFoundException {
try {
String username = RestApiUtil.getLoggedInUsername(request);
APIPublisher apiProvider = RestAPIPublisherUtil.getApiPublisher(username);
String existingFingerprint = apisApiIdDocumentsDocumentIdContentGetFingerprint(apiId, documentId, null, null, request);
if (!StringUtils.isEmpty(ifMatch) && !StringUtils.isEmpty(existingFingerprint) && !ifMatch.contains(existingFingerprint)) {
return Response.status(Response.Status.PRECONDITION_FAILED).build();
}
if (fileInputStream != null && inlineContent != null) {
String msg = "Only one of 'file' and 'inlineContent' should be specified";
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(msg, 900314L, msg);
log.error(msg);
return Response.status(Response.Status.BAD_REQUEST).entity(errorDTO).build();
}
// retrieves the document and send 404 if not found
DocumentInfo documentation = apiProvider.getDocumentationSummary(documentId);
if (documentation == null) {
String msg = "Documentation not found " + documentId;
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(msg, 900314L, msg);
log.error(msg);
return Response.status(Response.Status.NOT_FOUND).entity(errorDTO).build();
}
// add content depending on the availability of either input stream or inline content
if (fileInputStream != null) {
if (!documentation.getSourceType().equals(DocumentInfo.SourceType.FILE)) {
String msg = "Source type of document " + documentId + " is not FILE";
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(msg, 900314L, msg);
log.error(msg);
return Response.status(Response.Status.BAD_REQUEST).entity(errorDTO).build();
}
apiProvider.uploadDocumentationFile(documentId, fileInputStream, fileDetail.getContentType());
} else if (inlineContent != null) {
if (!documentation.getSourceType().equals(DocumentInfo.SourceType.INLINE)) {
String msg = "Source type of document " + documentId + " is not INLINE";
log.error(msg);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(msg, 900976L, msg);
return Response.status(Response.Status.BAD_REQUEST).entity(errorDTO).build();
}
apiProvider.addDocumentationContent(documentId, inlineContent);
} else {
String msg = "Either 'file' or 'inlineContent' should be specified";
log.error(msg);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(msg, 900976L, msg);
return Response.status(Response.Status.BAD_REQUEST).entity(errorDTO).build();
}
String newFingerprint = apisApiIdDocumentsDocumentIdContentGetFingerprint(apiId, documentId, null, null, request);
return Response.status(Response.Status.CREATED).header(HttpHeaders.ETAG, "\"" + newFingerprint + "\"").build();
} catch (APIManagementException e) {
String errorMessage = "Error while adding content to document" + documentId;
HashMap<String, String> paramList = new HashMap<String, String>();
paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, apiId);
paramList.put(APIMgtConstants.ExceptionsConstants.DOC_ID, documentId);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
}
use of org.wso2.msf4j.formparam.FileInfo in project carbon-apimgt by wso2.
the class CompositeApisApiServiceImplTestCase method testCompositeApisApiIdImplementationPut.
@Test
public void testCompositeApisApiIdImplementationPut() throws APIManagementException, NotFoundException {
TestUtil.printTestMethodName();
String apiID = UUID.randomUUID().toString();
CompositeApisApiServiceImpl compositeApisApiService = new CompositeApisApiServiceImpl();
APIStore apiStore = Mockito.mock(APIStoreImpl.class);
PowerMockito.mockStatic(RestApiUtil.class);
PowerMockito.when(RestApiUtil.getConsumer(USER)).thenReturn(apiStore);
Request request = TestUtil.getRequest();
PowerMockito.when(RestApiUtil.getLoggedInUsername(request)).thenReturn(USER);
InputStream implmentation = null;
FileInfo fileInfo = new FileInfo();
fileInfo.setFileName("sample.txt");
fileInfo.setContentType(contentType);
Mockito.doNothing().doThrow(new IllegalArgumentException()).when(apiStore).updateCompositeApiImplementation(apiID, implmentation);
Response response = compositeApisApiService.compositeApisApiIdImplementationPut(apiID, implmentation, fileInfo, null, null, request);
Assert.assertEquals(200, response.getStatus());
}
use of org.wso2.msf4j.formparam.FileInfo in project carbon-apimgt by wso2.
the class ApisApiServiceImplTestCase method testApisApiIdWsdlPutFile.
@Test
public void testApisApiIdWsdlPutFile() throws Exception {
printTestMethodName();
File file = new File(getClass().getClassLoader().getResource(WSDL_FILE_LOCATION).getFile());
FileInfo fileInfo = new FileInfo();
fileInfo.setFileName(WSDL_FILE);
InputStream inputStream = new FileInputStream(file);
String fileContent = IOUtils.toString(inputStream);
ApisApiServiceImpl apisApiService = new ApisApiServiceImpl();
APIPublisher apiPublisher = powerMockDefaultAPIPublisher();
API api = SampleTestObjectCreator.createDefaultAPI().build();
Mockito.doReturn(fileContent).when(apiPublisher).updateAPIWSDL(api.getId(), inputStream);
Response response = apisApiService.apisApiIdWsdlPut(api.getId(), inputStream, fileInfo, null, null, getRequest());
assertEquals(response.getStatus(), 200);
assertTrue(response.getEntity().toString().contains("StockQuote"));
}
use of org.wso2.msf4j.formparam.FileInfo in project carbon-apimgt by wso2.
the class ApisApiServiceImplTestCase method testApisApiIdDocumentsDocumentIdContentPostFile.
@Test
public void testApisApiIdDocumentsDocumentIdContentPostFile() throws Exception {
String fileName = "swagger.json";
String contentType = "text/json";
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource(fileName).getFile());
FileInfo fileDetail = new FileInfo();
fileDetail.setFileName(fileName);
fileDetail.setContentType(contentType);
FileInputStream fis = null;
fis = new FileInputStream(file);
printTestMethodName();
ApisApiServiceImpl apisApiService = new ApisApiServiceImpl();
APIPublisher apiPublisher = Mockito.mock(APIPublisherImpl.class);
PowerMockito.mockStatic(RestAPIPublisherUtil.class);
PowerMockito.when(RestAPIPublisherUtil.getApiPublisher(USER)).thenReturn(apiPublisher);
String api1Id = UUID.randomUUID().toString();
String documentId = UUID.randomUUID().toString();
DocumentInfo documentInfo = SampleTestObjectCreator.createDefaultDocumentationInfo().fileName(fileName).sourceType(DocumentInfo.SourceType.FILE).build();
Mockito.doReturn(documentInfo).doThrow(new IllegalArgumentException()).when(apiPublisher).getDocumentationSummary(documentId);
Mockito.doNothing().doThrow(new IllegalArgumentException()).when(apiPublisher).uploadDocumentationFile(documentId, fis, contentType);
Response response = apisApiService.apisApiIdDocumentsDocumentIdContentPost(api1Id, documentId, fis, fileDetail, null, null, null, getRequest());
fis.close();
assertEquals(response.getStatus(), 201);
}
use of org.wso2.msf4j.formparam.FileInfo in project carbon-apimgt by wso2.
the class ApisApiServiceImplTestCase method testApisValidateDefinitionPostWSDLFileInvalidExtension.
@Test
public void testApisValidateDefinitionPostWSDLFileInvalidExtension() throws Exception {
printTestMethodName();
File file = new File(getClass().getClassLoader().getResource("swagger.json").getFile());
FileInputStream fis = new FileInputStream(file);
FileInfo fileInfo = new FileInfo();
fileInfo.setFileName("swagger.json");
ApisApiServiceImpl apisApiService = new ApisApiServiceImpl();
powerMockDefaultAPIPublisher();
Response response = apisApiService.apisValidateDefinitionPost(WSDL, fis, fileInfo, null, getRequest());
fis.close();
assertEquals(response.getStatus(), 400);
}
Aggregations