use of org.wso2.carbon.apimgt.api.model.Documentation in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method apisApiIdDocumentsGet.
/**
* Retrieves a list of documents of an API
*
* @param apiId UUID of API
* @param limit maximum documents to return
* @param offset starting position of the pagination
* @param ifNoneMatch If-None-Match header value
* @param request minor version header
* @return a list of document DTOs
* @throws NotFoundException When the particular resource does not exist in the system
*/
@Override
public Response apisApiIdDocumentsGet(String apiId, Integer limit, Integer offset, String ifNoneMatch, Request request) throws NotFoundException {
DocumentListDTO documentListDTO = null;
limit = limit != null ? limit : RestApiConstants.PAGINATION_LIMIT_DEFAULT;
offset = offset != null ? offset : RestApiConstants.PAGINATION_OFFSET_DEFAULT;
String username = RestApiUtil.getLoggedInUsername(request);
try {
APIStore apiStore = RestApiUtil.getConsumer(username);
List<DocumentInfo> documentInfoResults = apiStore.getAllDocumentation(apiId, offset, limit);
documentListDTO = DocumentationMappingUtil.fromDocumentationListToDTO(documentInfoResults, offset, limit);
} catch (APIManagementException e) {
String errorMessage = "Error while retrieving documentation for given apiId " + apiId;
HashMap<String, String> paramList = new HashMap<String, String>();
paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, apiId);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
return Response.ok().entity(documentListDTO).build();
}
use of org.wso2.carbon.apimgt.api.model.Documentation in project carbon-apimgt by wso2.
the class FileBasedApiImportExportManager method getDocumentInfoFromExtractedArchive.
/**
* Retrieves {@link DocumentInfo} instance from the directory containing docs
*
* @param documentImportLocation path to the directory containing docs
* @param apiName API name
* @param version API version
* @return Set of {@link DocumentInfo} insjtaces
*/
private Set<DocumentInfo> getDocumentInfoFromExtractedArchive(String documentImportLocation, String apiName, String version) {
Set<DocumentInfo> documents = new HashSet<>();
File rootDocumentationDirectoryForAPI = new File(documentImportLocation);
if (!rootDocumentationDirectoryForAPI.isDirectory()) {
// no Docs!
log.debug("No documentation found for API name: " + apiName + ", version: " + version);
return documents;
}
File[] documentationDirectories = rootDocumentationDirectoryForAPI.listFiles(File::isDirectory);
if (documentationDirectories == null) {
// do docs!
log.debug("No documents found at " + documentImportLocation);
return documents;
}
for (File docDir : documentationDirectories) {
// read the 'doc.json'
String content;
try {
content = APIFileUtils.readFileContentAsText(docDir.getPath() + File.separator + DOCUMENTATION_DEFINITION_FILE);
Gson gson = new GsonBuilder().create();
documents.add(gson.fromJson(content, DocumentInfo.class));
// add the doc
} catch (APIManagementException e) {
// no need to throw, log and continue
log.error("Error in importing documentation from file: " + docDir.getPath() + " for API: " + apiName + ", version: " + version);
}
}
return documents;
}
use of org.wso2.carbon.apimgt.api.model.Documentation in project carbon-apimgt by wso2.
the class ApiImportExportManager method addAPIDetails.
/**
* Adds the API details
*
* @param apiDetails {@link APIDetails} instance
* @throws APIManagementException if an error occurs while adding API details
*/
public void addAPIDetails(APIDetails apiDetails) throws APIManagementException {
// update everything
String swaggerDefinition = apiDetails.getSwaggerDefinition();
String gatewayConfig = apiDetails.getGatewayConfiguration();
Map<String, Endpoint> endpointTypeToIdMap = apiDetails.getApi().getEndpoint();
Map<String, UriTemplate> uriTemplateMap = apiDetails.getApi().getUriTemplates();
// endpoints
for (Endpoint endpoint : apiDetails.getEndpoints()) {
try {
Endpoint existingEndpoint = apiPublisher.getEndpointByName(endpoint.getName());
String endpointId;
if (existingEndpoint == null) {
// no endpoint by that name, add it
endpointId = apiPublisher.addEndpoint(endpoint);
} else {
endpointId = existingEndpoint.getId();
if (log.isDebugEnabled()) {
log.debug("Endpoint with id " + endpoint.getId() + " already exists, not adding again");
}
// endpoint with same name exists, add to endpointTypeToIdMap
// endpointTypeToIdMap.put(endpoint.getType(), existingEndpoint.getId());
}
endpointTypeToIdMap.forEach((String k, Endpoint v) -> {
if (endpoint.getName().equals(v.getName())) {
Endpoint replacedEndpoint = new Endpoint.Builder(v).id(endpointId).build();
endpointTypeToIdMap.replace(k, replacedEndpoint);
}
});
uriTemplateMap.forEach(((String templateId, UriTemplate uriTemplate) -> {
UriTemplate.UriTemplateBuilder uriTemplateBuilder = new UriTemplate.UriTemplateBuilder(uriTemplate);
Map<String, Endpoint> uriEndpointMap = uriTemplateBuilder.getEndpoint();
uriEndpointMap.forEach((String type, Endpoint endpoint1) -> {
if (endpoint.getName().equals(endpoint1.getName())) {
Endpoint replacedEndpoint = new Endpoint.Builder(endpoint1).id(endpointId).build();
uriEndpointMap.replace(type, replacedEndpoint);
}
});
uriTemplateMap.replace(templateId, uriTemplateBuilder.endpoint(uriEndpointMap).build());
}));
} catch (APIManagementException e) {
// skip adding this API; log and continue
log.error("Error while adding the endpoint with id: " + endpoint.getId() + ", type: " + endpoint.getType() + " for API: " + apiDetails.getApi().getName() + ", version: " + apiDetails.getApi().getVersion());
}
}
API.APIBuilder apiBuilder = new API.APIBuilder(apiDetails.getApi());
apiPublisher.addAPI(apiBuilder.apiDefinition(swaggerDefinition).gatewayConfig(gatewayConfig).endpoint(endpointTypeToIdMap).uriTemplates(uriTemplateMap));
// docs
try {
Set<DocumentInfo> documentInfo = apiDetails.getAllDocumentInformation();
for (DocumentInfo aDocInfo : documentInfo) {
apiPublisher.addDocumentationInfo(apiDetails.getApi().getId(), aDocInfo);
}
for (DocumentContent aDocContent : apiDetails.getDocumentContents()) {
// add documentation
if (aDocContent.getDocumentInfo().getSourceType().equals(DocumentInfo.SourceType.FILE)) {
apiPublisher.uploadDocumentationFile(aDocContent.getDocumentInfo().getId(), aDocContent.getFileContent(), URLConnection.guessContentTypeFromStream(aDocContent.getFileContent()));
} else if (aDocContent.getDocumentInfo().getSourceType().equals(DocumentInfo.SourceType.INLINE)) {
apiPublisher.addDocumentationContent(aDocContent.getDocumentInfo().getId(), aDocContent.getInlineContent());
}
}
} catch (APIManagementException e) {
// no need to throw, log and continue
log.error("Error while adding Document details for API: " + apiDetails.getApi().getName() + ", version: " + apiDetails.getApi().getVersion(), e);
} catch (IOException e) {
// no need to throw, log and continue
log.error("Error while retrieving content type of the File documentation of API : " + apiDetails.getApi().getName() + ", version: " + apiDetails.getApi().getVersion(), e);
}
// add thumbnail
try {
apiPublisher.saveThumbnailImage(apiDetails.getApi().getId(), apiDetails.getThumbnailStream(), "thumbnail");
} catch (APIManagementException e) {
// no need to throw, log and continue
log.error("Error while adding thumbnail for API: " + apiDetails.getApi().getName() + ", version: " + apiDetails.getApi().getVersion(), e);
}
}
use of org.wso2.carbon.apimgt.api.model.Documentation in project carbon-apimgt by wso2.
the class ApisApiServiceImplTestCase method testApisApiIdDocumentsDocumentIdGet.
@Test
public void testApisApiIdDocumentsDocumentIdGet() throws Exception {
printTestMethodName();
ApisApiServiceImpl apisApiService = new ApisApiServiceImpl();
APIPublisher apiPublisher = Mockito.mock(APIPublisherImpl.class);
PowerMockito.mockStatic(RestAPIPublisherUtil.class);
PowerMockito.when(RestAPIPublisherUtil.getApiPublisher(USER)).thenReturn(apiPublisher);
String documentId = UUID.randomUUID().toString();
String apiId = UUID.randomUUID().toString();
DocumentInfo documentInfo = SampleTestObjectCreator.createDefaultDocumentationInfo().build();
Mockito.doReturn(documentInfo).doThrow(new IllegalArgumentException()).when(apiPublisher).getDocumentationSummary(documentId);
Response response = apisApiService.apisApiIdDocumentsDocumentIdGet(apiId, documentId, null, null, getRequest());
assertEquals(response.getStatus(), 200);
assertTrue(response.getEntity().toString().contains("Summary of Calculator Documentation"));
}
use of org.wso2.carbon.apimgt.api.model.Documentation in project carbon-apimgt by wso2.
the class ApisApiServiceImplTestCase method testApisApiIdDocumentsDocumentIdContentPostNotFound.
@Test
public void testApisApiIdDocumentsDocumentIdContentPostNotFound() throws Exception {
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();
Mockito.doReturn(null).doThrow(new IllegalArgumentException()).when(apiPublisher).getDocumentationSummary(documentId);
Response response = apisApiService.apisApiIdDocumentsDocumentIdContentPost(api1Id, documentId, null, null, null, null, null, getRequest());
assertEquals(response.getStatus(), 404);
assertTrue(response.getEntity().toString().contains("Documentation not found"));
}
Aggregations