1+ //
2+ // Licensed to the Apache Software Foundation (ASF) under one
3+ // or more contributor license agreements. See the NOTICE file
4+ // distributed with this work for additional information
5+ // regarding copyright ownership. The ASF licenses this file
6+ // to you under the Apache License, Version 2.0 (the
7+ // "License"); you may not use this file except in compliance
8+ // with the License. You may obtain a copy of the License at
9+ //
10+ // http://www.apache.org/licenses/LICENSE-2.0
11+ //
12+ // Unless required by applicable law or agreed to in writing,
13+ // software distributed under the License is distributed on an
14+ // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+ // KIND, either express or implied. See the License for the
16+ // specific language governing permissions and limitations
17+ // under the License.
18+ //
19+ package org .apache .cloudstack .direct .download ;
20+
21+ import com .cloud .utils .exception .CloudRuntimeException ;
22+ import org .apache .cloudstack .agent .directdownload .DirectDownloadCommand ;
23+ import org .apache .cloudstack .agent .directdownload .HttpDirectDownloadCommand ;
24+ import org .apache .cloudstack .agent .directdownload .HttpsDirectDownloadCommand ;
25+ import org .apache .cloudstack .agent .directdownload .MetalinkDirectDownloadCommand ;
26+ import org .apache .cloudstack .agent .directdownload .NfsDirectDownloadCommand ;
27+ import org .apache .log4j .Logger ;
28+
29+ public class DirectDownloadHelper {
30+
31+ public static final Logger LOGGER = Logger .getLogger (DirectDownloadHelper .class .getName ());
32+
33+ /**
34+ * Get direct template downloader from direct download command and destination pool
35+ */
36+ public static DirectTemplateDownloader getDirectTemplateDownloaderFromCommand (DirectDownloadCommand cmd ,
37+ String destPoolLocalPath ,
38+ String temporaryDownloadPath ) {
39+ if (cmd instanceof HttpDirectDownloadCommand ) {
40+ return new HttpDirectTemplateDownloader (cmd .getUrl (), cmd .getTemplateId (), destPoolLocalPath , cmd .getChecksum (), cmd .getHeaders (),
41+ cmd .getConnectTimeout (), cmd .getSoTimeout (), temporaryDownloadPath );
42+ } else if (cmd instanceof HttpsDirectDownloadCommand ) {
43+ return new HttpsDirectTemplateDownloader (cmd .getUrl (), cmd .getTemplateId (), destPoolLocalPath , cmd .getChecksum (), cmd .getHeaders (),
44+ cmd .getConnectTimeout (), cmd .getSoTimeout (), cmd .getConnectionRequestTimeout (), temporaryDownloadPath );
45+ } else if (cmd instanceof NfsDirectDownloadCommand ) {
46+ return new NfsDirectTemplateDownloader (cmd .getUrl (), destPoolLocalPath , cmd .getTemplateId (), cmd .getChecksum (), temporaryDownloadPath );
47+ } else if (cmd instanceof MetalinkDirectDownloadCommand ) {
48+ return new MetalinkDirectTemplateDownloader (cmd .getUrl (), destPoolLocalPath , cmd .getTemplateId (), cmd .getChecksum (), cmd .getHeaders (),
49+ cmd .getConnectTimeout (), cmd .getSoTimeout (), temporaryDownloadPath );
50+ } else {
51+ throw new IllegalArgumentException ("Unsupported protocol, please provide HTTP(S), NFS or a metalink" );
52+ }
53+ }
54+
55+ public static boolean checkUrlExistence (String url ) {
56+ try {
57+ DirectTemplateDownloader checker = getCheckerDownloader (url );
58+ return checker .checkUrl (url );
59+ } catch (CloudRuntimeException e ) {
60+ LOGGER .error (String .format ("Cannot check URL %s is reachable due to: %s" , url , e .getMessage ()), e );
61+ return false ;
62+ }
63+ }
64+
65+ private static DirectTemplateDownloader getCheckerDownloader (String url ) {
66+ if (url .toLowerCase ().startsWith ("https:" )) {
67+ return new HttpsDirectTemplateDownloader (url );
68+ } else if (url .toLowerCase ().startsWith ("http:" )) {
69+ return new HttpDirectTemplateDownloader (url );
70+ } else if (url .toLowerCase ().startsWith ("nfs:" )) {
71+ return new NfsDirectTemplateDownloader (url );
72+ } else if (url .toLowerCase ().endsWith (".metalink" )) {
73+ return new MetalinkDirectTemplateDownloader (url );
74+ } else {
75+ throw new CloudRuntimeException (String .format ("Cannot find a download checker for url: %s" , url ));
76+ }
77+ }
78+
79+ public static Long getFileSize (String url , String format ) {
80+ DirectTemplateDownloader checker = getCheckerDownloader (url );
81+ return checker .getRemoteFileSize (url , format );
82+ }
83+ }
0 commit comments