Search in sources :

Example 1 with TextFileLoader

use of org.tinystruct.system.util.TextFileLoader in project bible-online by m0ver.

the class login method oAuth2_github_callback.

public String oAuth2_github_callback() throws ApplicationException {
    HttpServletRequest request = (HttpServletRequest) this.context.getAttribute(HTTP_REQUEST);
    HttpServletResponse response = (HttpServletResponse) this.context.getAttribute(HTTP_RESPONSE);
    Reforward reforward = new Reforward(request, response);
    if (this.getVariable("github_client_secrets") == null) {
        TextFileLoader loader = new TextFileLoader();
        loader.setInputStream(login.class.getResourceAsStream("/clients_secrets.json"));
        builder = new Builder();
        builder.parse(loader.getContent().toString());
        if (builder.get("github") instanceof Builder) {
            builder = (Builder) builder.get("github");
            System.out.println(builder.get("client_secret"));
            System.out.println(builder.get("client_id"));
            this.setVariable(new ObjectVariable("github_client_secrets", builder), false);
        }
    } else
        builder = (Builder) this.getVariable("github_client_secrets").getValue();
    String arguments = this.http_client("https://github.com/login/oauth/access_token?client_id=" + builder.get("client_id") + "&client_secret=" + builder.get("client_secret") + "&code=" + request.getParameter("code"));
    try {
        HttpClient httpClient = new DefaultHttpClient();
        String url = "https://api.github.com/user";
        HttpGet httpget = new HttpGet(url + "?" + arguments);
        httpClient.getParams().setParameter(HttpProtocolParams.HTTP_CONTENT_CHARSET, "UTF-8");
        HttpResponse http_response = httpClient.execute(httpget);
        HeaderIterator iterator = http_response.headerIterator();
        while (iterator.hasNext()) {
            Header next = iterator.nextHeader();
            System.out.println(next.getName() + ":" + next.getValue());
        }
        InputStream instream = http_response.getEntity().getContent();
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] bytes = new byte[1024];
        int len;
        while ((len = instream.read(bytes)) != -1) {
            out.write(bytes, 0, len);
        }
        instream.close();
        out.close();
        Struct struct = new Builder();
        struct.parse(new String(out.toByteArray(), "utf-8"));
        this.usr = new User();
        this.usr.setEmail(struct.toData().getFieldInfo("email").stringValue());
        if (this.usr.findOneByKey("email", this.usr.getEmail()).size() == 0) {
            usr.setPassword("");
            usr.setUsername(usr.getEmail());
            usr.setLastloginIP(request.getRemoteAddr());
            usr.setLastloginTime(new Date());
            usr.setRegistrationTime(new Date());
            usr.append();
        }
        new passport(request, response, "waslogined").setLoginAsUser(this.usr.getId());
        reforward.setDefault(URLDecoder.decode(this.getVariable("from").getValue().toString(), "utf8"));
        reforward.forward();
        return new String(out.toByteArray(), "utf-8");
    } catch (ClientProtocolException e) {
        throw new ApplicationException(e.getMessage(), e);
    } catch (IOException e) {
        throw new ApplicationException(e.getMessage(), e);
    } catch (ParseException e) {
        throw new ApplicationException(e.getMessage(), e);
    }
}
Also used : User(custom.objects.User) Builder(org.tinystruct.data.component.Builder) HttpGet(org.apache.http.client.methods.HttpGet) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) TextFileLoader(org.tinystruct.system.util.TextFileLoader) Struct(org.tinystruct.data.component.Struct) ClientProtocolException(org.apache.http.client.ClientProtocolException) HttpServletRequest(javax.servlet.http.HttpServletRequest) ObjectVariable(org.tinystruct.system.template.variable.ObjectVariable) HttpServletResponse(javax.servlet.http.HttpServletResponse) HttpResponse(org.apache.http.HttpResponse) Date(java.util.Date) ApplicationException(org.tinystruct.ApplicationException) Header(org.apache.http.Header) Reforward(org.tinystruct.handler.Reforward) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) HeaderIterator(org.apache.http.HeaderIterator) ParseException(org.apache.http.ParseException)

Example 2 with TextFileLoader

use of org.tinystruct.system.util.TextFileLoader in project bible-online by m0ver.

the class error method not_found.

