Skip to content

Commit fad00e8

Browse files
author
Chris Wiechmann
committed
Error-Handling improved if Unknown APUI-Spec
1 parent 519ee7f commit fad00e8

11 files changed

Lines changed: 41 additions & 27 deletions

File tree

modules/api-export/src/main/java/com/axway/apim/APIExportApp.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,10 @@ private static int runExport(APIExportParams params, APIListImpl resultHandlerIm
119119
}
120120
}
121121
APIManagerAdapter.deleteInstance();
122-
return 0;
122+
if(ErrorState.getInstance().hasError()) {
123+
ErrorState.getInstance().logErrorMessages(LOG);
124+
}
125+
return ErrorState.getInstance().getErrorCode().getCode();
123126
} catch (AppException ap) {
124127
ErrorState errorState = ErrorState.getInstance();
125128
if(errorState.hasError()) {

modules/api-export/src/main/java/com/axway/apim/api/export/impl/JsonAPIExporter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ private void saveAPILocally(ExportAPI exportAPI) throws AppException {
123123
if(exportAPI.getCaCerts()!=null && !exportAPI.getCaCerts().isEmpty()) {
124124
storeCaCerts(localFolder, exportAPI.getCaCerts());
125125
}
126-
LOG.info("Successfully export API to folder: " + localFolder);
126+
LOG.info("Successfully exported API into folder: " + localFolder);
127127
if(!APIManagerAdapter.hasAdminAccount()) {
128128
LOG.warn("Export has been done with an Org-Admin account only. Export is restricted by the following: ");
129129
LOG.warn("- No Quotas has been exported for the API");

modules/api-import/src/main/java/com/axway/apim/apiimport/APIImportConfigAdapter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ public API getDesiredAPI() throws AppException {
219219
addDefaultAuthenticationProfile(apiConfig);
220220
validateOutboundProfile(apiConfig);
221221
validateInboundProfile(apiConfig);
222-
APISpecification apiSpecification = APISpecificationFactory.getAPISpecification(getAPIDefinitionContent(), this.pathToAPIDefinition, ((DesiredAPI)apiConfig).getBackendBasepath());
222+
APISpecification apiSpecification = APISpecificationFactory.getAPISpecification(getAPIDefinitionContent(), this.pathToAPIDefinition, ((DesiredAPI)apiConfig).getBackendBasepath(), apiConfig.getName());
223223
apiConfig.setApiDefinition(apiSpecification);
224224
addImageContent(apiConfig);
225225
validateCustomProperties(apiConfig);

modules/api-import/src/test/java/com/axway/apim/api/definition/APISpecificationOpenAPI3xTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ private void initTestIndicator() {
3535
public void backendHostAndBasePath() throws AppException, IOException {
3636

3737
byte[] content = getSwaggerContent(testPackage + "/openapi30.json");
38-
APISpecification apiDefinition = APISpecificationFactory.getAPISpecification(content, "teststore.json", "https://myhost.customer.com:8767/api/v1/myAPI");
38+
APISpecification apiDefinition = APISpecificationFactory.getAPISpecification(content, "teststore.json", "https://myhost.customer.com:8767/api/v1/myAPI", "TesAPI");
3939

4040
// Check if the Swagger-File has been changed
4141
Assert.assertTrue(apiDefinition instanceof OAS3xSpecification);

modules/api-import/src/test/java/com/axway/apim/api/definition/APISpecificationSwagger1xTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ private void initTestIndicator() {
3333
@Test
3434
public void standardPort() throws AppException, IOException {
3535
byte[] content = getSwaggerContent(testPackage + "/swagger12.json");
36-
APISpecification apiDefinition = APISpecificationFactory.getAPISpecification(content, "teststore.json", "https://petstore.swagger.io");
36+
APISpecification apiDefinition = APISpecificationFactory.getAPISpecification(content, "teststore.json", "https://petstore.swagger.io", "Test-API");
3737

3838
Assert.assertTrue(apiDefinition instanceof Swagger1xSpecification, "Specification must be an Swagger12Specification");
3939
JsonNode swagger = mapper.readTree(apiDefinition.getApiSpecificationContent());
@@ -43,7 +43,7 @@ public void standardPort() throws AppException, IOException {
4343
@Test
4444
public void specificPort() throws AppException, IOException {
4545
byte[] content = getSwaggerContent(testPackage + "/swagger12.json");
46-
APISpecification apiDefinition = APISpecificationFactory.getAPISpecification(content, "teststore.json", "https://petstore.swagger.io:8180");
46+
APISpecification apiDefinition = APISpecificationFactory.getAPISpecification(content, "teststore.json", "https://petstore.swagger.io:8180", "Test-API");
4747

4848
Assert.assertTrue(apiDefinition instanceof Swagger1xSpecification, "Specification must be an Swagger12Specification");
4949
JsonNode swagger = mapper.readTree(apiDefinition.getApiSpecificationContent());
@@ -53,7 +53,7 @@ public void specificPort() throws AppException, IOException {
5353
@Test
5454
public void standardPortGiven() throws AppException, IOException {
5555
byte[] content = getSwaggerContent(testPackage + "/swagger12.json");
56-
APISpecification apiDefinition = APISpecificationFactory.getAPISpecification(content, "teststore.json", "https://petstore.swagger.io:443");
56+
APISpecification apiDefinition = APISpecificationFactory.getAPISpecification(content, "teststore.json", "https://petstore.swagger.io:443", "Test-API");
5757

5858
Assert.assertTrue(apiDefinition instanceof Swagger1xSpecification, "Specification must be an Swagger12Specification");
5959
JsonNode swagger = mapper.readTree(apiDefinition.getApiSpecificationContent());
@@ -63,7 +63,7 @@ public void standardPortGiven() throws AppException, IOException {
6363
@Test
6464
public void includingBasePath() throws AppException, IOException {
6565
byte[] content = getSwaggerContent(testPackage + "/swagger12.json");
66-
APISpecification apiDefinition = APISpecificationFactory.getAPISpecification(content, "teststore.json", "https://petstore.swagger.io:443/myapi");
66+
APISpecification apiDefinition = APISpecificationFactory.getAPISpecification(content, "teststore.json", "https://petstore.swagger.io:443/myapi", "Test-API");
6767

6868
Assert.assertTrue(apiDefinition instanceof Swagger1xSpecification, "Specification must be an Swagger12Specification");
6969
JsonNode swagger = mapper.readTree(apiDefinition.getApiSpecificationContent());
@@ -73,7 +73,7 @@ public void includingBasePath() throws AppException, IOException {
7373
@Test
7474
public void testSwagger11Specification() throws AppException, IOException {
7575
byte[] content = getSwaggerContent(testPackage + "/swagger11.json");
76-
APISpecification apiDefinition = APISpecificationFactory.getAPISpecification(content, "teststore.json", "https://petstore.swagger.io:443/myapi");
76+
APISpecification apiDefinition = APISpecificationFactory.getAPISpecification(content, "teststore.json", "https://petstore.swagger.io:443/myapi", "Test-API");
7777

7878
Assert.assertTrue(apiDefinition instanceof Swagger1xSpecification, "Specification must be an Swagger12Specification");
7979
JsonNode swagger = mapper.readTree(apiDefinition.getApiSpecificationContent());

modules/api-import/src/test/java/com/axway/apim/api/definition/APISpecificationSwagger2xTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ private void initTestIndicator() {
3232
public void backendHostAndBasePath() throws AppException, IOException {
3333

3434
byte[] content = getSwaggerContent("/api_definition_1/petstore.json");
35-
APISpecification apiDefinition = APISpecificationFactory.getAPISpecification(content, "teststore.json", "https://myhost.customer.com:8767/api/v1/myAPI");
35+
APISpecification apiDefinition = APISpecificationFactory.getAPISpecification(content, "teststore.json", "https://myhost.customer.com:8767/api/v1/myAPI", "Test-API");
3636

3737
// Check if the Swagger-File has been changed
3838
Assert.assertTrue(apiDefinition instanceof Swagger2xSpecification);
@@ -46,7 +46,7 @@ public void backendHostAndBasePath() throws AppException, IOException {
4646
@Test
4747
public void backendHostOnly() throws AppException, IOException {
4848
byte[] content = getSwaggerContent("/api_definition_1/petstore.json");
49-
APISpecification apiDefinition = APISpecificationFactory.getAPISpecification(content, "teststore.json", "http://myhost.customer.com:8767");
49+
APISpecification apiDefinition = APISpecificationFactory.getAPISpecification(content, "teststore.json", "http://myhost.customer.com:8767", "Test-API");
5050

5151
Assert.assertTrue(apiDefinition instanceof Swagger2xSpecification);
5252
JsonNode swagger = mapper.readTree(apiDefinition.getApiSpecificationContent());
@@ -59,7 +59,7 @@ public void backendHostOnly() throws AppException, IOException {
5959
@Test
6060
public void backendHostBasisBasePath() throws AppException, IOException {
6161
byte[] content = getSwaggerContent("/api_definition_1/petstore.json");
62-
APISpecification apiDefinition = APISpecificationFactory.getAPISpecification(content, "teststore.json", "https://myhost.customer.com/");
62+
APISpecification apiDefinition = APISpecificationFactory.getAPISpecification(content, "teststore.json", "https://myhost.customer.com/", "Test-API");
6363

6464
Assert.assertTrue(apiDefinition instanceof Swagger2xSpecification);
6565
JsonNode swagger = mapper.readTree(apiDefinition.getApiSpecificationContent());
@@ -72,7 +72,7 @@ public void backendHostBasisBasePath() throws AppException, IOException {
7272
@Test
7373
public void swaggerWithoutSchemes() throws AppException, IOException {
7474
byte[] content = getSwaggerContent("/api_definition_1/petstore-without-schemes.json");
75-
APISpecification apiDefinition = APISpecificationFactory.getAPISpecification(content, "teststore.json", "https://myhost.customer.com/");
75+
APISpecification apiDefinition = APISpecificationFactory.getAPISpecification(content, "teststore.json", "https://myhost.customer.com/", "Test-API");
7676

7777
Assert.assertTrue(apiDefinition instanceof Swagger2xSpecification);
7878
JsonNode swagger = mapper.readTree(apiDefinition.getApiSpecificationContent());
@@ -85,7 +85,7 @@ public void swaggerWithoutSchemes() throws AppException, IOException {
8585
@Test
8686
public void backendBasepathChangesNothing() throws AppException, IOException {
8787
byte[] content = getSwaggerContent("/api_definition_1/petstore-only-https-scheme.json");
88-
APISpecification apiDefinition = APISpecificationFactory.getAPISpecification(content, "teststore.json", "https://petstore.swagger.io");
88+
APISpecification apiDefinition = APISpecificationFactory.getAPISpecification(content, "teststore.json", "https://petstore.swagger.io", "Test-API");
8989

9090
Assert.assertTrue(apiDefinition instanceof Swagger2xSpecification);
9191
JsonNode swagger = mapper.readTree(apiDefinition.getApiSpecificationContent());
@@ -99,7 +99,7 @@ public void backendBasepathChangesNothing() throws AppException, IOException {
9999
public void testWithoutBackendBasepath() throws AppException, IOException {
100100

101101
byte[] content = getSwaggerContent("/api_definition_1/petstore.json");
102-
APISpecification apiDefinition = APISpecificationFactory.getAPISpecification(content, "teststore.json", null);
102+
APISpecification apiDefinition = APISpecificationFactory.getAPISpecification(content, "teststore.json", null, "Test-API");
103103

104104
// Check if the Swagger-File has been changed
105105
Assert.assertTrue(apiDefinition instanceof Swagger2xSpecification);

modules/api-import/src/test/java/com/axway/apim/api/definition/APISpecificationWSDLTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ private void initTestIndicator() {
3131
public void isWSDLSpecification() throws AppException, IOException {
3232

3333
byte[] content = getSwaggerContent(testPackage + "/sample-wsdl.xml");
34-
APISpecification apiDefinition = APISpecificationFactory.getAPISpecification(content, "http://www.mnb.hu/arfolyamok.asmx?WSDL", "https://myhost.customer.com:8767/api/v1/myAPI");
34+
APISpecification apiDefinition = APISpecificationFactory.getAPISpecification(content, "http://www.mnb.hu/arfolyamok.asmx?WSDL", "https://myhost.customer.com:8767/api/v1/myAPI", "Test-API");
3535

3636
// Check if the Swagger-File has been changed
3737
Assert.assertTrue(apiDefinition instanceof WSDLSpecification);

modules/apim-adapter/src/main/java/com/axway/apim/adapter/apis/APIManagerAPIAdapter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ private static void addOriginalAPIDefinitionFromAPIM(API api, APIFilter filter)
485485
if(httpResponse.containsHeader("Content-Disposition")) {
486486
origFilename = httpResponse.getHeaders("Content-Disposition")[0].getValue();
487487
}
488-
apiDefinition = APISpecificationFactory.getAPISpecification(res.getBytes(StandardCharsets.UTF_8), origFilename.substring(origFilename.indexOf("filename=")+9), null, filter.isFailOnError());
488+
apiDefinition = APISpecificationFactory.getAPISpecification(res.getBytes(StandardCharsets.UTF_8), origFilename.substring(origFilename.indexOf("filename=")+9), null, api.getName(), filter.isFailOnError());
489489
api.setApiDefinition(apiDefinition);
490490
} catch (Exception e) {
491491
throw new AppException("Cannot parse API-Definition for API: '" + api.getName() + "' ("+api.getVersion()+") on path: '"+api.getPath()+"'", ErrorCode.CANT_READ_API_DEFINITION_FILE, e);

modules/apim-adapter/src/main/java/com/axway/apim/api/definition/APISpecificationFactory.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ public class APISpecificationFactory {
2424
add(WSDLSpecification.class);
2525
}};
2626

27-
public static APISpecification getAPISpecification(byte[] apiSpecificationContent, String apiDefinitionFile, String backendBasepath) throws AppException {
28-
return getAPISpecification(apiSpecificationContent, apiDefinitionFile, backendBasepath, true);
27+
public static APISpecification getAPISpecification(byte[] apiSpecificationContent, String apiDefinitionFile, String backendBasepath, String apiName) throws AppException {
28+
return getAPISpecification(apiSpecificationContent, apiDefinitionFile, backendBasepath, apiName, true);
2929
}
3030

31-
public static APISpecification getAPISpecification(byte[] apiSpecificationContent, String apiDefinitionFile, String backendBasepath, boolean failOnError) throws AppException {
31+
public static APISpecification getAPISpecification(byte[] apiSpecificationContent, String apiDefinitionFile, String backendBasepath, String apiName, boolean failOnError) throws AppException {
3232
if(LOG.isDebugEnabled()) {
3333
LOG.debug("Handle API-Specification: '" + getContentStart(apiSpecificationContent) + "...', apiDefinitionFile: '"+apiDefinitionFile+"'");
3434
}
@@ -54,14 +54,15 @@ public static APISpecification getAPISpecification(byte[] apiSpecificationConten
5454
}
5555
}
5656
if(!failOnError) {
57-
LOG.error("Can't handle API specification: '" + getContentStart(apiSpecificationContent) + "'");
58-
return new UnknownAPISpecification(apiSpecificationContent, backendBasepath);
57+
LOG.error("API: '"+apiName+"' has a unkown/invalid API-Specification: '" + getContentStart(apiSpecificationContent) + "'");
58+
ErrorState.getInstance().setError("API: '"+apiName+"' has a unkown/invalid API-Specification. Please check the log.", ErrorCode.CANT_READ_API_DEFINITION_FILE, false);
59+
return new UnknownAPISpecification(apiSpecificationContent, backendBasepath, apiName);
5960
}
60-
LOG.error("Can't handle API specification: '" + getContentStart(apiSpecificationContent) + "'");
61-
throw new AppException("Can't handle API specification. No suiteable API-Specification implementation available.", ErrorCode.UNXPECTED_ERROR);
61+
LOG.error("API: '"+apiName+"' has a unkown/invalid API-Specification: '" + getContentStart(apiSpecificationContent) + "'");
62+
throw new AppException("Can't handle API specification. No suiteable API-Specification implementation available.", ErrorCode.CANT_READ_API_DEFINITION_FILE);
6263
}
6364

64-
private static String getContentStart(byte[] apiSpecificationContent) {
65+
static String getContentStart(byte[] apiSpecificationContent) {
6566
try {
6667
if(apiSpecificationContent == null) return "API-Specificaion is null";
6768
return (apiSpecificationContent.length<200) ? new String(apiSpecificationContent, 0, apiSpecificationContent.length) : new String(apiSpecificationContent, 0, 200) + "...";

modules/apim-adapter/src/main/java/com/axway/apim/api/definition/UnknownAPISpecification.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
import com.axway.apim.lib.errorHandling.AppException;
44

55
public class UnknownAPISpecification extends APISpecification {
6+
7+
String apiName;
68

7-
public UnknownAPISpecification(byte[] apiSpecificationContent, String backendBasepath) throws AppException {
9+
public UnknownAPISpecification(byte[] apiSpecificationContent, String backendBasepath, String apiName) throws AppException {
810
super(apiSpecificationContent, backendBasepath);
11+
this.apiName = apiName;
912
}
1013

1114
public UnknownAPISpecification() {
@@ -25,4 +28,11 @@ public boolean configure() throws AppException {
2528
return false;
2629
}
2730

31+
@Override
32+
public byte[] getApiSpecificationContent() {
33+
LOG.error("API: '" + this.apiName + "' has a unkown/invalid API-Specification: " + APISpecificationFactory.getContentStart(this.apiSpecificationContent) );
34+
return super.getApiSpecificationContent();
35+
}
36+
37+
2838
}

0 commit comments

Comments
 (0)