1616// under the License.
1717package com .cloud .template ;
1818
19+ import java .net .MalformedURLException ;
20+ import java .net .URL ;
1921import java .util .List ;
2022import java .util .concurrent .ExecutionException ;
2123
@@ -111,7 +113,29 @@ public TemplateProfile prepare(RegisterIsoCmd cmd) throws ResourceAllocationExce
111113 public TemplateProfile prepare (RegisterTemplateCmd cmd ) throws ResourceAllocationException {
112114 TemplateProfile profile = super .prepare (cmd );
113115 String url = profile .getUrl ();
116+ String path = null ;
117+ try {
118+ URL str = new URL (url );
119+ path = str .getPath ();
120+ } catch (MalformedURLException ex ) {
121+ throw new InvalidParameterValueException ("Please specify a valid URL. URL:" + url + " is invalid" );
122+ }
123+
124+ try {
125+ checkFormat (cmd .getFormat (), url );
126+ } catch (InvalidParameterValueException ex ) {
127+ checkFormat (cmd .getFormat (), path );
128+ }
114129
130+ UriUtils .validateUrl (url );
131+ profile .setUrl (url );
132+ // Check that the resource limit for secondary storage won't be exceeded
133+ _resourceLimitMgr .checkResourceLimit (_accountMgr .getAccount (cmd .getEntityOwnerId ()),
134+ ResourceType .secondary_storage , UriUtils .getRemoteSize (url ));
135+ return profile ;
136+ }
137+
138+ private void checkFormat (String format , String url ) {
115139 if ((!url .toLowerCase ().endsWith ("vhd" ))&&(!url .toLowerCase ().endsWith ("vhd.zip" ))
116140 &&(!url .toLowerCase ().endsWith ("vhd.bz2" ))&&(!url .toLowerCase ().endsWith ("vhd.gz" ))
117141 &&(!url .toLowerCase ().endsWith ("qcow2" ))&&(!url .toLowerCase ().endsWith ("qcow2.zip" ))
@@ -121,25 +145,31 @@ public TemplateProfile prepare(RegisterTemplateCmd cmd) throws ResourceAllocatio
121145 &&(!url .toLowerCase ().endsWith ("tar" ))&&(!url .toLowerCase ().endsWith ("tar.zip" ))
122146 &&(!url .toLowerCase ().endsWith ("tar.bz2" ))&&(!url .toLowerCase ().endsWith ("tar.gz" ))
123147 &&(!url .toLowerCase ().endsWith ("img" ))&&(!url .toLowerCase ().endsWith ("raw" ))){
124- throw new InvalidParameterValueException ("Please specify a valid " + cmd . getFormat () .toLowerCase ());
148+ throw new InvalidParameterValueException ("Please specify a valid " + format .toLowerCase ());
125149 }
126150
127- if ((cmd .getFormat ().equalsIgnoreCase ("vhd" ) && (!url .toLowerCase ().endsWith ("vhd" ) && !url .toLowerCase ().endsWith ("vhd.zip" ) && !url .toLowerCase ().endsWith ("vhd.bz2" ) && !url .toLowerCase ().endsWith ("vhd.gz" ) ))
128- || (cmd .getFormat ().equalsIgnoreCase ("qcow2" ) && (!url .toLowerCase ().endsWith ("qcow2" ) && !url .toLowerCase ().endsWith ("qcow2.zip" ) && !url .toLowerCase ().endsWith ("qcow2.bz2" ) && !url .toLowerCase ().endsWith ("qcow2.gz" ) ))
129- || (cmd .getFormat ().equalsIgnoreCase ("ova" ) && (!url .toLowerCase ().endsWith ("ova" ) && !url .toLowerCase ().endsWith ("ova.zip" ) && !url .toLowerCase ().endsWith ("ova.bz2" ) && !url .toLowerCase ().endsWith ("ova.gz" )))
130- || (cmd .getFormat ().equalsIgnoreCase ("tar" ) && (!url .toLowerCase ().endsWith ("tar" ) && !url .toLowerCase ().endsWith ("tar.zip" ) && !url .toLowerCase ().endsWith ("tar.bz2" ) && !url .toLowerCase ().endsWith ("tar.gz" )))
131- || (cmd .getFormat ().equalsIgnoreCase ("raw" ) && (!url .toLowerCase ().endsWith ("img" ) && !url .toLowerCase ().endsWith ("raw" )))) {
132- throw new InvalidParameterValueException ("Please specify a valid URL. URL:" + url + " is an invalid for the format " + cmd .getFormat ().toLowerCase ());
151+ if ((format .equalsIgnoreCase ("vhd" ) && (!url .toLowerCase ().endsWith ("vhd" )
152+ && !url .toLowerCase ().endsWith ("vhd.zip" ) && !url .toLowerCase ().endsWith ("vhd.bz2" ) && !url
153+ .toLowerCase ().endsWith ("vhd.gz" )))
154+ || (format .equalsIgnoreCase ("qcow2" ) && (!url .toLowerCase ().endsWith ("qcow2" )
155+ && !url .toLowerCase ().endsWith ("qcow2.zip" ) && !url .toLowerCase ().endsWith ("qcow2.bz2" ) && !url
156+ .toLowerCase ().endsWith ("qcow2.gz" )))
157+ || (format .equalsIgnoreCase ("ova" ) && (!url .toLowerCase ().endsWith ("ova" )
158+ && !url .toLowerCase ().endsWith ("ova.zip" ) && !url .toLowerCase ().endsWith ("ova.bz2" ) && !url
159+ .toLowerCase ().endsWith ("ova.gz" )))
160+ || (format .equalsIgnoreCase ("tar" ) && (!url .toLowerCase ().endsWith ("tar" )
161+ && !url .toLowerCase ().endsWith ("tar.zip" ) && !url .toLowerCase ().endsWith ("tar.bz2" ) && !url
162+ .toLowerCase ().endsWith ("tar.gz" )))
163+ || (format .equalsIgnoreCase ("raw" ) && (!url .toLowerCase ().endsWith ("img" ) && !url .toLowerCase ()
164+ .endsWith ("raw" )))) {
165+ throw new InvalidParameterValueException ("Please specify a valid URL. URL:" + url
166+ + " is an invalid for the format " + format .toLowerCase ());
133167 }
134168
135- UriUtils .validateUrl (url );
136- profile .setUrl (url );
137- // Check that the resource limit for secondary storage won't be exceeded
138- _resourceLimitMgr .checkResourceLimit (_accountMgr .getAccount (cmd .getEntityOwnerId ()),
139- ResourceType .secondary_storage , UriUtils .getRemoteSize (url ));
140- return profile ;
169+
141170 }
142171
172+
143173 @ Override
144174 public VMTemplateVO create (TemplateProfile profile ) {
145175 // persist entry in vm_template, vm_template_details and template_zone_ref tables, not that entry at template_store_ref is not created here, and created in createTemplateAsync.
0 commit comments