Skip to content

Commit

Permalink
Implementação para converter byte[] em base64 e vise-versa
Browse files Browse the repository at this point in the history
  • Loading branch information
osnimarin committed Oct 6, 2016
1 parent 2c197f4 commit 21eb290
Show file tree
Hide file tree
Showing 9 changed files with 1,042 additions and 802 deletions.
248 changes: 128 additions & 120 deletions angular-beans/src/main/java/angularBeans/remote/HalfDuplexEndPoint.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
/*
* AngularBeans, CDI-AngularJS bridge
*
* Copyright (c) 2014, Bessem Hmidi. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
*/

package angularBeans.remote;

import java.io.BufferedReader;
Expand Down Expand Up @@ -32,128 +50,118 @@
@SuppressWarnings("serial")
@WebServlet(asyncSupported = true, urlPatterns = Constants.URL_PATTERNS)
public class HalfDuplexEndPoint extends HttpServlet implements Serializable {

public static final String URL_PATTERNS = "/http/invoke/*";

@Inject
InvocationHandler remoteInvoker;

@Inject
BeanLocator locator;

@Inject
AngularBeansUtils util;

@Inject
HttpSession session;
@Inject
InvocationHandler remoteInvoker;

@Inject
BeanLocator locator;

@Inject
AngularBeansUtils util;

@Inject
HttpSession session;

@Inject
@DataReceivedEvent
private Event<DataReceived> receiveEvents;

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
process(req, resp);
}

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
process(req, resp);
}

@Override
protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
process(req, resp);
}

@Override
protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
process(req, resp);
}

@Override
protected void doOptions(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
process(request, response);

}

private void process(HttpServletRequest request, HttpServletResponse resp) {

AsyncContext asyncContext = request.startAsync();

resp.setCharacterEncoding("UTF-8");

if (request.getRequestURL().toString().endsWith("/CORS")) {
resp.addHeader("Access-Control-Allow-Origin", "*");
resp.addHeader("Access-Control-Allow-Methods", "POST, GET, PUT, DELETE");
resp.addHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
}

// Just ACCEPT and REPLY OK if OPTIONS
if (request.getMethod().equals("OPTIONS")) {
resp.setStatus(HttpServletResponse.SC_OK);

}

String fullPath = request.getRequestURI();
fullPath = (fullPath.substring(fullPath.indexOf("/service/") + 9));

String parts[] = fullPath.split("/");

String beanName = parts[0];
String method = parts[1];

String params = request.getParameter("params");

if (request.getMethod().equals("POST")) {
try {
StringBuilder buffer = new StringBuilder();
BufferedReader reader;

reader = request.getReader();

String line;

while ((line = reader.readLine()) != null) {
buffer.append(line);
}

params = buffer.toString();

}
catch (Exception e) {
// TODO: handle exception
}
}

JsonObject paramsObj = CommonUtils.parse(params).getAsJsonObject();

String UID = session.getId();// (paramsObj.get("sessionUID")).getAsString();

NGSessionScopeContext.setCurrentContext(UID);

receiveEvents.fire(new HalfDuplexDataReceivedEvent(paramsObj));

@Inject
@DataReceivedEvent
private Event<DataReceived> receiveEvents;
Object result = remoteInvoker.invoke(locator.lookup(beanName, UID), method, paramsObj, UID, request);

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
process(req, resp);
}

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
process(req, resp);
}

@Override
protected void doPut(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
process(req, resp);
}

@Override
protected void doDelete(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
process(req, resp);
}


@Override
protected void doOptions(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
process(request, response);

}
private void process(HttpServletRequest request, HttpServletResponse resp) {

AsyncContext asyncContext=request.startAsync();

resp.setCharacterEncoding("UTF-8");

if(request.getRequestURL().toString().endsWith("/CORS")){
resp.addHeader("Access-Control-Allow-Origin", "*");
resp.addHeader("Access-Control-Allow-Methods", "POST, GET, PUT, DELETE");
resp.addHeader("Access-Control-Allow-Headers","Origin, X-Requested-With, Content-Type, Accept");
}

// Just ACCEPT and REPLY OK if OPTIONS
if ( request.getMethod().equals("OPTIONS") ) {
resp.setStatus(HttpServletResponse.SC_OK);

}

String fullPath = request.getRequestURI();
fullPath = (fullPath.substring(fullPath.indexOf("/service/") + 9));

String parts[] = fullPath.split("/");

String beanName = parts[0];
String method = parts[1];

String params = request.getParameter("params");

if (request.getMethod().equals("POST")) {
try {
StringBuilder buffer = new StringBuilder();
BufferedReader reader;

reader = request.getReader();

String line;

while ((line = reader.readLine()) != null) {
buffer.append(line);
}

params = buffer.toString();

} catch (Exception e) {
// TODO: handle exception
}
}

JsonObject paramsObj = CommonUtils.parse(params).getAsJsonObject();

String UID = session.getId();// (paramsObj.get("sessionUID")).getAsString();

NGSessionScopeContext.setCurrentContext(UID);



receiveEvents.fire(new HalfDuplexDataReceivedEvent(paramsObj));

Object result = remoteInvoker.invoke(locator.lookup(beanName, UID),
method, paramsObj, UID,request);



try {
PrintWriter writer=asyncContext.getResponse().getWriter();
writer.write( util.getJson(result));
writer.flush();
asyncContext.complete();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
PrintWriter writer = asyncContext.getResponse().getWriter();
writer.write(util.getJson(result));
writer.flush();
asyncContext.complete();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}
Loading

0 comments on commit 21eb290

Please sign in to comment.