Update Batch from Testing
[aaf/authz.git] / auth / auth-batch / src / main / java / org / onap / aaf / auth / batch / actions / EmailPrint.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6  *
7  * Modifications Copyright © 2018 IBM.
8  * ===========================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END====================================================
21  *
22  */
23
24 package org.onap.aaf.auth.batch.actions;
25
26 import java.io.PrintStream;
27
28 import org.onap.aaf.auth.env.AuthzTrans;
29 import org.onap.aaf.auth.layer.Result;
30 import org.onap.aaf.auth.org.Organization;
31
32 public class EmailPrint extends Email {
33
34     private static final int LINE_LENGTH = 100;
35
36     public EmailPrint(String... defaultCC) {
37         super(defaultCC);
38     }
39
40     /* (non-Javadoc)
41      * @see org.onap.aaf.auth.actions.Email#exec(org.onap.aaf.auth.org.test.Organization, java.lang.StringBuilder)
42      */
43     @Override
44     protected Result<Void> exec(AuthzTrans trans, Organization org, StringBuilder msg) {
45         PrintStream out = System.out;
46         boolean first = true;
47         out.print("To: ");
48         for (String s: toList) {
49             if (first) {
50                 first = false;
51             }
52             else {out.print(',');}
53             out.print(s);
54         }
55         out.println();
56         
57         first = true;
58         out.print("CC: ");
59         for (String s: ccList) {
60             if (first) {
61                 first = false;
62             }
63             else {out.print(',');}
64             out.print(s);
65         }
66         out.println();
67
68         out.print("Subject: ");
69         out.println(subject);
70         out.println();
71         boolean go = true;
72         
73         for (int start=0, end=LINE_LENGTH;go;start=end,end=Math.min(msg.length(), start+LINE_LENGTH)) {
74             int ret = msg.indexOf("\n",start+1);
75             switch(ret) {
76                 case -1:
77                     out.println(msg.substring(start,end));
78                     break;
79                 case 0:
80                     end=start+1;
81                     out.println();
82                     break;
83                 default:
84                     if (ret<end) {
85                         end = ret;
86                     }
87                     if (end==start+LINE_LENGTH) {
88                         // Word-wrapping
89                         ret = msg.lastIndexOf(" ", end);
90                         if (ret>start && ret<end) {
91                             end=ret+1;
92                         }
93                         out.println(msg.substring(start,end));
94                     } else {
95                         out.print(msg.substring(start,end));
96                     }
97             }
98             go = end<msg.length();
99         }
100         return Result.ok();
101
102     }
103
104 }