use of de.janrufmonitor.service.commons.http.handler.HandlerException in project janrufmonitor by tbrandt77.
the class SetCallList method handleWithException.
public void handleWithException(IHttpRequest req, IMutableHttpResponse resp) throws HandlerException {
ICallManager mgr = null;
String manager = null;
boolean isCompression = false;
try {
manager = req.getParameter(RemoveCallList.PARAMETER_CALLMANAGER);
isCompression = (req.getParameter(GetCallList.PARAMETER_COMPRESSION) != null ? req.getParameter(GetCallList.PARAMETER_COMPRESSION).equalsIgnoreCase("true") : false);
} catch (Exception e) {
throw new HandlerException(e.getMessage(), 500);
}
if (manager == null)
mgr = this.getRuntime().getCallManagerFactory().getDefaultCallManager();
if (manager != null && manager.length() > 0)
mgr = this.getRuntime().getCallManagerFactory().getCallManager(manager);
if (mgr == null || !mgr.isActive() || !mgr.isSupported(IWriteCallRepository.class)) {
throw new HandlerException("Requested Callermanager does not exist or is not active.", 404);
}
ICallList l;
try {
byte[] data = this.getPostData(req).getBytes();
if (isCompression) {
data = CompressBase64.decompressBase64Decode(data);
}
l = XMLSerializer.toCallList(new String(data));
if (l != null) {
this.m_logger.info("Setting call list with " + l.size() + " entries.");
((IWriteCallRepository) mgr).setCalls(l);
resp.getContentStreamForWrite().close();
} else {
this.m_logger.severe("Invalid call list transfered from client.");
throw new HandlerException("Invalid call list transfered from client.", 500);
}
} catch (Exception e) {
throw new HandlerException(e.getMessage(), 500);
}
}
use of de.janrufmonitor.service.commons.http.handler.HandlerException in project janrufmonitor by tbrandt77.
the class Unregister method handleWithException.
public void handleWithException(IHttpRequest req, IMutableHttpResponse resp) throws HandlerException {
String client = "";
String clientip = "";
try {
client = req.getInetAddress().getHostName();
clientip = req.getInetAddress().getHostAddress();
if (clientip == null)
throw new HandlerException("No valid IP address.", 403);
if (client == null)
client = "";
} catch (Exception ex) {
throw new HandlerException(ex.getMessage(), 500);
}
if (client.length() > 0 || clientip.length() > 0) {
try {
String s_port = req.getParameter(Unregister.PARAMETER_CLIENT_PORT);
if (s_port == null)
throw new HandlerException("No client call back port.", 403);
int port = 0;
if (s_port.length() > 0) {
port = Integer.parseInt(s_port);
}
this.m_logger.info("Unregistering client " + client + ":" + port);
ClientRegistry.getInstance().unregister(new Client(client, clientip, port));
resp.getContentStreamForWrite().close();
} catch (Exception e) {
throw new HandlerException(e.getMessage(), 500);
}
}
}
use of de.janrufmonitor.service.commons.http.handler.HandlerException in project janrufmonitor by tbrandt77.
the class Image method handleWithException.
public void handleWithException(IHttpRequest req, IMutableHttpResponse resp) throws HandlerException {
ICallerManager mgr = null;
String manager = null;
try {
manager = req.getParameter(Image.PARAMETER_CALLERMANAGER);
} catch (Exception e) {
throw new HandlerException(e.getMessage(), 500);
}
if (manager == null)
mgr = this.getRuntime().getCallerManagerFactory().getDefaultCallerManager();
if (manager != null && manager.length() > 0)
mgr = this.getRuntime().getCallerManagerFactory().getCallerManager(manager);
if (mgr == null || !mgr.isActive() || !mgr.isSupported(IIdentifyCallerRepository.class)) {
throw new HandlerException("Requested Callermanager does not exist or is not active.", 404);
}
String number = null;
try {
number = req.getParameter(Image.PARAMETER_NUMBER);
} catch (Exception e) {
throw new HandlerException(e.getMessage(), 500);
}
if (number == null || number.length() == 0) {
this.m_logger.severe("Parameter &number= was empty or not set.");
throw new HandlerException("Parameter &number= was empty or not set.", 404);
}
IPhonenumber pn = null;
StringTokenizer st = new StringTokenizer(number, ";");
if (st.countTokens() == 3) {
pn = this.getRuntime().getCallerFactory().createPhonenumber(st.nextToken().trim(), st.nextToken().trim(), st.nextToken().trim());
}
if (st.countTokens() == 2) {
pn = this.getRuntime().getCallerFactory().createPhonenumber(st.nextToken().trim(), "", st.nextToken().trim());
}
if (st.countTokens() == 1) {
pn = this.getRuntime().getCallerFactory().createPhonenumber(st.nextToken().trim());
}
try {
ICaller caller = ((IIdentifyCallerRepository) mgr).getCaller(pn);
IAttribute imageAtt = caller.getAttribute(IJAMConst.ATTRIBUTE_NAME_IMAGEPATH);
if (ImageHandler.getInstance().hasImage(caller)) {
InputStream in = ImageHandler.getInstance().getImageStream(caller);
if (in != null) {
resp.setParameter("Content-Type", this.getMimetype("jpg"));
OutputStream ps = resp.getContentStreamForWrite();
byte[] buffer = new byte[8092];
int bytesRead;
while ((bytesRead = in.read(buffer)) != -1) {
ps.write(buffer, 0, bytesRead);
}
in.close();
ps.flush();
ps.close();
}
} else if (imageAtt != null) {
String pathToImage = PathResolver.getInstance(getRuntime()).resolve(imageAtt.getValue());
File image = new File(pathToImage);
if (image.exists()) {
resp.setParameter("Content-Type", this.getMimetype(pathToImage));
resp.setParameter("Content-Length", Long.toString(image.length()));
OutputStream ps = resp.getContentStreamForWrite();
FileInputStream in = new FileInputStream(image);
byte[] buffer = new byte[8092];
int bytesRead;
while ((bytesRead = in.read(buffer)) != -1) {
ps.write(buffer, 0, bytesRead);
}
in.close();
ps.flush();
ps.close();
} else {
throw new CallerNotFoundException("Image " + pathToImage + " not found");
}
} else {
throw new CallerNotFoundException("No image assigned for caller " + caller);
}
} catch (CallerNotFoundException e) {
throw new HandlerException(e.getMessage(), 404);
} catch (Exception e) {
throw new HandlerException(e.getMessage(), 500);
}
}
use of de.janrufmonitor.service.commons.http.handler.HandlerException in project janrufmonitor by tbrandt77.
the class IdentifiedCall method handleWithException.
public void handleWithException(IHttpRequest req, IMutableHttpResponse resp) throws HandlerException {
try {
ICall call = XMLSerializer.toCall(this.getPostData(req));
if (call != null) {
ClientCallMap.getInstance().setCall((call.getCaller().getPhoneNumber().isClired() ? IJAMConst.CLIRED_CALL : call.getCaller().getPhoneNumber().getTelephoneNumber()) + "/" + call.getMSN().getMSN(), call);
Thread sender = new Thread(new HandlerThread(call, this));
sender.start();
resp.getContentStreamForWrite().close();
} else {
this.m_logger.severe("Invalid call transfered from server.");
}
} catch (Exception e) {
throw new HandlerException(e.getMessage(), 500);
}
}
use of de.janrufmonitor.service.commons.http.handler.HandlerException in project janrufmonitor by tbrandt77.
the class Rejected method handleWithException.
public void handleWithException(IHttpRequest req, IMutableHttpResponse resp) throws HandlerException {
try {
String phone = req.getParameter(Rejected.PARAMETER_NUMBER);
String msn = req.getParameter(Rejected.PARAMETER_MSN);
if (phone == null || phone.length() == 0)
phone = IJAMConst.CLIRED_CALL;
ICall call = ClientCallMap.getInstance().getCall(phone + "/" + msn);
this.m_logger.info("Key for ClientCallMap: " + phone + "/" + msn);
if (call != null) {
long end = new Date().getTime();
long start = this.getStartTime(call);
call.setAttribute(this.getRuntime().getCallFactory().createAttribute(IJAMConst.ATTRIBUTE_NAME_ENDRING, Long.toString(end)));
call.setAttribute(this.getRuntime().getCallFactory().createAttribute(IJAMConst.ATTRIBUTE_NAME_RINGDURATION, Long.toString((end - start) / 1000)));
call.setAttribute(this.getRuntime().getCallFactory().createAttribute(IJAMConst.ATTRIBUTE_NAME_CALLSTATUS, IJAMConst.ATTRIBUTE_VALUE_REJECTED));
if (ClientCallMap.getInstance().removeCall(phone + "/" + msn))
this.m_logger.info("Successfully found call: " + phone + "/" + msn);
Thread sender = new Thread(new HandlerThread(call, this));
sender.start();
resp.getContentStreamForWrite().close();
}
} catch (Exception e) {
throw new HandlerException(e.getMessage(), 500);
}
}
Aggregations