2121import java .io .InputStream ;
2222import java .io .InputStreamReader ;
2323import java .util .ArrayList ;
24- import java .util .Arrays ;
2524import java .util .Collections ;
2625import java .util .List ;
2726import java .util .Map ;
3736import com .zimbra .cs .account .AccountServiceException ;
3837import com .zimbra .cs .account .Provisioning ;
3938import com .zimbra .cs .account .Server ;
40- import com .zimbra .cs .account .ZAttrServer ;
4139import com .zimbra .cs .account .accesscontrol .AdminRight ;
4240import com .zimbra .cs .account .accesscontrol .Rights .Admin ;
4341import com .zimbra .soap .ZimbraSoapContext ;
@@ -87,107 +85,104 @@ public void docRights(List<AdminRight> relatedRights, List<String> notes) {
8785 notes .add (String .format (AdminRightCheckPoint .Notes .MODIFY_ENTRY ,
8886 Admin .R_modifyServer .getName (), "server" ));
8987 }
90-
88+
89+ /**
90+ * Enable/disable postsrs service
91+ * @param server to check what services are available
92+ * @param attrs existing map to populate services
93+ * @return nothing
94+ */
9195 public void startOrStopPostSRSd (Server server , Map <String , Object > attrs ) throws ServiceException {
92-
93- ZimbraLog .mailbox .info ("==============================" );
94-
95- List <String > command = new ArrayList <>();
96- List <String > response = new ArrayList <>();
97- List <String > attrsUI = new ArrayList <>();
98- List <String > attrsLDAP = new ArrayList <>();
99- boolean UIWantsToEnablePostsrs = false ;
100- boolean isPostsrsEnabledInLDAP = false ;
101- final String POSTSRSD_EXE = LC .zimbra_home .value () + "/common/sbin/postsrsd" ;
102- final String POSTSRSD_SECRET = "/opt/zimbra/common/etc/postsrsd.secret" ;
103-
104- try {
105-
106- if (!attrs .isEmpty ()) {
107- Collections .addAll (attrsUI , (String []) attrs .get ("zimbraServiceEnabled" ));
108- ZimbraLog .mailbox .info ("attrsUI: " + attrsUI );
109- UIWantsToEnablePostsrs = attrsUI .contains ("postsrs" );
110- }
111-
112- if (!server .getAttrs ().isEmpty ()) {
113- Collections .addAll (attrsLDAP , server .getServiceEnabled ());
114- ZimbraLog .mailbox .info ("attrsLDAP: " + attrsLDAP );
115- isPostsrsEnabledInLDAP = attrsLDAP .contains ("postsrs" );
116- }
117-
118- if (UIWantsToEnablePostsrs && !isPostsrsEnabledInLDAP ) {
119- command = Stream .of (POSTSRSD_EXE , "-s" , POSTSRSD_SECRET , "-d" , "mydomain" , "-D" ).collect (Collectors .toList ());
120- response = executeLinuxCommand (command );
121- ZimbraLog .mailbox .info (response );
122- ZimbraLog .mailbox .info ("postsrsd has been enabled" );
123- }
124- else if (UIWantsToEnablePostsrs && isPostsrsEnabledInLDAP ) {
125- ZimbraLog .mailbox .info ("postsrsd is already enabled" );
126- }
127- else if (!UIWantsToEnablePostsrs && isPostsrsEnabledInLDAP ) {
128- // There is no command to disable SRS so far. The only way is killing the process.
129- command = Stream .of ("pgrep" , "-f" , "postsrsd" ).collect (Collectors .toList ());
130- response = executeLinuxCommand (command );
131- ZimbraLog .mailbox .info ("response: " + response );
132- if (response .isEmpty ()) {
133- ZimbraLog .mailbox .info ("postsrsd is already disabled" );
134- }
135- else {
136- String postSrsdPID = response .get (0 );
137- command .clear ();
138- response .clear ();
139- command = Stream .of ("kill" , "-9" , postSrsdPID ).collect (Collectors .toList ());
140- response = executeLinuxCommand (command );
141- ZimbraLog .mailbox .info ("response: " + response );
142- ZimbraLog .mailbox .info ("postsrsd has been disabled" );
143- }
144- }
145- else if (!UIWantsToEnablePostsrs && !isPostsrsEnabledInLDAP ) {
146- ZimbraLog .mailbox .info ("postsrsd is already disabled" );
147- }
148-
149- } catch (IOException e ) {
150- ZimbraLog .mailbox .warn (e );
151- } catch (InterruptedException e ) {
152- ZimbraLog .mailbox .warn (e );
153- }
154-
155-
156- ZimbraLog .mailbox .info ("==============================" );
157-
96+
97+ List <String > command = new ArrayList <>();
98+ List <String > response = new ArrayList <>();
99+ List <String > attrsUI = new ArrayList <>();
100+ List <String > attrsLDAP = new ArrayList <>();
101+ boolean UIWantsToEnablePostsrs = false ;
102+ boolean isPostsrsEnabledInLDAP = false ;
103+ final String POSTSRSD_SECRET = "/opt/zimbra/common/etc/postsrsd.secret" ;
104+ final String POSTSRSD_EXE = LC .zimbra_home .value () + "/common/sbin/postsrsd" ;
105+
106+ try {
107+ if (!attrs .isEmpty ()) {
108+ Collections .addAll (attrsUI , (String []) attrs .get (Provisioning .A_zimbraServiceEnabled ));
109+ ZimbraLog .mailbox .info ("attrsUI: " + attrsUI );
110+ UIWantsToEnablePostsrs = attrsUI .contains ("postsrs" );
111+ }
112+
113+ if (!server .getAttrs ().isEmpty ()) {
114+ Collections .addAll (attrsLDAP , server .getServiceEnabled ());
115+ ZimbraLog .mailbox .info ("attrsLDAP: " + attrsLDAP );
116+ isPostsrsEnabledInLDAP = attrsLDAP .contains ("postsrs" );
117+ }
118+
119+ if (UIWantsToEnablePostsrs && !isPostsrsEnabledInLDAP ) {
120+ command = Stream .of (POSTSRSD_EXE , "-s" , POSTSRSD_SECRET , "-d" , server .getName (), "-D" )
121+ .collect (Collectors .toList ());
122+ response = executeLinuxCommand (command );
123+ ZimbraLog .mailbox .info (response );
124+ ZimbraLog .mailbox .info ("postsrsd has been enabled" );
125+ } else if (UIWantsToEnablePostsrs && isPostsrsEnabledInLDAP ) {
126+ ZimbraLog .mailbox .info ("postsrsd is already enabled" );
127+ } else if (!UIWantsToEnablePostsrs && isPostsrsEnabledInLDAP ) {
128+ // There is no command to disable SRS so far. The only way is killing the
129+ // process.
130+ command = Stream .of ("pgrep" , "-f" , "postsrsd" ).collect (Collectors .toList ());
131+ response = executeLinuxCommand (command );
132+ ZimbraLog .mailbox .info ("response: " + response );
133+ if (response .isEmpty ()) {
134+ ZimbraLog .mailbox .info ("postsrsd is already disabled" );
135+ } else {
136+ String postSrsdPID = response .get (0 );
137+ command .clear ();
138+ response .clear ();
139+ command = Stream .of ("kill" , "-9" , postSrsdPID ).collect (Collectors .toList ());
140+ response = executeLinuxCommand (command );
141+ ZimbraLog .mailbox .info ("response: " + response );
142+ ZimbraLog .mailbox .info ("postsrsd has been disabled" );
143+ }
144+ } else if (!UIWantsToEnablePostsrs && !isPostsrsEnabledInLDAP ) {
145+ ZimbraLog .mailbox .info ("postsrsd is already disabled" );
146+ }
147+ } catch (IOException e ) {
148+ ZimbraLog .mailbox .warn (e );
149+ } catch (InterruptedException e ) {
150+ ZimbraLog .mailbox .warn (e );
151+ }
158152 }
159-
153+
154+ /**
155+ * Execute linux command
156+ * @param command to be executed
157+ * @return list of string
158+ */
160159 public List <String > executeLinuxCommand (List <String > command ) throws IOException , InterruptedException {
161160
162- InputStream is = null ;
163- ProcessBuilder pb = null ;
164- Process ps = null ;
165- List <String > lines = new ArrayList <>();
166-
167- ZimbraLog .mailbox .info ("command: " + command );
168-
169- // Executing the linux command
170- pb = new ProcessBuilder (command );
171- ps = pb .start ();
172- int exitValue = ps .waitFor ();
173- ZimbraLog .mailbox .info ("command executed" );
174- // Getting executed command response as List
175- if (exitValue == 0 ) {
176- is = ps .getInputStream ();
177- }
178- else {
179- is = ps .getErrorStream ();
180- }
181- InputStreamReader isr = new InputStreamReader (is );
182- BufferedReader br = new BufferedReader (isr );
183- String line ;
184- while ((line = br .readLine ()) != null ) {
185- lines .add (line );
186- }
187- ps .destroy ();
188-
189- return lines ;
190-
191- }
192-
193- }
161+ InputStream is = null ;
162+ ProcessBuilder pb = null ;
163+ Process ps = null ;
164+ List <String > lines = new ArrayList <>();
165+
166+ ZimbraLog .mailbox .info ("command: " + command );
167+ // Executing the linux command
168+ pb = new ProcessBuilder (command );
169+ ps = pb .start ();
170+ int exitValue = ps .waitFor ();
171+ ZimbraLog .mailbox .info ("command executed" );
172+ // Getting executed command response as List
173+ if (exitValue == 0 ) {
174+ is = ps .getInputStream ();
175+ } else {
176+ is = ps .getErrorStream ();
177+ }
178+ InputStreamReader isr = new InputStreamReader (is );
179+ BufferedReader br = new BufferedReader (isr );
180+ String line ;
181+ while ((line = br .readLine ()) != null ) {
182+ lines .add (line );
183+ }
184+ ps .destroy ();
185+
186+ return lines ;
187+ }
188+ }
0 commit comments