Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package db.dao; import beans.Article; import beans.Image; import beans.ShareSiteUser; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.Serializable; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLEncoder; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.LinkedList; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; import javax.ejb.Stateless; import static javax.ws.rs.core.HttpHeaders.USER_AGENT; import org.apache.http.HttpResponse; import org.primefaces.json.JSONArray; import org.primefaces.json.JSONException; import org.primefaces.json.JSONObject; /** * * @author Pepijn */ @Stateless public class ArticleDao extends Dao implements Serializable { public ArticleDao() { } public HttpResponse saveHttpClient(Article article) { return this.add("article/new", article); } public HttpResponse updateArticle(Article article) { return this.update("article/update", article); } public List<Article> listAll() { JSONArray json = this.listAllHttpClient("article/"); List<Article> list = new LinkedList<>(); for (int i = 0; i < json.length(); i++) { try { Article currentArticle = new Article(); JSONObject currentjson = json.getJSONObject(i); currentArticle.setId(currentjson.getString("id")); currentArticle.setContent(currentjson.getString("content")); currentArticle.setTitle(currentjson.getString("title")); currentArticle.setLocation(currentjson.getString("location")); currentArticle.setValidated(currentjson.getInt("validated")); String pattern = "yyyy-MM-dd'T'HH:mm:ss"; SimpleDateFormat df = new SimpleDateFormat(pattern); try { Date result = df.parse(currentjson.getString("dateOfPost")); currentArticle.setDateOfPost(result); } catch (ParseException e) { Logger.getLogger(ArticleDao.class.getName()).log(Level.SEVERE, null, e); } JSONObject image = currentjson.getJSONObject("image"); currentArticle.setImage(new Image(image.getString("id"), image.getString("path"))); JSONObject user = currentjson.getJSONObject("shareSiteUser"); currentArticle.setShareSiteUser(new ShareSiteUser(user.getString("name"), user.getString("id"), user.getString("password"), user.getInt("usergroupid"))); list.add(currentArticle); } catch (JSONException ex) { Logger.getLogger(ArticleDao.class.getName()).log(Level.SEVERE, null, ex); } } return list; } public void deletebyId(String deleteId) { try { String url = this.checkIfLocal("article/delete"); URL obj = new URL(url); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); //add reuqest header con.setRequestMethod("POST"); con.setRequestProperty("User-Agent", USER_AGENT); con.setRequestProperty("Accept-Language", "en-US,en;q=0.5"); String urlParameters = "id=" + URLEncoder.encode(deleteId); // Send post request con.setDoOutput(true); DataOutputStream wr = new DataOutputStream(con.getOutputStream()); wr.writeBytes(urlParameters); wr.flush(); wr.close(); int responseCode = con.getResponseCode(); //print result System.out.println(responseCode); } catch (MalformedURLException ex) { Logger.getLogger(ArticleDao.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(ArticleDao.class.getName()).log(Level.SEVERE, null, ex); } } }