Skip to content

Commit b4cfecd

Browse files
author
Chris Wiechmann
committed
Merge branch 'develop' into test-with-7.7-20200130
2 parents a3b7bba + dc8ddc6 commit b4cfecd

13 files changed

Lines changed: 370 additions & 11 deletions

File tree

modules/apim-adapter/src/main/java/com/axway/apim/adapter/APIManagerAdapter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ public static User getCurrentUser(boolean useAdminClient) throws AppException {
309309

310310
private static void getCsrfToken(HttpResponse response, boolean useAdminClient) throws AppException {
311311
for (Header header : response.getAllHeaders()) {
312-
if(header.getName().equals("CSRF-Token")) {
312+
if(header.getName().equalsIgnoreCase("csrf-token")) {
313313
APIMHttpClient.getInstance(useAdminClient).setCsrfToken(header.getValue());
314314
break;
315315
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,15 @@ public Organization createOrUpdateOrganization(Organization desiredOrg, Organiza
122122
try {
123123
URI uri;
124124
if(actualOrg==null) {
125+
if(!APIManagerAdapter.hasAdminAccount()) {
126+
throw new AppException("Admin account is required to create a new organization", ErrorCode.NO_ADMIN_ROLE_USER);
127+
}
125128
uri = new URIBuilder(cmd.getAPIManagerURL()).setPath(RestAPICall.API_VERSION+"/organizations").build();
126129
} else {
127130
uri = new URIBuilder(cmd.getAPIManagerURL()).setPath(RestAPICall.API_VERSION+"/organizations/"+actualOrg.getId()).build();
128131
}
129132
FilterProvider filter = new SimpleFilterProvider().setDefaultFilter(
130-
SimpleBeanPropertyFilter.serializeAllExcept(new String[] {"image"}));
133+
SimpleBeanPropertyFilter.serializeAllExcept(new String[] {"image", "createdOn"}));
131134
mapper.setFilterProvider(filter);
132135
mapper.setSerializationInclusion(Include.NON_NULL);
133136
try {
@@ -137,6 +140,7 @@ public Organization createOrUpdateOrganization(Organization desiredOrg, Organiza
137140
HttpEntity entity = new StringEntity(json);
138141
request = new POSTRequest(entity, uri, true);
139142
} else {
143+
desiredOrg.setId(actualOrg.getId());
140144
String json = mapper.writeValueAsString(desiredOrg);
141145
HttpEntity entity = new StringEntity(json);
142146
request = new PUTRequest(entity, uri, true);

modules/apim-adapter/src/main/java/com/axway/apim/api/model/APIAccess.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class APIAccess {
1414

1515
String state;
1616

17-
String createdOn;
17+
Long createdOn;
1818

1919
String apiName;
2020

@@ -54,11 +54,11 @@ public void setState(String state) {
5454
this.state = state;
5555
}
5656

57-
public String getCreatedOn() {
57+
public Long getCreatedOn() {
5858
return createdOn;
5959
}
6060

61-
public void setCreatedOn(String createdOn) {
61+
public void setCreatedOn(Long createdOn) {
6262
this.createdOn = createdOn;
6363
}
6464

modules/apim-adapter/src/main/java/com/axway/apim/api/model/AuthenticationProfile.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,30 @@ public boolean equals(Object other) {
6464
Map<String, Object> thisParameters = this.getParameters();
6565
otherParameters.remove("_id_");
6666
thisParameters.remove("_id_");
67+
// Passwords are no longer exposed by API-Manager REST-API - Can't use it anymore to compare the state
68+
Object otherPassword = null;
69+
Object thisPassword = null;
6770
if(APIManagerAdapter.hasAPIManagerVersion("7.7 SP1") || APIManagerAdapter.hasAPIManagerVersion("7.6.2 SP5")) {
68-
// Password no longer exposed by API-Manager REST-API - Can't use it anymore to compare the state
69-
otherParameters.remove("password");
70-
thisParameters.remove("password");
71+
// Empty password handling - Make sure, there is a password set
72+
if(!thisParameters.containsKey("password") || thisParameters.get("password")==null) thisParameters.put("password", "");
73+
if(!otherParameters.containsKey("password") || otherParameters.get("password")==null) otherParameters.put("password", "");
74+
if(otherParameters.containsKey("password")) {
75+
otherPassword = otherParameters.get("password");
76+
otherParameters.remove("password");
77+
}
78+
if(thisParameters.containsKey("password")) {
79+
thisPassword = thisParameters.get("password");
80+
thisParameters.remove("password");
81+
}
7182
}
7283

7384
boolean rc = StringUtils.equals(authenticationProfile.getName(), this.getName())
7485
&& authenticationProfile.getIsDefault() == this.getIsDefault()
7586
&& StringUtils.equals(authenticationProfile.getType().name(),this.getType().name())
7687
&& otherParameters.equals(thisParameters);
88+
// Restore that password, that have been removed
89+
if(otherPassword!=null) otherParameters.put("password", otherPassword);
90+
if(thisPassword!=null) thisParameters.put("password", thisPassword);
7791
return rc;
7892
} else {
7993
return false;

modules/apis/src/main/java/com/axway/apim/apiimport/DesiredAPI.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import com.axway.apim.lib.errorHandling.AppException;
2020
import com.axway.apim.lib.errorHandling.ErrorCode;
2121
import com.axway.apim.lib.errorHandling.ErrorState;
22-
import com.fasterxml.jackson.annotation.JsonIgnore;
2322
import com.fasterxml.jackson.annotation.JsonProperty;
2423

2524
/**

modules/apps/src/main/java/com/axway/apim/appexport/ApplicationExportApp.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ public static int export(String args[]) {
5656
return runExport(params, ExportImpl.CONSOLE_EXPORTER);
5757
case json:
5858
return runExport(params, ExportImpl.JSON_EXPORTER);
59+
case csv:
60+
return runExport(params, ExportImpl.CSV_EXPORTER);
5961
default:
6062
return runExport(params, ExportImpl.CONSOLE_EXPORTER);
6163
}

modules/apps/src/main/java/com/axway/apim/appexport/impl/ApplicationExporter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ public abstract class ApplicationExporter {
1919

2020
public enum ExportImpl {
2121
JSON_EXPORTER(JsonApplicationExporter.class),
22-
CONSOLE_EXPORTER(ConsoleAppExporter.class);
22+
CONSOLE_EXPORTER(ConsoleAppExporter.class),
23+
CSV_EXPORTER(CSVAppExporter.class);
2324

2425
private final Class<ApplicationExporter> implClass;
2526

Lines changed: 264 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,264 @@
1+
package com.axway.apim.appexport.impl;
2+
3+
import java.io.File;
4+
import java.io.FileWriter;
5+
import java.io.IOException;
6+
import java.text.DateFormat;
7+
import java.text.SimpleDateFormat;
8+
import java.util.Date;
9+
import java.util.List;
10+
11+
import org.apache.commons.csv.CSVFormat;
12+
import org.apache.commons.csv.CSVPrinter;
13+
import org.slf4j.Logger;
14+
import org.slf4j.LoggerFactory;
15+
16+
import com.axway.apim.adapter.APIManagerAdapter;
17+
import com.axway.apim.adapter.clientApps.ClientAppFilter;
18+
import com.axway.apim.adapter.clientApps.ClientAppFilter.Builder;
19+
import com.axway.apim.api.API;
20+
import com.axway.apim.api.model.APIAccess;
21+
import com.axway.apim.api.model.QuotaRestriction;
22+
import com.axway.apim.api.model.apps.ClientApplication;
23+
import com.axway.apim.appexport.lib.APIAccessComparator;
24+
import com.axway.apim.appexport.lib.AppExportParams;
25+
import com.axway.apim.appexport.lib.ApplicationComparator;
26+
import com.axway.apim.lib.StandardExportParams.Wide;
27+
import com.axway.apim.lib.errorHandling.AppException;
28+
import com.axway.apim.lib.errorHandling.ErrorCode;
29+
import com.axway.apim.lib.errorHandling.ErrorState;
30+
31+
public class CSVAppExporter extends ApplicationExporter {
32+
private static Logger LOG = LoggerFactory.getLogger(CSVAppExporter.class);
33+
34+
private static enum HeaderFields {
35+
standard(new String[] {
36+
"ID",
37+
"Organization",
38+
"Name",
39+
"Email",
40+
"Phone",
41+
"State",
42+
"Enabled"
43+
}),
44+
wide(new String[] {
45+
"ID",
46+
"Organization",
47+
"Name",
48+
"Email",
49+
"Phone",
50+
"State",
51+
"Enabled",
52+
"API Quota",
53+
"API-Method",
54+
"Quota Config"
55+
}),
56+
ultra(new String[] {
57+
"ID",
58+
"Organization",
59+
"Name",
60+
"Email",
61+
"Phone",
62+
"State",
63+
"Enabled",
64+
"API-Name",
65+
"API-Version",
66+
"Access created by",
67+
"Access created on"
68+
});
69+
70+
String[] headerFields;
71+
72+
private HeaderFields(String[] headerFields) {
73+
this.headerFields = headerFields;
74+
}
75+
}
76+
77+
APIManagerAdapter apiManager;
78+
79+
public CSVAppExporter(AppExportParams params) throws AppException {
80+
super(params);
81+
apiManager = APIManagerAdapter.getInstance();
82+
}
83+
84+
@Override
85+
public void export(List<ClientApplication> apps) throws AppException {
86+
CSVPrinter csvPrinter = null;
87+
Wide wide = params.getWide();
88+
String givenTarget = params.getTarget();
89+
try {
90+
File target = new File(givenTarget);
91+
if(target.isDirectory()) {
92+
target = new File(givenTarget + File.separator + createFileName());
93+
}
94+
if(target.exists() && !params.deleteTarget()) {
95+
ErrorState.getInstance().setError("Targetfile: " + target.getCanonicalPath() + " already exists. You may set the flag -deleteTarget if you wish to overwrite it.", ErrorCode.EXPORT_FOLDER_EXISTS, false);
96+
throw new AppException("Targetfile: " + target.getCanonicalPath() + " already exists.", ErrorCode.EXPORT_FOLDER_EXISTS);
97+
}
98+
Appendable appendable = new FileWriter(target);
99+
appendable.append("sep=,\n"); // Helps Excel to detect columns
100+
csvPrinter = new CSVPrinter(appendable, CSVFormat.DEFAULT.withHeader(HeaderFields.valueOf(wide.name()).headerFields));
101+
writeRecords(csvPrinter, apps, wide);
102+
LOG.info("API export successfully written to file: " + target.getCanonicalPath());
103+
} catch (IOException e1) {
104+
throw new AppException("Cant open CSV-File: "+givenTarget+" for writing", ErrorCode.UNXPECTED_ERROR, e1);
105+
} finally {
106+
if(csvPrinter!=null)
107+
try {
108+
csvPrinter.close(true);
109+
} catch (Exception ignore) {
110+
throw new AppException("Unable to close CSVWriter", ErrorCode.UNXPECTED_ERROR, ignore);
111+
}
112+
}
113+
}
114+
115+
private String createFileName() throws AppException {
116+
DateFormat df = new SimpleDateFormat("ddMMyyyy-HHmm");
117+
String dateTime = df.format(new Date());
118+
String host = params.getHostname();
119+
if(params.getValue("stage")!=null) {
120+
host = params.getValue("stage");
121+
}
122+
return "app_export_"+host+"_"+APIManagerAdapter.getCurrentUser(false).getLoginName()+"_"+dateTime+".csv";
123+
}
124+
125+
private void writeRecords(CSVPrinter csvPrinter, List<ClientApplication> apps, Wide wide) throws IOException, AppException {
126+
apps.sort(new ApplicationComparator());
127+
int i=0;
128+
for(ClientApplication app : apps) {
129+
if( i % 50 == 0 ){
130+
csvPrinter.flush();
131+
}
132+
// With wide - Report the application quotas
133+
if(wide.equals(Wide.wide)) {
134+
if(app.getAppQuota()!=null && app.getAppQuota().getRestrictions()!=null && app.getAppQuota().getRestrictions().size()!=0) {
135+
for(QuotaRestriction restriction : app.getAppQuota().getRestrictions()) {
136+
writeRecords(csvPrinter, app, null, restriction, wide);
137+
}
138+
} else {
139+
writeRecords(csvPrinter, app, null, null, wide);
140+
}
141+
142+
// With ultra - Report all subscribed APIs
143+
} else if(wide.equals(Wide.ultra)) {
144+
if(app.getApiAccess()!=null);
145+
app.getApiAccess().sort(new APIAccessComparator());
146+
for(APIAccess apiAccess : app.getApiAccess()) {
147+
writeRecords(csvPrinter, app, apiAccess, null, wide);
148+
}
149+
} else {
150+
writeRecords(csvPrinter, app, null, null, wide);
151+
}
152+
i++;
153+
}
154+
csvPrinter.flush();
155+
}
156+
157+
private void writeRecords(CSVPrinter csvPrinter, ClientApplication app, APIAccess apiAccess, QuotaRestriction restriction, Wide wide) throws IOException, AppException {
158+
switch(wide) {
159+
case standard:
160+
writeStandardToCSV(csvPrinter, app);
161+
break;
162+
case wide:
163+
writeWideToCSV(csvPrinter, app, restriction);
164+
break;
165+
case ultra:
166+
writeUltraToCSV(csvPrinter, app, apiAccess);
167+
break;
168+
default:
169+
break;
170+
}
171+
}
172+
173+
private void writeStandardToCSV(CSVPrinter csvPrinter, ClientApplication app) throws IOException, AppException {
174+
csvPrinter.printRecord(
175+
app.getId(),
176+
app.getOrganization().getName(),
177+
app.getName(),
178+
app.getEmail(),
179+
app.getPhone(),
180+
app.getState(),
181+
app.isEnabled()
182+
);
183+
}
184+
185+
private void writeWideToCSV(CSVPrinter csvPrinter, ClientApplication app, QuotaRestriction quotaRestriction) throws IOException, AppException {
186+
csvPrinter.printRecord(
187+
app.getId(),
188+
app.getOrganization().getName(),
189+
app.getName(),
190+
app.getEmail(),
191+
app.getPhone(),
192+
app.getState(),
193+
app.isEnabled(),
194+
getRestrictedAPI(quotaRestriction),
195+
getRestrictedMethod(quotaRestriction),
196+
getQuotaConfig(quotaRestriction)
197+
);
198+
}
199+
200+
private void writeUltraToCSV(CSVPrinter csvPrinter, ClientApplication app, APIAccess apiAccess) throws IOException, AppException {
201+
csvPrinter.printRecord(
202+
app.getId(),
203+
app.getOrganization().getName(),
204+
app.getName(),
205+
app.getEmail(),
206+
app.getPhone(),
207+
app.getState(),
208+
app.isEnabled(),
209+
apiAccess.getApiName(),
210+
apiAccess.getApiVersion(),
211+
getCreatedBy(apiAccess.getCreatedBy()),
212+
getCreatedOn(apiAccess.getCreatedOn())
213+
);
214+
}
215+
216+
private String getRestrictedAPI(QuotaRestriction quotaRestriction) throws AppException {
217+
if(quotaRestriction==null) return "N/A";
218+
API api = apiManager.apiAdapter.getAPIWithId(quotaRestriction.getApi());
219+
if(api==null) return "Err";
220+
return api.getName();
221+
}
222+
223+
private String getRestrictedMethod(QuotaRestriction quotaRestriction) throws AppException {
224+
if(quotaRestriction==null) return "N/A";
225+
API restrictedAPI = apiManager.apiAdapter.getAPIWithId(quotaRestriction.getApi());
226+
if(restrictedAPI==null) return "Err";
227+
return quotaRestriction.getMethod().equals("*") ? "All Methods" : apiManager.methodAdapter.getMethodForId(restrictedAPI.getId(), quotaRestriction.getMethod()).getName();
228+
}
229+
230+
private String getQuotaConfig(QuotaRestriction quotaRestriction) throws AppException {
231+
if(quotaRestriction==null) return "N/A";
232+
return ""+quotaRestriction.getConfig();
233+
}
234+
235+
private String getCreatedBy(String userId) {
236+
try {
237+
return apiManager.userAdapter.getUserForId(userId).getLoginName();
238+
} catch (AppException e) {
239+
LOG.error("Error getting createdBy user with Id: " + userId,e);
240+
return "Err";
241+
}
242+
}
243+
244+
private Date getCreatedOn(Long createdOn) {
245+
return new Date(createdOn);
246+
}
247+
248+
@Override
249+
public ClientAppFilter getFilter() throws AppException {
250+
Builder builder = getBaseFilterBuilder();
251+
252+
switch(params.getWide()) {
253+
case standard:
254+
case wide:
255+
builder.includeQuotas(true);
256+
break;
257+
case ultra:
258+
builder.includeAPIAccess(true);
259+
break;
260+
}
261+
ClientAppFilter filter = builder.build();
262+
return filter;
263+
}
264+
}

0 commit comments

Comments
 (0)