package entity;
import entity.util.JsfUtil;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author Nafanya Victorovna
*/
public class OAuth extends HttpServlet {
private static final String client_id = "4135134";
private final String redirect_uri="http://localhost:8080/My/OAuth";
private static final String client_secret="V7BO8Cw5tv4TEvl8auzi";
private static String address = "List";
private static boolean access = false;
private static String user_id = null;
private static int count = 0;
public static void setAddress(String str){
OAuth.address = str;
}
public String Redirect() {
String reference = "http://localhost:8080/My/faces/information/" + address + ".xhtml";
address = "List";
return reference;
}
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
String code = request.getParameter("code");
if(code == null || code.equals("")) {}
response.getWriter().println(code);
String token = null;
try{
String address = "https://oauth.vk.com/access_token?"
+ "client_id="+client_id
+"&redirect_uri="+URLEncoder.encode(redirect_uri)
+"&client_secret="+client_secret
+"&code="+code;
URL url = new URL(address);
URLConnection connection = url.openConnection();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String temp = null;
while((temp = reader.readLine()) != null){
token += temp;
}
token = token.replace("null","");
}catch(Exception e) {}
System.out.println(token);
UsermyController userController = new UsermyController();
if(token.contains("access_token")) {
access = true;
count++;
int begin = token.lastIndexOf(':')+1;
user_id = token.substring(begin, begin+8);
if(count == 1){
userController.create();
}else{
userController.update(user_id);
}
System.out.println(user_id);
System.out.println(access);
if(address.equals("Login")){
if(count == 0){
JsfUtil.addErrorMessage("А вы разве аторизованы?");
}else{
userController.delete(user_id);
access = false;
token = null;
System.out.println("*************");
System.out.println(token);
System.out.println(access);
System.out.println("*************");
}
}
response.sendRedirect(Redirect());
}
}
public static int getCount(){
return OAuth.count;
}
public static boolean getAccess(){
return OAuth.access;
}
public static String getId(){
return OAuth.user_id;
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
public String getServletInfo() {
return "Short description";
}
}
package entity;
import entity.util.JsfUtil;
import java.io.Serializable;
import javax.ejb.EJB;
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
@Named("usermyController")
@SessionScoped
public class UsermyController implements Serializable {
private static Usermy current;
@EJB
private UsermyFacade ejbFacade;
public UsermyController() {}
private UsermyFacade getFacade() {
System.out.println("******");
System.out.println(ejbFacade);
System.out.println("******");
return ejbFacade;
}
public Usermy getUsermy(java.lang.String id) {
return ejbFacade.find(id);
}
public void writeToDB(){
boolean bool = OAuth.getAccess();
if(bool){
String id = OAuth.getId();
getFacade();
Usermy user = getUsermy(id);
if(user != null){
update(user);
} else{
Create();
}
}
}
public void deleteFromDB(){
boolean bool = OAuth.getAccess();
if(bool){
String id = OAuth.getId();
Usermy user = getUsermy(id);
delete(user);
} else {
JsfUtil.addErrorMessage("А вы разве авторизовывались?");
}
}
public void Create() {
current = new Usermy();
getFacade().create(current);
String id = OAuth.getId();
boolean bool = OAuth.getAccess();
current.setIduser(id);
current.setIsAuth(bool);
}
public void update(Usermy user) {
boolean bool = OAuth.getAccess();
current.setIsAuth(bool);
getFacade().edit(current);
}
private static void delete(Usermy user) {
current = user;
current.setIsAuth(Boolean.FALSE);
}
}
package entity;
import entity.util.JsfUtil;
import entity.util.PaginationHelper;
import java.io.Serializable;
import java.util.ResourceBundle;
import javax.ejb.EJB;
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import javax.faces.model.SelectItem;
@Named("usermyController")
@SessionScoped
public class UsermyController implements Serializable {
private Usermy current;
private DataModel items = null;
@EJB
private entity.UsermyFacade ejbFacade;
private PaginationHelper pagination;
private int selectedItemIndex;
public UsermyController() {
}
public Usermy getSelected() {
if (current == null) {
current = new Usermy();
selectedItemIndex = -1;
}
return current;
}
private UsermyFacade getFacade() {
return ejbFacade;
}
public PaginationHelper getPagination() {
if (pagination == null) {
pagination = new PaginationHelper(10) {
@Override
public int getItemsCount() {
return getFacade().count();
}
@Override
public DataModel createPageDataModel() {
return new ListDataModel(getFacade().findRange(new int[]{getPageFirstItem(), getPageFirstItem() + getPageSize()}));
}
};
}
return pagination;
}
public String prepareList() {
recreateModel();
return "List";
}
public String prepareView() {
current = (Usermy) getItems().getRowData();
selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex();
return "View";
}
public String prepareCreate() {
current = new Usermy();
selectedItemIndex = -1;
return "Create";
}
public String create() {
try {
getFacade().create(current);
JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("UsermyCreated"));
return prepareCreate();
} catch (Exception e) {
JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
return null;
}
}
public String prepareEdit() {
current = (Usermy) getItems().getRowData();
selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex();
return "Edit";
}
public String update() {
try {
getFacade().edit(current);
JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("UsermyUpdated"));
return "View";
} catch (Exception e) {
JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
return null;
}
}
public String destroy() {
current = (Usermy) getItems().getRowData();
selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex();
performDestroy();
recreatePagination();
recreateModel();
return "List";
}
public String destroyAndView() {
performDestroy();
recreateModel();
updateCurrentItem();
if (selectedItemIndex >= 0) {
return "View";
} else {
// all items were removed - go back to list
recreateModel();
return "List";
}
}
private void performDestroy() {
try {
getFacade().remove(current);
JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("UsermyDeleted"));
} catch (Exception e) {
JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
}
}
private void updateCurrentItem() {
int count = getFacade().count();
if (selectedItemIndex >= count) {
// selected index cannot be bigger than number of items:
selectedItemIndex = count - 1;
// go to previous page if last page disappeared:
if (pagination.getPageFirstItem() >= count) {
pagination.previousPage();
}
}
if (selectedItemIndex >= 0) {
current = getFacade().findRange(new int[]{selectedItemIndex, selectedItemIndex + 1}).get(0);
}
}
public DataModel getItems() {
if (items == null) {
items = getPagination().createPageDataModel();
}
return items;
}
private void recreateModel() {
items = null;
}
private void recreatePagination() {
pagination = null;
}
public String next() {
getPagination().nextPage();
recreateModel();
return "List";
}
public String previous() {
getPagination().previousPage();
recreateModel();
return "List";
}
public SelectItem[] getItemsAvailableSelectMany() {
return JsfUtil.getSelectItems(ejbFacade.findAll(), false);
}
public SelectItem[] getItemsAvailableSelectOne() {
return JsfUtil.getSelectItems(ejbFacade.findAll(), true);
}
public Usermy getUsermy(java.lang.String id) {
return ejbFacade.find(id);
}
@FacesConverter(forClass = Usermy.class)
public static class UsermyControllerConverter implements Converter {
@Override
public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
if (value == null || value.length() == 0) {
return null;
}
UsermyController controller = (UsermyController) facesContext.getApplication().getELResolver().
getValue(facesContext.getELContext(), null, "usermyController");
return controller.getUsermy(getKey(value));
}
java.lang.String getKey(String value) {
java.lang.String key;
key = value;
return key;
}
String getStringKey(java.lang.String value) {
StringBuilder sb = new StringBuilder();
sb.append(value);
return sb.toString();
}
@Override
public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
if (object == null) {
return null;
}
if (object instanceof Usermy) {
Usermy o = (Usermy) object;
return getStringKey(o.getIduser());
} else {
throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: " + Usermy.class.getName());
}
}
}
}