public String not_found() throws ApplicationException {
    final error app = this;
    this.request = (HttpServletRequest) this.context.getAttribute(HTTP_REQUEST);
    this.response = (HttpServletResponse) this.context.getAttribute(HTTP_RESPONSE);
    this.response.setStatus(HttpServletResponse.SC_NOT_FOUND);
    this.setVariable("action", this.config.get("default.base_url") + this.context.getAttribute("REQUEST_ACTION").toString());
    this.setVariable("base_url", String.valueOf(this.context.getAttribute("HTTP_HOST")));
    HttpSession session = this.request.getSession();
    if (session.getAttribute("usr") != null) {
        this.usr = (User) session.getAttribute("usr");
        this.setVariable("user.status", "");
        this.setVariable("user.profile", "<a href=\"javascript:void(0)\" onmousedown=\"profileMenu.show(event,'1')\">" + this.usr.getEmail() + "</a>");
        this.setVariable("scripts", "$.ajax({url:\"" + this.getLink("services/getwords") + "\",dataType:\"xml\",type:'GET'}).success(function(data){data=wordsXML(data);ldialog.show(data);});");
    } else {
        this.setVariable("user.status", "<a href=\"" + this.getLink("user/login") + "\">" + this.getProperty("page.login.caption") + "</a>");
        this.setVariable("user.profile", "");
        this.setVariable("scripts", "");
    }
    this.setTemplate(new Template() {

        private String template_path;

        ConcurrentHashMap<String, Variable<?>> variables = Variables.getInstance();

        @Override
        public String getName() {
            // TODO Auto-generated method stub
            return "404";
        }

        @Override
        public void setVariable(Variable<?> variable) {
            app.setVariable(variable, true);
        }

        @Override
        public Variable<?> getVariable(String name) {
            // TODO Auto-generated method stub
            return app.getVariable(name);
        }

        @Override
        public Map<String, Variable<?>> getVariables() {
            // TODO Auto-generated method stub
            return variables;
        }

        @Override
        public String parse() throws ApplicationException {
            // TODO Auto-generated method stub
            InputStream in;
            this.template_path = "themes" + File.separatorChar + this.getName() + ".view";
            in = AbstractApplication.class.getClassLoader().getResourceAsStream(this.template_path);
            TextFileLoader loader = new TextFileLoader(in);
            loader.setCharset(config.get("charset").toString());
            String tpl, value;
            tpl = loader.getContent().toString();
            Set<Entry<String, Variable<?>>> sets = variables.entrySet();
            Iterator<Entry<String, Variable<?>>> iterator = sets.iterator();
            List<Variable<?>> list = new ArrayList<Variable<?>>();
            Variable<?> variable;
            while (iterator.hasNext()) {
                Entry<String, Variable<?>> v = iterator.next();
                variable = v.getValue();
                if (variable.getType() == DataType.ARRAY) {
                    list.add(variable);
                } else {
                    if (v.getKey().startsWith("[%LINK:")) {
                        String base_url;
                        if (context != null && context.getAttribute("HTTP_HOST") != null)
                            base_url = context.getAttribute("HTTP_HOST").toString();
                        else
                            base_url = config.get("default.base_url");
                        value = base_url + variable.getValue();
                    } else
                        value = variable.getValue().toString();
                    tpl = tpl.replace(v.getKey(), value);
                }
            }
            return tpl;
        }
    });
    return this.getOutputText();
}
Also used : Variable(org.tinystruct.system.template.variable.Variable) HttpSession(javax.servlet.http.HttpSession) InputStream(java.io.InputStream) Template(org.tinystruct.application.Template) TextFileLoader(org.tinystruct.system.util.TextFileLoader) Entry(java.util.Map.Entry) ApplicationException(org.tinystruct.ApplicationException) AbstractApplication(org.tinystruct.AbstractApplication) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Aggregations

ApplicationException (org.tinystruct.ApplicationException)2 TextFileLoader (org.tinystruct.system.util.TextFileLoader)2 User (custom.objects.User)1 InputStream (java.io.InputStream)1 Date (java.util.Date)1 Entry (java.util.Map.Entry)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 HttpSession (javax.servlet.http.HttpSession)1 Header (org.apache.http.Header)1 HeaderIterator (org.apache.http.HeaderIterator)1 HttpResponse (org.apache.http.HttpResponse)1 ParseException (org.apache.http.ParseException)1 ClientProtocolException (org.apache.http.client.ClientProtocolException)1 HttpClient (org.apache.http.client.HttpClient)1 HttpGet (org.apache.http.client.methods.HttpGet)1 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)1 AbstractApplication (org.tinystruct.AbstractApplication)1 Template (org.tinystruct.application.Template)1