3030@ Singleton
3131public class DefaultWorkspaceConfigValidator implements WorkspaceConfigValidator {
3232 /* should contain [3, 20] characters, first and last character is letter or digit, available characters {A-Za-z0-9.-_}*/
33- private static final Pattern WS_NAME = Pattern .compile ("[\\ w][ \\ w \\ . \\ - ]{1,18}[\\ w ]" );
33+ private static final Pattern WS_NAME = Pattern .compile ("[a-zA-Z0-9][-_.a-zA-Z0-9 ]{1,18}[a-zA-Z0-9 ]" );
3434
3535 /**
3636 * Checks that workspace configuration is valid.
@@ -49,58 +49,67 @@ public class DefaultWorkspaceConfigValidator implements WorkspaceConfigValidator
4949 @ Override
5050 public void validate (WorkspaceConfig config ) throws BadRequestException {
5151 // configuration object itself
52- requiredNotNull (config .getName (), "Workspace name required" );
53- if (!WS_NAME .matcher (config .getName ()).matches ()) {
54- throw new BadRequestException ("Incorrect workspace name, it must be between 3 and 20 characters and may contain digits, " +
55- "latin letters, underscores, dots, dashes and should start and end only with digits, " +
56- "latin letters or underscores" );
57- }
52+ checkNotNull (config .getName (), "Workspace name required" );
53+ checkArgument (WS_NAME .matcher (config .getName ()).matches (),
54+ "Incorrect workspace name, it must be between 3 and 20 characters and may contain digits, " +
55+ "latin letters, underscores, dots, dashes and should start and end only with digits, " +
56+ "latin letters or underscores" );
5857
5958 //attributes
6059 for (String attributeName : config .getAttributes ().keySet ()) {
6160 //attribute name should not be empty and should not start with codenvy
62- if (attributeName .trim ().isEmpty () || attributeName .toLowerCase ().startsWith ("codenvy" )) {
63- throw new BadRequestException ( format ( "Attribute name '%s' is not valid" , attributeName ));
64- }
61+ checkArgument (attributeName != null && ! attributeName .trim ().isEmpty () && ! attributeName .toLowerCase ().startsWith ("codenvy" ),
62+ "Attribute name '%s' is not valid" ,
63+ attributeName );
6564 }
6665
6766 //environments
68- requiredNotNull (config .getDefaultEnv (), "Workspace default environment name required" );
69- if (!config .getEnvironments ().stream ().anyMatch (env -> env .getName ().equals (config .getDefaultEnv ()))) {
70- throw new BadRequestException ("Workspace default environment configuration required" );
71- }
67+ checkArgument (!isNullOrEmpty (config .getDefaultEnv ()), "Workspace default environment name required" );
68+ checkArgument (config .getEnvironments ()
69+ .stream ()
70+ .anyMatch (env -> config .getDefaultEnv ().equals (env .getName ())),
71+ "Workspace default environment configuration required" );
72+
7273 for (Environment environment : config .getEnvironments ()) {
7374 final String envName = environment .getName ();
74- requiredNotNull (envName , "Environment name should not be null" );
75+ checkArgument (!isNullOrEmpty (envName ), "Environment name should be neither null nor empty" );
76+
77+ checkArgument (environment .getRecipe () == null || "docker" .equals (environment .getRecipe ().getType ()),
78+ "Couldn't start workspace '%s' from environment '%s', environment recipe has unsupported type '%s'" ,
79+ config .getName (),
80+ envName ,
81+ environment .getRecipe () != null ? environment .getRecipe ().getType () : null );
7582
7683 //machine configs
77- if (environment .getMachineConfigs ().isEmpty ()) {
78- throw new BadRequestException ("Environment '" + envName + "' should contain at least 1 machine" );
79- }
84+ checkArgument (!environment .getMachineConfigs ().isEmpty (), "Environment '%s' should contain at least 1 machine" , envName );
85+
8086 final long devCount = environment .getMachineConfigs ()
8187 .stream ()
8288 .filter (MachineConfig ::isDev )
8389 .count ();
84- if (devCount != 1 ) {
85- throw new BadRequestException (format ("Environment should contain exactly 1 dev machine, but '%s' contains '%d'" ,
86- envName ,
87- devCount ));
88- }
90+ checkArgument (devCount == 1 ,
91+ "Environment should contain exactly 1 dev machine, but '%s' contains '%d'" ,
92+ envName ,
93+ devCount );
8994 for (MachineConfig machineCfg : environment .getMachineConfigs ()) {
90- if (isNullOrEmpty (machineCfg .getName ())) {
91- throw new BadRequestException ("Environment " + envName + " contains machine without of name" );
92- }
93- requiredNotNull (machineCfg .getSource (), "Environment " + envName + " contains machine without of source" );
94- //TODO require type?
95+ checkArgument (!isNullOrEmpty (machineCfg .getName ()), "Environment %s contains machine with null or empty name" , envName );
96+ checkNotNull (machineCfg .getSource (), "Environment " + envName + " contains machine without source" );
97+ checkArgument ("docker" .equals (machineCfg .getType ()),
98+ "Type of machine %s in environment %s is not supported. Supported value is 'docker'." ,
99+ machineCfg .getName (),
100+ envName );
95101 }
96102 }
97103
98104 //commands
99105 for (Command command : config .getCommands ()) {
100- requiredNotNull (command .getName (), "Workspace " + config .getName () + " contains command without of name" );
101- requiredNotNull (command .getCommandLine (), format ("Command line required for command '%s' in workspace '%s'" ,
102- command .getName (),
103- config .getName ()));
106+ checkArgument (!isNullOrEmpty (command .getName ()),
107+ "Workspace %s contains command with null or empty name" ,
108+ config .getName ());
109+ checkArgument (!isNullOrEmpty (command .getCommandLine ()),
110+ "Command line required for command '%s' in workspace '%s'" ,
111+ command .getName (),
112+ config .getName ());
104113 }
105114
106115 //projects
@@ -111,9 +120,31 @@ public void validate(WorkspaceConfig config) throws BadRequestException {
111120 * Checks that object reference is not null, throws {@link BadRequestException}
112121 * in the case of null {@code object} with given {@code message}.
113122 */
114- private void requiredNotNull (Object object , String message ) throws BadRequestException {
123+ private void checkNotNull (Object object , String message ) throws BadRequestException {
115124 if (object == null ) {
116125 throw new BadRequestException (message );
117126 }
118127 }
128+
129+ /**
130+ * Checks that expression is true, throws {@link BadRequestException} otherwise.
131+ *
132+ * <p>Exception uses error message built from error message template and error message parameters.
133+ */
134+ private void checkArgument (boolean expression , String errorMessageTemplate , Object ... errorMessageParams ) throws BadRequestException {
135+ if (!expression ) {
136+ throw new BadRequestException (format (errorMessageTemplate , errorMessageParams ));
137+ }
138+ }
139+
140+ /**
141+ * Checks that expression is true, throws {@link BadRequestException} otherwise.
142+ *
143+ * <p>Exception uses error message built from error message template and error message parameters.
144+ */
145+ private void checkArgument (boolean expression , String errorMessage ) throws BadRequestException {
146+ if (!expression ) {
147+ throw new BadRequestException (errorMessage );
148+ }
149+ }
119150}
0 commit comments