diff options
11 files changed, 458 insertions, 62 deletions
diff --git a/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/actions/EmailPrint.java b/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/actions/EmailPrint.java index 3ab481f5..2ae86994 100644 --- a/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/actions/EmailPrint.java +++ b/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/actions/EmailPrint.java @@ -3,6 +3,8 @@ * org.onap.aaf * =========================================================================== * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * + * Modifications Copyright © 2018 IBM. * =========================================================================== * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -44,7 +46,9 @@ public class EmailPrint extends Email { boolean first = true; out.print("To: "); for (String s: toList) { - if (first) {first = false;} + if (first) { + first = false; + } else {out.print(',');} out.print(s); } @@ -53,7 +57,9 @@ public class EmailPrint extends Email { first = true; out.print("CC: "); for (String s: ccList) { - if (first) {first = false;} + if (first) { + first = false; + } else {out.print(',');} out.print(s); } diff --git a/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/helpers/ExpireRange.java b/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/helpers/ExpireRange.java index a985640b..c459dc66 100644 --- a/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/helpers/ExpireRange.java +++ b/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/helpers/ExpireRange.java @@ -3,6 +3,8 @@ * org.onap.aaf * =========================================================================== * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * + * Modifications Copyright © 2018 IBM. * =========================================================================== * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -30,20 +32,19 @@ import java.util.Map; import java.util.Set; import org.onap.aaf.cadi.Access; -import org.onap.aaf.cadi.PropAccess; public class ExpireRange { private static final String AAF_BATCH_RANGE = "aaf_batch_range."; public Map<String,List<Range>> ranges; public final Date now; + public String rangeOneMonth = "OneMonth"; public ExpireRange(final Access access) { now = new Date(); ranges = new HashMap<>(); int i=0; String prop = access.getProperty(AAF_BATCH_RANGE + i,null); - if(prop==null) { - if(i==0) { + if(prop==null && i==0) { List<Range> lcred = getRangeList("cred"); List<Range> lur = getRangeList("ur"); List<Range> lx509 = getRangeList("x509"); @@ -55,14 +56,13 @@ public class ExpireRange { lcred.add(new Range("CredOneWeek",3,1,0,0,GregorianCalendar.WEEK_OF_MONTH,1)); lcred.add(new Range("CredTwoWeek",2,1,GregorianCalendar.WEEK_OF_MONTH,1,GregorianCalendar.WEEK_OF_MONTH,2)); - lcred.add(new Range("OneMonth",1,7,GregorianCalendar.WEEK_OF_MONTH,2,GregorianCalendar.MONTH,1)); + lcred.add(new Range(rangeOneMonth,1,7,GregorianCalendar.WEEK_OF_MONTH,2,GregorianCalendar.MONTH,1)); lcred.add(new Range("TwoMonth",1,0,GregorianCalendar.MONTH,1,GregorianCalendar.MONTH,2)); - lur.add(new Range("OneMonth",1,7,GregorianCalendar.WEEK_OF_MONTH,2,GregorianCalendar.MONTH,1)); + lur.add(new Range(rangeOneMonth,1,7,GregorianCalendar.WEEK_OF_MONTH,2,GregorianCalendar.MONTH,1)); - lx509.add(new Range("OneMonth",1,7,GregorianCalendar.WEEK_OF_MONTH,2,GregorianCalendar.MONTH,1)); + lx509.add(new Range(rangeOneMonth,1,7,GregorianCalendar.WEEK_OF_MONTH,2,GregorianCalendar.MONTH,1)); } - } } public Set<String> names() { diff --git a/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/helpers/MiscID.java b/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/helpers/MiscID.java index 45875a20..e30336fb 100644 --- a/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/helpers/MiscID.java +++ b/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/helpers/MiscID.java @@ -4,6 +4,8 @@ * =========================================================================== * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. * =========================================================================== + * Modifications Copyright (C) 2018 IBM. + * =========================================================================== * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -47,9 +49,12 @@ public class MiscID { CREATE_DATE - Date when MiscID was created LAST_RENEWAL_DATE - Date when MiscID Sponsorship was last renewed */ - public String id,sponsor,created,renewal; + public String id; + public String sponsor; + public String created; + public String renewal; - private static final String fieldString = "id,created,sponsor,renewal"; + private static final String FIELD_STRING = "id,created,sponsor,renewal"; /** * Load a Row of Strings (from CSV file). @@ -63,8 +68,10 @@ public class MiscID { * @throws IllegalAccessException * @throws IllegalArgumentException */ - public void set(String row []) throws BatchException { - if (row.length<4) {throw new BatchException("Row of MiscID_XRef is too short");} + public void set(String[] row ) throws BatchException { + if (row.length<4) { + throw new BatchException("Row of MiscID_XRef is too short"); + } id = row[0]; sponsor = row[1]; created = row[2]; @@ -80,15 +87,15 @@ public class MiscID { public static void load(Trans trans, Session session ) { - load(trans, session,"SELECT " + fieldString + " FROM authz.miscid;",data); + load(trans, session,"SELECT " + FIELD_STRING + " FROM authz.miscid;",data); } public static void load(Trans trans, Session session, Map<String,MiscID> map ) { - load(trans, session,"SELECT " + fieldString + " FROM authz.miscid;",map); + load(trans, session,"SELECT " + FIELD_STRING + " FROM authz.miscid;",map); } public static void loadOne(Trans trans, Session session, String id ) { - load(trans, session,"SELECT " + fieldString + " FROM authz.miscid WHERE id ='" + id + "';", data); + load(trans, session,"SELECT " + FIELD_STRING + " FROM authz.miscid WHERE id ='" + id + "';", data); } public static void load(Trans trans, Session session, String query, Map<String,MiscID> map) { @@ -141,7 +148,7 @@ public class MiscID { public StringBuilder insertStmt() throws IllegalArgumentException, IllegalAccessException { StringBuilder sb = new StringBuilder("INSERT INTO authz.miscid ("); - sb.append(fieldString); + sb.append(FIELD_STRING); sb.append(") VALUES ('"); sb.append(id); sb.append("','"); diff --git a/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/helpers/MonthData.java b/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/helpers/MonthData.java index 35ad6a10..b0499a2d 100644 --- a/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/helpers/MonthData.java +++ b/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/helpers/MonthData.java @@ -65,16 +65,17 @@ public class MonthData { } } - public void add(int yr_mon, String target, long total, long adds, long drops) { - Set<Row> row = data.get(yr_mon); + public void add(int yrMon, String target, long total, long adds, long drops) { + Set<Row> row = data.get(yrMon); if (row==null) { - data.put(yr_mon, (row=new HashSet<>())); + row=new HashSet<>(); + data.put(yrMon, row); } row.add(new Row(target,total,adds,drops)); } - public boolean notExists(int yr_mon) { - return data.get(yr_mon)==null; + public boolean notExists(int yrMon) { + return data.get(yrMon)==null; } public static class Row implements Comparable<Row> { diff --git a/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/helpers/Notification.java b/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/helpers/Notification.java index 5c7a8269..ac6cb67d 100644 --- a/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/helpers/Notification.java +++ b/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/helpers/Notification.java @@ -4,6 +4,8 @@ * =========================================================================== * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. * =========================================================================== + * Modifications Copyright (C) 2018 IBM. + * =========================================================================== * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -77,7 +79,7 @@ public class Notification { public final String user; public final TYPE type; public Date last; - public int checksum; + public int checkSum; public Message msg; private int current; public Organization org; @@ -87,7 +89,7 @@ public class Notification { this.user = user; this.type = nt; this.last = last; - this.checksum = checksum; + this.checkSum = checksum; current = 0; count = 0; } @@ -147,11 +149,11 @@ public class Notification { @Override public Notification create(Row row) { int idx =row.getInt(1); - TYPE type = TYPE.get(idx); - if (type==null) { + TYPE typeCreator = TYPE.get(idx); + if (typeCreator==null) { return null; } - return new Notification(row.getString(0), type, row.getTimestamp(2), row.getInt(3)); + return new Notification(row.getString(0), typeCreator, row.getTimestamp(2), row.getInt(3)); } @Override @@ -180,7 +182,7 @@ public class Notification { public boolean update(AuthzTrans trans, Session session, boolean dryRun) { checksum(); - if (last==null || current==0 || current!=checksum) { + if (last==null || current==0 || current!=checkSum) { last = now; current = checksum(); String update = "UPDATE authz.notify SET " + @@ -204,6 +206,6 @@ public class Notification { public String toString() { return "\"" + user + "\",\"" + type.name() + "\",\"" - + Chrono.dateTime(last)+ "\", " + checksum; + + Chrono.dateTime(last)+ "\", " + checkSum; } }
\ No newline at end of file diff --git a/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/helpers/Perm.java b/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/helpers/Perm.java index 21bd71ee..acf54274 100644 --- a/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/helpers/Perm.java +++ b/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/helpers/Perm.java @@ -3,6 +3,8 @@ * org.onap.aaf * =========================================================================== * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * + * Modifications Copyright © 2018 IBM. * =========================================================================== * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -42,9 +44,24 @@ public class Perm implements Comparable<Perm> { public static final TreeMap<String,Perm> keys = new TreeMap<>(); private static List<Perm> deletePerms = new ArrayList<>(); - public final String ns, type, instance, action,description; - private String fullType = null, fullPerm = null, encode = null; + public final String ns; + public final String type; + public final String instance; + public final String action; + public final String description; + private String fullType = null; + private String fullPerm = null; + private String encode = null; public final Set<String> roles; + + public Perm(String ns, String type, String instance, String action, String description, Set<String> roles) { + this.ns = ns; + this.type = type; + this.instance = instance; + this.action = action; + this.description = description; + this.roles = roles; + } public String encode() { if (encode == null) { @@ -66,17 +83,6 @@ public class Perm implements Comparable<Perm> { } return fullPerm; } - - public Perm(String ns, String type, String instance, String action, String description, Set<String> roles) { - this.ns = ns; - this.type = type; - this.instance = instance; - this.action = action; - this.description = description; - // 2.0.11 -// this.full = encode();//ns+'.'+type+'|'+instance+'|'+action; - this.roles = roles; - } public static void load(Trans trans, Session session) { load(trans, session, "select ns, type, instance, action, description, roles from authz.perm;"); diff --git a/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/helpers/X509.java b/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/helpers/X509.java index 0ffaa8f5..3cbf90fa 100644 --- a/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/helpers/X509.java +++ b/auth/auth-batch/src/main/java/org/onap/aaf/auth/batch/helpers/X509.java @@ -3,6 +3,8 @@ * org.onap.aaf * =========================================================================== * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * + * Modifications Copyright © 2018 IBM. * =========================================================================== * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,7 +23,6 @@ package org.onap.aaf.auth.batch.helpers; -import java.io.IOException; import java.nio.ByteBuffer; import java.security.cert.X509Certificate; import java.util.Iterator; @@ -42,7 +43,10 @@ import com.datastax.driver.core.SimpleStatement; import com.datastax.driver.core.Statement; public class X509 { - public final String ca,id,x500,x509; + public final String ca; + public final String id; + public final String x500; + public final String x509; public ByteBuffer serial; public X509(String ca, String id, String x500, String x509, ByteBuffer serial) { @@ -104,12 +108,12 @@ public class X509 { } - public void row(CSV.Writer cw, X509Certificate x509Cert) throws IOException { + public void row(CSV.Writer cw, X509Certificate x509Cert) { cw.row("x509",ca,Hash.toHex(serial.array()),Chrono.dateOnlyStamp(x509Cert.getNotAfter()),x500); } - public static void row(StringBuilder sb, List<String> row) throws IOException { + public static void row(StringBuilder sb, List<String> row) { sb.append("DELETE from authz.x509 WHERE ca='"); sb.append(row.get(1)); sb.append("' AND serial="); diff --git a/auth/auth-certman/src/main/java/org/onap/aaf/auth/cm/validation/CertmanValidator.java b/auth/auth-certman/src/main/java/org/onap/aaf/auth/cm/validation/CertmanValidator.java index 1a27b41f..bb157a2e 100644 --- a/auth/auth-certman/src/main/java/org/onap/aaf/auth/cm/validation/CertmanValidator.java +++ b/auth/auth-certman/src/main/java/org/onap/aaf/auth/cm/validation/CertmanValidator.java @@ -78,22 +78,6 @@ public class CertmanValidator extends Validator{ return this; } - public CertmanValidator artisKeys(List<ArtiDAO.Data> list, int min) { - if (list==null) { - msg(ARTIFACT_LIST_IS_NULL); - } else { - if (list.size()<min) { - msg(ARTIFACTS_MUST_HAVE_AT_LEAST + min + ENTR + (min==1?Y:IES)); - } else { - for (ArtiDAO.Data a : list) { - keys(a); - } - } - } - return this; - } - - public CertmanValidator keys(ArtiDAO.Data add) { if (add==null) { msg("Artifact is null."); diff --git a/auth/auth-certman/src/test/java/org/onap/aaf/auth/cm/validation/JU_CertmanValidator.java b/auth/auth-certman/src/test/java/org/onap/aaf/auth/cm/validation/JU_CertmanValidator.java new file mode 100644 index 00000000..4aa3d6d3 --- /dev/null +++ b/auth/auth-certman/src/test/java/org/onap/aaf/auth/cm/validation/JU_CertmanValidator.java @@ -0,0 +1,114 @@ +/** + * ============LICENSE_START==================================================== + * org.onap.aaf + * =========================================================================== + * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * =========================================================================== + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END==================================================== + * + */ +package org.onap.aaf.auth.cm.validation; + +import org.junit.Test; +import org.onap.aaf.auth.dao.cass.ArtiDAO; + +import static com.google.common.collect.Lists.newArrayList; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; + +public class JU_CertmanValidator { + + private static final String COLLECTION_NAME = "collection_name"; + private static final int MIN_SIZE = 3; + private CertmanValidator certmanValidator = new CertmanValidator(); + + + @Test + public void nullBlankMin_shouldReportErrorWhenListIsNull() { + + certmanValidator.nullBlankMin(COLLECTION_NAME, null, MIN_SIZE); + assertEquals(COLLECTION_NAME + " is null.\n", certmanValidator.errs()); + } + + @Test + public void nullBlankMin_shouldReportErrorWhenListHasNotEnoughElements() { + + certmanValidator.nullBlankMin(COLLECTION_NAME, newArrayList("one", "two"), MIN_SIZE); + assertEquals(COLLECTION_NAME + " must have at least " + MIN_SIZE + " entries.\n", certmanValidator.errs()); + } + + @Test + public void nullBlankMin_shouldReportErrorWhenListContainsNullOrEmptyElements() { + + certmanValidator.nullBlankMin(COLLECTION_NAME, newArrayList("one", "", "three"), MIN_SIZE); + assertEquals("List Item is blank.\n", certmanValidator.errs()); + } + + @Test + public void nullBlankMin_shouldPassValidation() { + + certmanValidator.nullBlankMin(COLLECTION_NAME, newArrayList("one", "two", "three"), MIN_SIZE); + assertFalse(certmanValidator.err()); + } + + @Test + public void artisRequired_shouldReportErrorWhenListIsNull() { + + certmanValidator.artisRequired(null, MIN_SIZE); + assertEquals("Artifact List is null.\n", certmanValidator.errs()); + } + + @Test + public void artisRequired_shouldReportErrorWhenListHasNotEnoughElements() { + + certmanValidator.artisRequired(newArrayList(newArtifactData(), newArtifactData()), MIN_SIZE); + assertEquals("Artifacts must have at least " + MIN_SIZE + " entries.\n", certmanValidator.errs()); + } + + @Test + public void artisRequired_shouldReportErrorWhenArtifactDoesNotHaveAllRequiredFields() { + + certmanValidator.artisRequired(newArrayList(newArtifactData("id", "", "ca", "dir", "user")), 1); + assertEquals("machine is blank.\n", certmanValidator.errs()); + } + + @Test + public void keys_shouldReportErrorWhenArtifactIsNull() { + + certmanValidator.keys(null); + assertEquals("Artifact is null.\n", certmanValidator.errs()); + } + + @Test + public void keys_shouldReportErrorWhenArtifactDoesNotHaveAllRequiredFields() { + + certmanValidator.keys(newArtifactData("", "", "ca", "dir", "user")); + assertEquals("mechid is blank.\n" + "machine is blank.\n", certmanValidator.errs()); + } + + private ArtiDAO.Data newArtifactData() { + return new ArtiDAO.Data(); + } + + private ArtiDAO.Data newArtifactData(String mechId, String machine, String ca, String dir, String user) { + ArtiDAO.Data artifact = new ArtiDAO.Data(); + artifact.mechid = mechId; + artifact.machine = machine; + artifact.ca = ca; + artifact.dir = dir; + artifact.os_user = user; + return artifact; + + } +}
\ No newline at end of file diff --git a/misc/env/src/test/java/org/onap/aaf/misc/env/jaxb/JAXBObjectifier.java b/misc/env/src/test/java/org/onap/aaf/misc/env/jaxb/JAXBObjectifier.java new file mode 100644 index 00000000..ac0e859c --- /dev/null +++ b/misc/env/src/test/java/org/onap/aaf/misc/env/jaxb/JAXBObjectifier.java @@ -0,0 +1,135 @@ +/** + * ============LICENSE_START==================================================== + * org.onap.aaf + * =========================================================================== + * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * =========================================================================== + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END==================================================== + * + */ + +package org.onap.aaf.misc.env.jaxb; + +import java.io.InputStream; +import java.io.Reader; + +import javax.xml.bind.JAXBException; +import javax.xml.validation.Schema; + +import org.onap.aaf.misc.env.APIException; +import org.onap.aaf.misc.env.Env; +import org.onap.aaf.misc.env.TimeTaken; +import org.onap.aaf.misc.env.old.IOObjectifier; + +/** + * Allow Extended IO interface usage without muddying up the Stringifier Interface + */ +public class JAXBObjectifier<T> implements IOObjectifier<T> { + private JAXBumar jumar; + + public JAXBObjectifier(Schema schema, Class<?>... classes) throws APIException { + try { + jumar = new JAXBumar(schema, classes); + } catch (JAXBException e) { + throw new APIException(e); + } + } + + public JAXBObjectifier(Class<?>... classes) throws APIException { + try { + jumar = new JAXBumar(classes); + } catch (JAXBException e) { + throw new APIException(e); + } + } + + // package on purpose + JAXBObjectifier(JAXBumar jumar) { + this.jumar = jumar; + } + + @SuppressWarnings("unchecked") + // @Override + public T objectify(Env env, String input) throws APIException { + TimeTaken tt = env.start("JAXB Unmarshal", Env.XML); + try { + tt.size(input.length()); + return (T)jumar.unmarshal(env.debug(), input); + } catch (JAXBException e) { + throw new APIException(e); + } finally { + tt.done(); + } + } + + @SuppressWarnings("unchecked") + // @Override + public T objectify(Env env, Reader rdr) throws APIException { + //TODO create a Reader that Counts? + TimeTaken tt = env.start("JAXB Unmarshal", Env.XML); + try { + return (T)jumar.unmarshal(env.debug(), rdr); + } catch (JAXBException e) { + throw new APIException(e); + } finally { + tt.done(); + } + } + + + @SuppressWarnings("unchecked") + // @Override + public T objectify(Env env, InputStream is) throws APIException { + //TODO create a Reader that Counts? + TimeTaken tt = env.start("JAXB Unmarshal", Env.XML); + try { + return (T)jumar.unmarshal(env.debug(), is); + } catch (JAXBException e) { + throw new APIException(e); + } finally { + tt.done(); + } + } + + + public void servicePrestart(Env env) throws APIException { + } + + public void threadPrestart(Env env) throws APIException { + } + + // // @Override + public void refresh(Env env) throws APIException { + } + + // // @Override + public void threadDestroy(Env env) throws APIException { + } + + // // @Override + public void serviceDestroy(Env env) throws APIException { + } + + + @SuppressWarnings("unchecked") + public T newInstance() throws APIException { + try { + return (T)jumar.newInstance(); + } catch (Exception e) { + throw new APIException(e); + } + } + +} + diff --git a/misc/env/src/test/java/org/onap/aaf/misc/env/jaxb/JAXBStringifier.java b/misc/env/src/test/java/org/onap/aaf/misc/env/jaxb/JAXBStringifier.java new file mode 100644 index 00000000..9b8a2c97 --- /dev/null +++ b/misc/env/src/test/java/org/onap/aaf/misc/env/jaxb/JAXBStringifier.java @@ -0,0 +1,137 @@ +/** + * ============LICENSE_START==================================================== + * org.onap.aaf + * =========================================================================== + * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * =========================================================================== + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END==================================================== + * + */ + +package org.onap.aaf.misc.env.jaxb; + +import java.io.OutputStream; +import java.io.StringWriter; +import java.io.Writer; + +import javax.xml.bind.JAXBException; +import javax.xml.namespace.QName; + +import org.onap.aaf.misc.env.APIException; +import org.onap.aaf.misc.env.Env; +import org.onap.aaf.misc.env.TimeTaken; +import org.onap.aaf.misc.env.old.IOStringifier; + +public class JAXBStringifier<T> implements IOStringifier<T> { + private JAXBmar jmar; + + public JAXBStringifier(Class<?>... classes) throws APIException { + try { + jmar = new JAXBmar(classes); + } catch (JAXBException e) { + throw new APIException(e); + } + } + + public JAXBStringifier(QName qname, Class<?>... classes) + throws APIException { + try { + jmar = new JAXBmar(qname, classes); + } catch (JAXBException e) { + throw new APIException(e); + } + } + + // package on purpose + JAXBStringifier(JAXBmar jmar) { + this.jmar = jmar; + } + + // // @Override + public void stringify(Env env, T input, Writer writer, boolean ... options) + throws APIException { + TimeTaken tt = env.start("JAXB Marshal", Env.XML); + try { + jmar.marshal(env.debug(), input, writer, options); + } catch (JAXBException e) { + throw new APIException(e); + } finally { + tt.done(); + } + } + + // @Override + public void stringify(Env env, T input, OutputStream os, boolean ... options) + throws APIException { + // TODO create an OutputStream that Counts? + TimeTaken tt = env.start("JAXB Marshal", Env.XML); + try { + jmar.marshal(env.debug(), input, os, options); + } catch (JAXBException e) { + throw new APIException(e); + } finally { + tt.done(); + } + } + + // @Override + public String stringify(Env env, T input, boolean ... options) throws APIException { + TimeTaken tt = env.start("JAXB Marshal", Env.XML); + StringWriter sw = new StringWriter(); + try { + jmar.marshal(env.debug(), input, sw, options); + String rv = sw.toString(); + tt.size(rv.length()); + return rv; + } catch (JAXBException e) { + tt.size(0); + throw new APIException(e); + } finally { + tt.done(); + } + } + + // // @Override + public void servicePrestart(Env env) throws APIException { + } + + // // @Override + public void threadPrestart(Env env) throws APIException { + } + + // // @Override + public void refresh(Env env) throws APIException { + } + + // // @Override + public void threadDestroy(Env env) throws APIException { + } + + // // @Override + public void serviceDestroy(Env env) throws APIException { + } + + // @Override + public JAXBStringifier<T> pretty(boolean pretty) { + jmar.pretty(pretty); + return this; + } + + // @Override + public JAXBStringifier<T> asFragment(boolean fragment) { + jmar.asFragment(fragment); + return this; + } + +} |