use of io.milton.http.exceptions.BadRequestException in project lobcder by skoulouzis.
the class MiltonFtpAdapter method createFileSystemView.
@Override
public FileSystemView createFileSystemView(User user) throws FtpException {
MiltonUser mu = (MiltonUser) user;
Resource root;
try {
root = resourceFactory.getResource(mu.domain, "/");
} catch (NotAuthorizedException ex) {
throw new FtpException(ex);
} catch (BadRequestException ex) {
throw new FtpException(ex);
}
return new MiltonFsView(Path.root, (CollectionResource) root, resourceFactory, (MiltonUser) user);
}
use of io.milton.http.exceptions.BadRequestException in project lobcder by skoulouzis.
the class SearchRunnable method run.
@Override
public void run() {
try {
int size = 0;
LogUtils.debug(log, "LOG_LDAP_REQ_SEARCH", currentMessageId, dn, scope, sizeLimit, timelimit, ldapFilter.toString(), returningAttributes);
if (scope == Ldap.SCOPE_BASE_OBJECT) {
log.debug("Check type of search... scope is BASE OBJECT");
if ("".equals(dn)) {
size = 1;
log.info("send root DSE");
responseHandler.sendRootDSE(currentMessageId);
} else if (Ldap.BASE_CONTEXT.equals(dn)) {
size = 1;
// root
// root
log.info("send base context");
responseHandler.sendBaseContext(currentMessageId);
} else if (dn.startsWith("uid=") && dn.indexOf(',') > 0) {
if (user != null) {
// single user request
// single user request
String uid = dn.substring("uid=".length(), dn.indexOf(','));
Set<LdapContact> persons = null;
// first search in contact
try {
// check if this is a contact uid
Integer.parseInt(uid);
persons = contactFind(conditions.isEqualTo("imapUid", uid), returningAttributes, sizeLimit);
} catch (NumberFormatException e) {
// ignore, this is not a contact uid
}
// then in GAL
if (persons == null || persons.isEmpty()) {
List<LdapContact> galContacts = null;
try {
log.info("do GAL search: " + uid);
galContacts = userFactory.galFind(conditions.isEqualTo("imapUid", uid), sizeLimit);
} catch (NotAuthorizedException ex) {
log.error("not auth", ex);
} catch (BadRequestException ex) {
log.error("bad req", ex);
}
if (galContacts != null && galContacts.size() > 0) {
LdapContact person = galContacts.get(0);
if (persons == null) {
persons = new HashSet<LdapContact>();
}
persons.add(person);
}
}
size = persons == null ? 0 : persons.size();
try {
sendPersons(currentMessageId, dn.substring(dn.indexOf(',')), persons, returningAttributes);
} catch (NotAuthorizedException ex) {
log.error("Not authorised", ex);
} catch (BadRequestException ex) {
log.error("bad req", ex);
}
} else {
LogUtils.debug(log, "LOG_LDAP_REQ_SEARCH_ANONYMOUS_ACCESS_FORBIDDEN", currentMessageId, dn);
}
} else {
LogUtils.debug(log, "LOG_LDAP_REQ_SEARCH_INVALID_DN (1)", currentMessageId, dn);
}
} else if (Ldap.COMPUTER_CONTEXT.equals(dn) || Ldap.COMPUTER_CONTEXT_LION.equals(dn)) {
size = 1;
// computer context for iCal
log.info("send computer context");
responseHandler.sendComputerContext(currentMessageId, returningAttributes);
} else if ((// Outlook 2010 by default sends no DN
dn.equals("") || Ldap.BASE_CONTEXT.equalsIgnoreCase(dn) || Ldap.OD_USER_CONTEXT.equalsIgnoreCase(dn)) || Ldap.MSLIVE_BASE_CONTEXT.equals(dn) || Ldap.OD_USER_CONTEXT_LION.equalsIgnoreCase(dn)) {
log.info("not a weird search... check for normal conditions");
if (user != null) {
log.debug("we have a user...");
Set<LdapContact> persons = new HashSet<LdapContact>();
if (ldapFilter.isFullSearch()) {
// append personal contacts first
log.info("do personcal contact search");
Set<LdapContact> contacts = contactFind(null, returningAttributes, sizeLimit);
LogUtils.debug(log, "fullSearch: results:", contacts.size());
for (LdapContact person : contacts) {
persons.add(person);
if (persons.size() == sizeLimit) {
break;
}
}
// full search
for (char c = 'A'; c <= 'Z'; c++) {
if (!abandon && persons.size() < sizeLimit) {
Condition startsWith = conditions.startsWith("cn", String.valueOf(c));
Collection<LdapContact> galContacts = null;
try {
log.info("now do GAL search");
galContacts = userFactory.galFind(startsWith, sizeLimit);
} catch (NotAuthorizedException ex) {
log.error("not auth", ex);
} catch (BadRequestException ex) {
log.error("bad req", ex);
}
if (galContacts != null) {
LogUtils.debug(log, "doSearch: results:", contacts.size());
for (LdapContact person : galContacts) {
persons.add(person);
if (persons.size() == sizeLimit) {
break;
}
}
}
}
if (persons.size() == sizeLimit) {
break;
}
}
} else {
// append only personal contacts
log.info("do personcal contact search only");
Condition filter = ldapFilter.getContactSearchFilter();
LogUtils.debug(log, "not full search:", filter);
// ignored all attribute filters => return empty results
if (ldapFilter.isFullSearch() || filter != null) {
Set<LdapContact> contacts = contactFind(filter, returningAttributes, sizeLimit);
for (LdapContact person : contacts) {
persons.add(person);
if (persons.size() == sizeLimit) {
log.debug("EXceeded size limit1");
break;
}
}
LogUtils.trace(log, "local contacts result size: ", persons.size());
if (!abandon && persons.size() < sizeLimit) {
List<LdapContact> galContacts = null;
try {
galContacts = ldapFilter.findInGAL(user, returningAttributes, sizeLimit - persons.size());
} catch (NotAuthorizedException ex) {
log.error("not auth", ex);
} catch (BadRequestException ex) {
log.error("bad req", ex);
}
if (galContacts != null) {
LogUtils.trace(log, "gal contacts result size: ", galContacts.size());
for (LdapContact person : galContacts) {
if (persons.size() >= sizeLimit) {
log.debug("EXceeded size limit2");
break;
}
LogUtils.trace(log, "add contact to results: ", person.getName());
persons.add(person);
}
}
}
}
}
LogUtils.debug(log, "LOG_LDAP_REQ_SEARCH_FOUND_RESULTS", currentMessageId, persons.size());
try {
sendPersons(currentMessageId, ", " + dn, persons, returningAttributes);
} catch (NotAuthorizedException ex) {
log.error("not auth", ex);
} catch (BadRequestException ex) {
log.error("bad req", ex);
}
LogUtils.debug(log, "LOG_LDAP_REQ_SEARCH_END", currentMessageId);
} else {
LogUtils.debug(log, "LOG_LDAP_REQ_SEARCH_ANONYMOUS_ACCESS_FORBIDDEN", currentMessageId, dn);
}
} else if (dn != null && dn.length() > 0 && !Ldap.OD_CONFIG_CONTEXT.equals(dn) && !Ldap.OD_GROUP_CONTEXT.equals(dn)) {
LogUtils.debug(log, "LOG_LDAP_REQ_SEARCH_INVALID_DN (2)", currentMessageId, dn);
log.debug("DN is not equal to: " + Ldap.OD_CONFIG_CONTEXT + " or " + Ldap.OD_GROUP_CONTEXT + " or any other valid pattern. Is: " + dn);
} else {
log.warn("Search criteria didnt match any of the expected patterns. Perhaps the user name is missing a context? DN=" + dn + ", expected something like: " + Ldap.OD_USER_CONTEXT);
}
// iCal: do not send LDAP_SIZE_LIMIT_EXCEEDED on apple-computer search by cn with sizelimit 1
if (size > 1 && size == sizeLimit) {
LogUtils.debug(log, "LOG_LDAP_REQ_SEARCH_SIZE_LIMIT_EXCEEDED", currentMessageId);
responseHandler.sendClient(currentMessageId, Ldap.LDAP_REP_RESULT, Ldap.LDAP_SIZE_LIMIT_EXCEEDED, "");
} else {
log.debug("No search results");
LogUtils.debug(log, "LOG_LDAP_REQ_SEARCH_SUCCESS", currentMessageId);
responseHandler.sendClient(currentMessageId, Ldap.LDAP_REP_RESULT, Ldap.LDAP_SUCCESS, "");
}
} catch (SocketException e) {
log.warn("closed connection", e);
} catch (IOException e) {
log.error("", e);
try {
responseHandler.sendErr(currentMessageId, Ldap.LDAP_REP_RESULT, e);
} catch (IOException e2) {
LogUtils.debug(log, "LOG_EXCEPTION_SENDING_ERROR_TO_CLIENT", e2);
}
} finally {
log.debug("search complete");
searchManager.searchComplete(uuid, currentMessageId);
}
}
use of io.milton.http.exceptions.BadRequestException in project lobcder by skoulouzis.
the class WebDataFileResource method delete.
@Override
public void delete() throws NotAuthorizedException, BadRequestException, ConflictException {
Logger.getLogger(WebDataFileResource.class.getName()).log(Level.FINEST, "delete() file {0}", getPath());
try (Connection connection = getCatalogue().getConnection()) {
try {
getCatalogue().remove(getLogicalData(), getPrincipal(), connection);
connection.commit();
} catch (Exception e) {
Logger.getLogger(WebDataFileResource.class.getName()).log(Level.SEVERE, null, e);
connection.rollback();
throw new BadRequestException(this, e.getMessage());
}
} catch (SQLException e) {
Logger.getLogger(WebDataFileResource.class.getName()).log(Level.SEVERE, null, e);
throw new BadRequestException(this, e.getMessage());
}
}
use of io.milton.http.exceptions.BadRequestException in project lobcder by skoulouzis.
the class WebDataFileResource method sendContent.
@Override
public void sendContent(OutputStream out, Range range, Map<String, String> params, String contentType) throws IOException, NotAuthorizedException, BadRequestException, NotFoundException {
double start = System.currentTimeMillis();
PDRI pdri;
Iterator<PDRIDescr> it;
try {
List<PDRIDescr> pdris = getCatalogue().getPdriDescrByGroupId(getLogicalData().getPdriGroupId());
if (pdris.size() <= 0) {
throw new NotFoundException("File inconsistency! Could not find physical file!");
}
// it = getCatalogue().getPdriDescrByGroupId(getLogicalData().getPdriGroupId()).iterator();
if (range != null) {
if (range.getFinish() == null) {
range = new Range(range.getStart(), (getLogicalData().getLength() - 1));
}
it = pdris.iterator();
Logger.getLogger(WebDataFileResource.class.getName()).log(Level.FINEST, "Start: {0} end: {1} range: {2}", new Object[] { range.getStart(), range.getFinish(), range.getRange() });
pdri = transfererRange(it, out, 0, null, range);
} else {
// pdri = transfer(it, out, 0, null, false);
pdri = transfer(pdris, out, 0, false);
}
} catch (SQLException ex) {
throw new BadRequestException(this, ex.getMessage());
} catch (IOException ex) {
if (ex.getMessage().contains("Resource not found") || ex.getMessage().contains("Couldn't locate path")) {
throw new NotFoundException(ex.getMessage());
} else {
throw new BadRequestException(this, ex.getMessage());
}
} finally {
// Don't close the output, we need it to send back the response
// if (out != null) {
// out.flush();
// out.close();
// }
}
double elapsed = System.currentTimeMillis() - start;
long len;
if (range != null) {
len = range.getFinish() - range.getStart() + 1;
} else {
len = this.getLogicalData().getLength();
}
double speed = ((len * 8.0) * 1000.0) / (elapsed * 1000.0);
Double oldSpeed = weightPDRIMap.get(pdri.getHost());
if (oldSpeed == null) {
oldSpeed = speed;
}
Integer numOfGets = numOfGetsMap.get(pdri.getHost());
if (numOfGets == null) {
numOfGets = 1;
}
double averagre = (speed + oldSpeed) / (double) numOfGets;
numOfGetsMap.put(pdri.getHost(), numOfGets++);
weightPDRIMap.put(pdri.getHost(), averagre);
getCatalogue().addViewForRes(getLogicalData().getUid());
Stats stats = new Stats();
stats.setSource(pdri.getHost());
stats.setDestination(fromAddress);
stats.setSpeed(speed);
stats.setSize(getLogicalData().getLength());
String msg = "Source: " + stats.getSource() + " Destination: " + stats.getDestination() + " Tx_Speed: " + speed + " Kbites/sec Tx_Size: " + getLogicalData().getLength() + " bytes Elapsed_Time: " + elapsed + " ms";
try {
if (!pdri.isCahce()) {
getCatalogue().setSpeed(stats);
}
} catch (SQLException ex) {
Logger.getLogger(WebDataFileResource.class.getName()).log(Level.SEVERE, null, ex);
}
Logger.getLogger(WebDataFileResource.class.getName()).log(Level.INFO, msg);
}
use of io.milton.http.exceptions.BadRequestException in project lobcder by skoulouzis.
the class LockHandler method process.
@Override
public void process(HttpManager httpManager, Request request, Response response) throws ConflictException, NotAuthorizedException, BadRequestException, NotFoundException {
if (!handlerHelper.checkExpects(responseHandler, request, response)) {
return;
}
String host = request.getHostHeader();
String url = HttpManager.decodeUrl(request.getAbsolutePath());
Resource r = httpManager.getResourceFactory().getResource(host, url);
try {
// Find a resource if it exists
if (r != null) {
processExistingResource(httpManager, request, response, r);
} else {
processNonExistingResource(httpManager, request, response, host, url);
}
} catch (IOException ex) {
throw new BadRequestException(r);
} catch (SAXException ex) {
throw new BadRequestException(r);
} catch (LockedException ex) {
responseHandler.respondLocked(request, response, r);
} catch (PreConditionFailedException ex) {
responseHandler.respondPreconditionFailed(request, response, r);
}
}
Aggregations