1010
1111import org .apache .commons .io .FileUtils ;
1212import org .apache .commons .io .IOUtils ;
13- import org .apache .http .HttpEntity ;
14- import org .apache .http .client .methods .CloseableHttpResponse ;
15- import org .apache .http .client .methods .HttpGet ;
16- import org .apache .http .impl .client .CloseableHttpClient ;
17- import org .apache .http .impl .client .HttpClients ;
1813import org .slf4j .Logger ;
1914import org .slf4j .LoggerFactory ;
2015
2116import java .io .File ;
17+ import java .io .InputStream ;
2218import java .io .IOException ;
19+ import java .net .MalformedURLException ;
20+ import java .net .URL ;
21+ import java .net .URLConnection ;
2322
2423public class TmcCliUpdater {
2524
@@ -46,73 +45,73 @@ public TmcCliUpdater(Io io, String currentVersion, boolean isWindows) {
4645 * Checks if there's a newer tmc-cli version released on Github and asks if
4746 * the user wants to download it. TODO: split it up
4847 */
49- public void run () {
48+ public boolean run () {
5049 JsonObject release = toJsonObject (fetchLatestReleaseJson ());
5150 if (release == null || !isNewer (release )) {
52- return ;
51+ return false ;
5352 }
5453
5554 JsonObject binAsset = findCorrectAsset (release , isWindows );
5655 if (binAsset == null || !binAsset .has ("name" ) || !binAsset .has ("browser_download_url" )) {
5756 logger .warn ("The JSON does not contain necessary information for update." );
58- return ;
57+ return false ;
5958 }
6059
6160 io .println ("A new version of tmc-cli is available!" );
6261
6362 if (isWindows ) { //just show a link for Windows users now, todo...
6463 io .println ("Download: https://github.com/tmc-cli/tmc-cli/releases/latest" );
65- return ;
64+ return false ;
6665 }
6766
6867 if (! io .readConfirmation ("Do you want to download it?" , true )) {
69- return ;
68+ return false ;
7069 }
7170
7271 String binName = binAsset .get ("name" ).getAsString () + ".new" ;
7372 String dlUrl = binAsset .get ("browser_download_url" ).getAsString ();
7473 String currentBinLocation = getJarLocation ();
7574 if (currentBinLocation == null ) {
7675 io .println ("Unable to find current program location, aborting update." );
77- return ;
76+ return false ;
7877 }
7978 File destination = new File (currentBinLocation + binName );
8079
8180 io .println ("Downloading..." );
8281 fetchTmcCliBinary (dlUrl , destination );
8382
8483 io .println ("Running " + destination .getAbsolutePath ());
85- runNewTmcCliBinary (destination .getAbsolutePath ());
84+ return runNewTmcCliBinary (destination .getAbsolutePath ());
8685 }
8786
88- protected byte [] fetchHttpEntity (String url ) {
89- CloseableHttpClient httpClient = HttpClients .createDefault ();
90- HttpGet httpGet = new HttpGet (url );
91- httpGet .addHeader ("User-Agent" , "tmc-cli (https://github.com/tmc-cli/tmc-cli)" );
87+ protected byte [] fetchHttpEntity (String urlAddress ) {
88+ URL url ;
9289
93- HttpEntity entity ;
94- byte [] content ;
9590 try {
96- CloseableHttpResponse httpResponse = httpClient .execute (httpGet );
97- entity = httpResponse .getEntity ();
98- if (entity == null ) {
99- logger .warn ("Failed to get http request content." );
100- httpGet .releaseConnection ();
101- return null ;
102- }
91+ url = new URL (urlAddress );
92+ } catch (MalformedURLException ex ) {
93+ logger .warn ("Url formatting failed" , ex );
94+ return null ;
95+ }
10396
97+ InputStream inputStream ;
98+ try {
99+ URLConnection connection = url .openConnection ();
100+ connection .setRequestProperty ("User-Agent" , "tmc-cli (https://github.com/tmc-cli/tmc-cli)" );
101+ inputStream = connection .getInputStream ();
104102 } catch (IOException ex ) {
105- logger .warn ("Failed to create http connection to github. " , ex );
103+ logger .warn ("Failed to fetch page " , ex );
106104 io .println ("Failed to create https connection to github." );
107105 return null ;
108106 }
107+
108+ byte [] content ;
109109 try {
110- content = IOUtils .toByteArray (entity . getContent () );
110+ content = IOUtils .toByteArray (inputStream );
111111 } catch (IOException ex ) {
112112 logger .warn ("Failed to fetch data from github" , ex );
113113 content = null ;
114114 }
115- httpGet .releaseConnection ();
116115 return content ;
117116 }
118117
@@ -153,8 +152,8 @@ protected void fetchTmcCliBinary(String downloadUrl, File destination) {
153152 /**
154153 * Finish the update by running downloaded binary.
155154 */
156- protected void runNewTmcCliBinary (String pathToNewBinary ) {
157- ExternalsUtil .runUpdater (io , pathToNewBinary );
155+ protected boolean runNewTmcCliBinary (String pathToNewBinary ) {
156+ return ExternalsUtil .runUpdater (io , pathToNewBinary );
158157 }
159158
160159 /**
0 commit comments