I am new with using Ldap API for Java and I want to add a user to a group but I'm failing all the time.
I am using this code for adding specific user to a specific group:
private void insertUserToGroup(List<DistinguishedName> memberOf, DistinguishedName newUserDN) {
try
{
// Loop all groups to put the user in.
for(DistinguishedName groupDn : memberOf) {
String encodedGroupDn = groupDn.encode(); // Example: "cn=GROUP_SKL, ou=roles"
String encodedUserDn = newUserDN.encode(); // Example: "cn=user_dfh, ou=external"
// Now. Add user to a group.
ModificationItem member[] = new ModificationItem[1];
member[0] = new ModificationItem(DirContext.ADD_ATTRIBUTE, new BasicAttribute("member", encodedUserDn));
ldapTemplate.modifyAttributes(encodedGroupDn, member);
}
} catch ( InvalidAttributeValueException exc ) {
throw exc;
} catch ( NameAlreadyBoundException exc ) {
throw exc;
} catch ( NameNotFoundException exc ) {
throw exc;
} catch (Exception exc) {
throw exc;
}
}
The input arguments is list of groups and the user and you can see in the example comments how the DistinguishedName will look like.
1. And the exception i get when i run ldapTemplate.modifyAttributes(encodedGroupDn, member); is:
org.springframework.ldap.NameNotFoundException: [LDAP: error code 32 - 00000525: NameErr: DSID-031A11CC, problem 2001 (NO_OBJECT), data 0, best match of:
''
];
nested exception is javax.naming.NameNotFoundException: [LDAP: error code 32 - 00000525: NameErr: DSID-031A11CC, problem 2001 (NO_OBJECT), data 0, best match of:
''
];
remaining name 'cn=GROUP_SKL, ou=roles'
2. I have tried to run modifyAttributes(...) with other attributes like "description", "company" and that works for both Groups and Users but not the attribute "member".
3. So the question is. What name do it expect? Is the DistinguishedName wrong? Or is this way to add a user to a group totally wrong? Or is it some details I am missing?
Now I found the error.
All I needed was to use the full address for the User DN. So I think that 'member' attribute for the group must have the full address. So both DN looks like this now:
encodedGroupDn = "cn=GROUP_SKL, ou=roles"
encodedUserDn = "cn=user_dfh, ou=external, ou=main, dc=nr, dc=company, dc=local"
And that works!
Related
This is a standard error I receive for all Errors parsed from Database Triggers. I use Payara5 as the Application Server and Netbeans 8.2 IDE. In the instance on the picture, it was supposed to display "ID Number mandatory for applicants order that 18 years of age".
How do I make sure that the exact error as in the trigger, appears on the Web Application?
Given your stacktrace it looks like you need to remove the ExceptionUtils.findRootException(ex).getMessage() and just use ex.getMessage() since the thrown topmost Exception already contains the message that you need.
I would try with the following code when an Exception is thrown:
catch (Exception ex) {
JSFUtils.addMessageSessionError(
ExceptionUtils.formatException(AdHocTools.getCurrentMethodName(),
ex.getMessage());
}
However, ExceptionUtils.findRootException(ex).getMessage() might be there for a reason. There are cases where the topmost Exception is pretty general (e.g. an EJBException) and you really need to get to the root exception to get a meaningful message.
You could also try with this code which returns an SQLException if applicable and in other cases the root Exception.
catch (Exception ex) {
Throwable rootCause = ex;
while (rootCause.getCause() != null && rootCause.getCause() != rootCause) {
if ( rootCause instanceof java.sql.SQLException ) {
break;
}
rootCause = rootCause.getCause();
}
JSFUtils.addMessageSessionError(
ExceptionUtils.formatException(AdHocTools.getCurrentMethodName(),
rootCause.getMessage());
}
What does the e mean in the following code?
try {
// Do something
} catch (Exception e) {
// Do something
}
I've been researching and have gotten nothing.
System.out.println("Thanks!");
It's a variable name. Exception is the type. e is the name. You can use a different name. You might display a message to the user (or a stack trace).
try {
// Do something
} catch (Exception ohNo) {
System.out.printf("Caught exception %s doing something.%n", ohNo.toString());
ohNo.printStackTrace();
}
It is a object which contain info about an error happend.
Inherit from throwable and give you a clear message of why your code went wrong
More info
enter link description here
enter link description here
I have this simple model written in Alloy:
module login
sig Email {}
sig Password {}
sig User {
login: one Login
}
sig Login {
email: one Email,
password: one Password,
owner: one User,
}
fact {
all u:User | u.login.owner = u
}
assert a {
all l:Login | one l.owner
all u:User | one u.login.email
all u:User | u.login.owner = u
}
check a for 3
If I run this with the alloy analyser GUI it says:
No counterexample found. Assertion may be valid. 11ms.
But if I run the same model with the API in my java program it returns:
---OUTCOME---
Unsatisfiable.
And not even 1 solutions is shown.
Can anyone help me to detect the problem?
Here goes the code in java using the API:
A4Reporter rep = new A4Reporter();
try {
Module loaded_model = CompUtil.parseEverything_fromFile(rep, null, model.getModelpath());
A4Options options = new A4Options();
options.solver = A4Options.SatSolver.SAT4J;
Command cmd = loaded_model.getAllCommands().get(0);
A4Solution sol = TranslateAlloyToKodkod.execute_command(rep, loaded_model.getAllReachableSigs(), cmd, options);
System.out.println(sol.toString());
while (sol.satisfiable()) {
System.out.println("[Solution]:");
System.out.println(sol.toString());
sol = sol.next();
}
} catch (Err e){
e.printStackTrace();
}
Thanks
In both cases no counter-examples are found.
Be aware that the command obtained via the method call loaded_model.getAllCommands().get(0) is check a for 3 in other words, you ask Alloy to look for counter examples.
If you would like to obtain an instance satisfying your constraints - i.e., not a counter-example - you should use a command containing the keyword run instead of check.
I am taking the foursquare sample java code and the same sample values from git and running in my local machine, but getting the following exception.
Here is my code:
String ll = args.length > 0 ? args[0] : "44.3,37.2";
try {
FourSquareSampleMain fourSquareSample = new FourSquareSampleMain();
fourSquareSample.searchVenues(ll);
} catch (FoursquareApiException e) {
// TODO: Error handling
e.printStackTrace();
}
}
public void searchVenues(String ll) throws FoursquareApiException {
// First we need a initialize FoursquareApi.
FoursquareApi foursquareApi = new FoursquareApi("CLIENT_ID",
"CLIENT_SECRET", null);
// After client has been initialized we can make queries.
Result<VenuesSearchResult> result = foursquareApi.venuesSearch(ll, null, null, null, null, null, null, null,
null, null, null, null, null);
if (result.getMeta().getCode() == 200) {
CompactVenue[] venueList = result.getResult().getVenues();
System.out.println("Compact Venue List size : " + venueList.length);
// if query was ok we can finally we do something with the data
for (CompactVenue venue : venueList) {
// TODO: Do something we the data
System.out.println("Venue Name : " + venue.getName());
}
System.out.println("End of IF Loop: ");
} else {
// TODO: Proper error handling
System.out.println("Error occured: ");
System.out.println(" code: " + result.getMeta().getCode());
System.out.println(" type: " + result.getMeta().getErrorType());
System.out.println(" detail: " + result.getMeta().getErrorDetail());
}
}
The size of the venueList is always "0", but when I debugged it , it throws the below exception:
"org.eclipse.debug.core.DebugException: com.sun.jdi.ClassNotLoadedException: Type has not been loaded occurred while retrieving component type of array."
But strange when I changed the latitude and longitude value,
String ll = "-33.883056 , 151.216667";// latlong surry hills sydney
I get the below exception:
fi.foyt.foursquare.api.FoursquareApiException: org.json.JSONException: JSONObject["icon"] not a string.
at fi.foyt.foursquare.api.JSONFieldParser.parseEntity(JSONFieldParser.java:143)
at fi.foyt.foursquare.api.JSONFieldParser.parseValue(JSONFieldParser.java:194)
at fi.foyt.foursquare.api.JSONFieldParser.parseEntity(JSONFieldParser.java:141)
at fi.foyt.foursquare.api.JSONFieldParser.parseEntities(JSONFieldParser.java:57)
at fi.foyt.foursquare.api.FoursquareApi.venuesSearch(FoursquareApi.java:1017)
at FourSquareSampleMain.searchVenues(FourSquareSampleMain.java:57)
at FourSquareSampleMain.main(FourSquareSampleMain.java:43)
Caused by: org.json.JSONException: JSONObject["icon"] not a string.
at org.json.JSONObject.getString(JSONObject.java:658)
at fi.foyt.foursquare.api.JSONFieldParser.parseValue(JSONFieldParser.java:202)
at fi.foyt.foursquare.api.JSONFieldParser.parseEntity(JSONFieldParser.java:141)
What am I missing here? please suggest.
I found a workaround for that. It's a patch, but it works. Details:
Info was taken from this post where same issue was identified and fixed
Take a look that diff that resolve the issue: https://github.com/wallabyfinancial/foursquare-api-java/compare/master...ganchix:master
Copy those 3 raw classes (Category, GeoCodeFeature and Icon) as they are and add them into your porject under the package fi.foyt.foursquare.api.entities and that's it
Note 1: when you replace classes (same package and class name) in yur project, the clasloader will use yours instead of the classes provided by the jar dependency, so, there is a quick fix.
I did that and it worked like a charm
Note 2: As soon as the sdk is updated, you should remove this patch and upgrade the sdk dependency
Hope it helps
I am having a LDAP Queue which process a object class.I cant find the exact location why it is giving the exception. The objclass is a concadenation string with pipe symbol. Any program coding to find the exact location in which concadination part is going to the Exception?.Please Assist.
try {
Attributes objClass = null;
try {
objClass = getObjClass(LdapInfo.PER_ID, person.perId);
} catch (NamingException e)
{
DCXError.myInstance().writeError("LdapUpdaterConnection: " + e.getMessage());
}
NamingEnumeration oc = objClass.get("objectclass").getAll();
String baseObjClass = null;
while (oc.hasMoreElements()) {
baseObjClass = (String) oc.nextElement();
if (baseObjClass.equalsIgnoreCase(LdapInfo.NON_EMPLOYEE_PERSON)
|| baseObjClass.equalsIgnoreCase("N/A")||
baseObjClass.equalsIgnoreCase(LdapInfo.EMPLOYEE_PERSON))
break;
}
} catch (SchemaViolationException e4) {
DCXError.myInstance().writeError(
"LdapUpdaterConnection:doUpdate SchemaViolationException "+ e4.getExplanation());
DCXError.myInstance().writeError("LdapUpdaterConnection:update persID = " + personId);
return (LdapUpdaterConnection.BAD_DATA);
}
You can't find the exact location only because you haven't logged the stack trace. You would also need to reformat your code so that each statement is on a separate line to make any use of that information. You should also use variable names that actually correspond to the content.
This is really terrible code.
It's also hard to see why you are doing all this in the first place. A decent query filter would do all that for you far more simply.