Java tutorial
/** * The MIT License * * Copyright 2016 Patrick Dallarosa and Stephen Brikiatis. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ package slideshow.server; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.net.HttpURLConnection; import java.net.ServerSocket; import java.net.Socket; import java.net.URL; import java.util.Calendar; import java.util.GregorianCalendar; import org.json.JSONException; import org.json.JSONObject; /** * <h1>Run Server</h1> * Manages the connections on the server side. Takes requests * and send files to client * * @author Stephen * @version 1.0 * @since 19 April 2016 */ public class App { /** * Initializes server and creates threads * * @param args * @throws java.lang.InterruptedException * @throws java.io.IOException */ public static void main(String[] args) throws InterruptedException, IOException, JSONException { System.out.print("Testing sending a picture"); Socket sSocket = null; Calendar cal = new GregorianCalendar(); int today = cal.get(Calendar.DAY_OF_MONTH); int month = cal.get(Calendar.MONTH) + 1; int year = cal.get(Calendar.YEAR); holderDate ticker = new holderDate(); ticker.setDay(today); ticker.setMonth(month); ticker.setYear(year); ticker.setNewPicture(""); String APIdate = ""; try { ServerSocket serverSocket = new ServerSocket(6001); System.out.println("Waiting for connection..."); sSocket = serverSocket.accept(); OutputStream outputStream = sSocket.getOutputStream(); while (true) { ticker = getNextDate(ticker); APIdate = ticker.getNewPicture(); String APIStream = getAPIPicture(APIdate); PrintWriter out = new PrintWriter(sSocket.getOutputStream(), true); String inputLine; out.println(APIStream); Thread.sleep(10000); /* ImageInputStream imageInput = ImageIO.createImageInputStream(APIStream); BufferedImage img = ImageIO.read(imageInput); ByteArrayOutputStream pictureOutput = new ByteArrayOutputStream(); ImageIO.write(img, "jpg", pictureOutput); byte[] size = ByteBuffer.allocate(4).putInt(pictureOutput.size()).array(); outputStream.write(size); outputStream.write(pictureOutput.toByteArray()); outputStream.flush();*/ if (ticker.getYear() == 1999) { break; } System.out.println(APIStream); } sSocket.close(); } catch (IOException es) { System.out.println(es.getMessage()); } } private static holderDate getNextDate(holderDate ticker) { String newDate = ""; int day = ticker.getDay(); int month = ticker.getMonth(); int year = ticker.getYear(); if (day != 1) { day--; ticker.setDay(day); } else if (month != 1) { month--; ticker.setMonth(month); } else { year--; ticker.setYear(year); } newDate = Integer.toString(year) + "-"; if (month <= 9) { newDate += "0" + Integer.toString(month) + "-"; } else { newDate += Integer.toString(month) + "-"; } if (day <= 9) { newDate += "0" + Integer.toString(day); } else { newDate += Integer.toString(day); } ticker.setNewPicture(newDate); return ticker; } private static String getAPIPicture(String APIdate) throws JSONException, FileNotFoundException { HttpURLConnection urlConnection = null; BufferedReader APIReader = null; String APIJson = ""; String API_KEY = "npjPdwCf5lRYWfL1aXYhHZf6q7nYjGVE362I7h6t"; InputStream PictureStream = null; try { String APIString = "https://api.nasa.gov/planetary/apod?date=" + APIdate + "&hd=true&api_key=" + API_KEY; URL url = new URL(APIString); urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestMethod("GET"); urlConnection.connect(); InputStream APIStream = urlConnection.getInputStream(); APIReader = new BufferedReader(new InputStreamReader(APIStream)); StringBuilder APIBuffer = new StringBuilder(); String line = ""; while ((line = APIReader.readLine()) != null) { APIBuffer.append(line).append("\n"); } APIJson = APIBuffer.toString(); JSONObject apod = new JSONObject(APIJson); APIJson = apod.getString("url"); } catch (IOException ex) { System.out.println(ex.getMessage()); } finally { if (urlConnection != null) { urlConnection.disconnect(); } if (APIReader != null) { try { APIReader.close(); } catch (IOException ex) { System.out.println(ex.getMessage()); } } } return APIJson; } }//end class App