Enhance HaloUtils

pull/137/head
johnniang 2019-03-08 20:47:53 +08:00
parent e654e96db6
commit 82695a2e30
3 changed files with 69 additions and 14 deletions

View File

@ -66,12 +66,7 @@ public class StartedListener implements ApplicationListener<ApplicationStartedEv
// Get server port // Get server port
String serverPort = applicationContext.getEnvironment().getProperty("server.port"); String serverPort = applicationContext.getEnvironment().getProperty("server.port");
String blogUrl = HaloConst.OPTIONS.get(BlogPropertiesEnum.BLOG_URL.getProp()); String blogUrl = getBlogUrl();
if (StrUtil.isNotBlank(blogUrl)) {
blogUrl = StrUtil.removeSuffix(blogUrl, "/");
} else {
blogUrl = "http://localhost:" + serverPort;
}
log.info("Halo started at {}", blogUrl); log.info("Halo started at {}", blogUrl);
log.info("Halo admin is at {}/admin", blogUrl); log.info("Halo admin is at {}/admin", blogUrl);
@ -80,6 +75,26 @@ public class StartedListener implements ApplicationListener<ApplicationStartedEv
} }
} }
/**
* Gets blog url.
*
* @return blog url (If blog url isn't present, current machine IP address will be default)
*/
private String getBlogUrl() {
// Get server port
String serverPort = applicationContext.getEnvironment().getProperty("server.port");
String blogUrl = HaloConst.OPTIONS.get(BlogPropertiesEnum.BLOG_URL.getProp());
if (StrUtil.isNotBlank(blogUrl)) {
blogUrl = StrUtil.removeSuffix(blogUrl, "/");
} else {
blogUrl = String.format("http://%s:%s", HaloUtils.getMachineIP(), serverPort);
}
return blogUrl;
}
/** /**
* *
*/ */

View File

@ -17,8 +17,10 @@ import org.springframework.util.ResourceUtils;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.*; import java.io.*;
import java.net.InetAddress;
import java.net.URL; import java.net.URL;
import java.net.URLConnection; import java.net.URLConnection;
import java.net.UnknownHostException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.LinkOption; import java.nio.file.LinkOption;
import java.nio.file.Path; import java.nio.file.Path;
@ -42,6 +44,21 @@ public class HaloUtils {
public final static int DEFAULT_PAGE_SIZE = 10; public final static int DEFAULT_PAGE_SIZE = 10;
/**
* Gets machine IP address.
*
* @return current machine IP address.
*/
public static String getMachineIP() {
InetAddress machineAddress;
try {
machineAddress = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
machineAddress = InetAddress.getLoopbackAddress();
}
return machineAddress.getHostAddress();
}
/** /**
* Gets default page size. * Gets default page size.
* *
@ -187,9 +204,9 @@ public class HaloUtils {
final BufferedImage image = ImageIO.read(new FileInputStream(file)); final BufferedImage image = ImageIO.read(new FileInputStream(file));
return image.getWidth() + "x" + image.getHeight(); return image.getWidth() + "x" + image.getHeight();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); log.error("Failed to get read image file", e);
return "";
} }
return "";
} }
/** /**
@ -232,7 +249,7 @@ public class HaloUtils {
} }
} }
} catch (Exception e) { } catch (Exception e) {
log.error("Themes scan failed{}", e.getMessage()); log.error("Themes scan failed", e);
} }
return themes; return themes;
} }
@ -268,7 +285,7 @@ public class HaloUtils {
} }
} }
} catch (Exception e) { } catch (Exception e) {
log.error("Failed to get theme template: {}", e.getMessage()); log.error("Failed to get theme template", e);
} }
return tpls; return tpls;
} }
@ -294,7 +311,7 @@ public class HaloUtils {
} }
} }
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
e.printStackTrace(); log.error("File not found", e);
} }
return tpls; return tpls;
} }
@ -318,7 +335,7 @@ public class HaloUtils {
bufferedWriter = new BufferedWriter(fileWriter); bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write(data); bufferedWriter.write(data);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); log.error("Failed to export file", e);
} finally { } finally {
if (null != bufferedWriter) { if (null != bufferedWriter) {
bufferedWriter.close(); bufferedWriter.close();
@ -392,7 +409,7 @@ public class HaloUtils {
result.append(line); result.append(line);
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); log.error("Failed to create baidu post", e);
} finally { } finally {
try { try {
if (null != out) { if (null != out) {
@ -402,7 +419,7 @@ public class HaloUtils {
in.close(); in.close();
} }
} catch (IOException ex) { } catch (IOException ex) {
ex.printStackTrace(); // Ignore this exception
} }
} }
return result.toString(); return result.toString();

View File

@ -0,0 +1,23 @@
package cc.ryanc.halo.utils;
import org.junit.Test;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* InetAddress test.
*
* @author johnniang
*/
public class InetAddressTest {
@Test
public void getMachaineAddressTest() throws UnknownHostException {
InetAddress localHost = InetAddress.getLocalHost();
System.out.println("Localhost: " + localHost.getHostAddress());
InetAddress loopbackAddress = InetAddress.getLoopbackAddress();
System.out.println("Loopback: " + loopbackAddress.getHostAddress());
}
}