2828import com .tw .go .plugin .provider .Provider ;
2929import com .tw .go .plugin .setting .PluginSettings ;
3030import com .tw .go .plugin .util .JSONUtils ;
31- import org .apache .commons .io .IOUtils ;
32- import org .apache .commons .lang3 .StringUtils ;
31+ import com .tw .go .plugin .util .ValidationUtils ;
3332
3433import java .io .IOException ;
34+ import java .io .InputStream ;
35+ import java .nio .charset .StandardCharsets ;
3536import java .util .*;
3637
37- import static java .util .Arrays .asList ;
38-
3938public abstract class BuildStatusNotifierPlugin implements GoPlugin {
40- private static Logger LOGGER = Logger .getLoggerFor (BuildStatusNotifierPlugin .class );
39+ private static final Logger LOGGER = Logger .getLoggerFor (BuildStatusNotifierPlugin .class );
4140
4241 public static final String EXTENSION_NAME = "notification" ;
43- public static final List <String > goSupportedVersions = asList ("1.0" );
42+ public static final List <String > goSupportedVersions = List . of ("1.0" );
4443
4544 public static final String PLUGIN_SETTINGS_GET_CONFIGURATION = "go.plugin-settings.get-configuration" ;
4645 public static final String PLUGIN_SETTINGS_GET_VIEW = "go.plugin-settings.get-view" ;
@@ -75,20 +74,21 @@ public void initializeGoApplicationAccessor(GoApplicationAccessor goApplicationA
7574 @ Override
7675 public GoPluginApiResponse handle (GoPluginApiRequest goPluginApiRequest ) {
7776 String requestName = goPluginApiRequest .requestName ();
78- if (requestName .equals (PLUGIN_SETTINGS_GET_CONFIGURATION )) {
79- return handleGetPluginSettingsConfiguration ();
80- } else if (requestName .equals (PLUGIN_SETTINGS_GET_VIEW )) {
81- try {
82- return handleGetPluginSettingsView ();
83- } catch (IOException e ) {
84- return renderJSON (500 , String .format ("Failed to find template: %s" , e .getMessage ()));
85- }
86- } else if (requestName .equals (PLUGIN_SETTINGS_VALIDATE_CONFIGURATION )) {
87- return handleValidatePluginSettingsConfiguration (goPluginApiRequest );
88- } else if (requestName .equals (REQUEST_NOTIFICATIONS_INTERESTED_IN )) {
89- return handleNotificationsInterestedIn ();
90- } else if (requestName .equals (REQUEST_STAGE_STATUS )) {
91- return handleStageNotification (goPluginApiRequest );
77+ switch (requestName ) {
78+ case PLUGIN_SETTINGS_GET_CONFIGURATION :
79+ return handleGetPluginSettingsConfiguration ();
80+ case PLUGIN_SETTINGS_GET_VIEW :
81+ try {
82+ return handleGetPluginSettingsView ();
83+ } catch (IOException e ) {
84+ return renderJSON (500 , String .format ("Failed to find template: %s" , e .getMessage ()));
85+ }
86+ case PLUGIN_SETTINGS_VALIDATE_CONFIGURATION :
87+ return handleValidatePluginSettingsConfiguration (goPluginApiRequest );
88+ case REQUEST_NOTIFICATIONS_INTERESTED_IN :
89+ return handleNotificationsInterestedIn ();
90+ case REQUEST_STAGE_STATUS :
91+ return handleStageNotification (goPluginApiRequest );
9292 }
9393 return renderJSON (NOT_FOUND_RESPONSE_CODE , null );
9494 }
@@ -103,67 +103,72 @@ private GoPluginApiResponse handleGetPluginSettingsConfiguration() {
103103 }
104104
105105 private GoPluginApiResponse handleGetPluginSettingsView () throws IOException {
106- Map <String , Object > response = new HashMap <String , Object >();
106+ Map <String , Object > response = new HashMap <>();
107107
108- response .put ("template" , IOUtils .toString (getClass ().getResourceAsStream ("/" + provider .configurationView ().templateName ()), "UTF-8" ));
109- return renderJSON (SUCCESS_RESPONSE_CODE , response );
108+ try (InputStream is = Objects .requireNonNull (getClass ().getResourceAsStream ("/" + provider .configurationView ().templateName ()))) {
109+ response .put ("template" , new String (is .readAllBytes (), StandardCharsets .UTF_8 ));
110+ return renderJSON (SUCCESS_RESPONSE_CODE , response );
111+ }
110112 }
111113
114+ @ SuppressWarnings ("unchecked" )
112115 private GoPluginApiResponse handleValidatePluginSettingsConfiguration (GoPluginApiRequest goPluginApiRequest ) {
113116 Map <String , Object > fields = (Map <String , Object >) JSONUtils .fromJSON (goPluginApiRequest .requestBody ());
114117 List <Map <String , Object >> response = provider .validateConfig ((Map <String , Object >) fields .get ("plugin-settings" ));
115118 return renderJSON (SUCCESS_RESPONSE_CODE , response );
116119 }
117120
118121
122+ @ SuppressWarnings ("unchecked" )
119123 public PluginSettings getPluginSettings () {
120- Map <String , Object > requestMap = new HashMap <String , Object >();
124+ Map <String , Object > requestMap = new HashMap <>();
121125 requestMap .put ("plugin-id" , provider .pluginId ());
122126 GoApiResponse response = goApplicationAccessor .submit (createGoApiRequest (GET_PLUGIN_SETTINGS , JSONUtils .toJSON (requestMap )));
123- Map <String , String > responseBodyMap = response .responseBody () == null ? new HashMap <String , String >() : (Map <String , String >) JSONUtils .fromJSON (response .responseBody ());
127+ Map <String , String > responseBodyMap = response .responseBody () == null ? new HashMap <>() : (Map <String , String >) JSONUtils .fromJSON (response .responseBody ());
124128 return provider .pluginSettings (responseBodyMap );
125129 }
126130
127131 GoPluginApiResponse handleNotificationsInterestedIn () {
128- Map <String , Object > response = new HashMap <String , Object >();
129- response .put ("notifications" , Arrays . asList (REQUEST_STAGE_STATUS ));
132+ Map <String , Object > response = new HashMap <>();
133+ response .put ("notifications" , List . of (REQUEST_STAGE_STATUS ));
130134 return renderJSON (SUCCESS_RESPONSE_CODE , response );
131135 }
132136
137+ @ SuppressWarnings ("unchecked" )
133138 GoPluginApiResponse handleStageNotification (GoPluginApiRequest goPluginApiRequest ) {
134139 Map <String , Object > dataMap = (Map <String , Object >) JSONUtils .fromJSON (goPluginApiRequest .requestBody ());
135140
136141 int responseCode = SUCCESS_RESPONSE_CODE ;
137- Map <String , Object > response = new HashMap <String , Object >();
138- List <String > messages = new ArrayList <String >();
142+ Map <String , Object > response = new HashMap <>();
143+ List <String > messages = new ArrayList <>();
139144 try {
140145 PluginSettings pluginSettings = getPluginSettings ();
141146 String serverBaseURLToUse = pluginSettings .getServerBaseURL ();
142- if (StringUtils .isEmpty (serverBaseURLToUse )) {
147+ if (ValidationUtils .isEmpty (serverBaseURLToUse )) {
143148 serverBaseURLToUse = System .getProperty ("go.plugin.build.status.go-server" , "http://localhost:8153" );
144149 }
145150
146- Map pipeline = (Map ) dataMap .get ("pipeline" );
147- Map stage = (Map ) pipeline .get ("stage" );
151+ Map < String , Object > pipeline = (Map < String , Object > ) dataMap .get ("pipeline" );
152+ Map < String , Object > stage = (Map < String , Object > ) pipeline .get ("stage" );
148153
149154 String pipelineStage = String .format ("%s/%s" , pipeline .get ("name" ), stage .get ("name" ));
150155 String pipelineInstance = String .format ("%s/%s/%s/%s" , pipeline .get ("name" ), pipeline .get ("counter" ), stage .get ("name" ), stage .get ("counter" ));
151156 String trackbackURL = String .format ("%s/go/pipelines/%s" , serverBaseURLToUse , pipelineInstance );
152157 String result = (String ) stage .get ("result" );
153158
154- List <Map > materialRevisions = (List <Map >) pipeline .get ("build-cause" );
155- for (Map materialRevision : materialRevisions ) {
156- Map material = (Map ) materialRevision .get ("material" );
159+ List <Map < String , Object >> materialRevisions = (List <Map < String , Object > >) pipeline .get ("build-cause" );
160+ for (Map < String , Object > materialRevision : materialRevisions ) {
161+ Map < String , Object > material = (Map < String , Object > ) materialRevision .get ("material" );
157162 if (isMaterialFromTypes (material , provider .pollerPluginIds ())) {
158- Map materialConfiguration = (Map ) material .get ("scm-configuration" );
163+ Map < String , Object > materialConfiguration = (Map < String , Object > ) material .get ("scm-configuration" );
159164 String url = (String ) materialConfiguration .get ("url" );
160165
161- List <Map > modifications = (List <Map >) materialRevision .get ("modifications" );
166+ List <Map < String , Object >> modifications = (List <Map < String , Object > >) materialRevision .get ("modifications" );
162167 String revision = (String ) modifications .get (0 ).get ("revision" );
163- Map modificationData = (Map ) modifications .get (0 ).get ("data" );
168+ Map < String , Object > modificationData = (Map < String , Object > ) modifications .get (0 ).get ("data" );
164169 String prBranch = (String ) modificationData .getOrDefault ("PR_BRANCH" , modificationData .get ("PR_ID" ));
165170
166- if (StringUtils .isEmpty (prBranch )) {
171+ if (ValidationUtils .isEmpty (prBranch )) {
167172 prBranch = (String ) modificationData .get ("CURRENT_BRANCH" );
168173 }
169174
@@ -188,8 +193,8 @@ GoPluginApiResponse handleStageNotification(GoPluginApiRequest goPluginApiReques
188193 return renderJSON (responseCode , response );
189194 }
190195
191- private boolean isMaterialFromTypes (Map material , List <String > pollerPluginIds ) {
192- if ((( String ) material .get ("type" )). equalsIgnoreCase ( "scm" )) {
196+ private boolean isMaterialFromTypes (Map < String , Object > material , List <String > pollerPluginIds ) {
197+ if ("scm" . equals ( material .get ("type" ))) {
193198 return pollerPluginIds .contains ((String ) material .get ("plugin-id" ));
194199 }
195200
0 commit comments