|
| 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