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);
}
}
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();
}
Aggregations