How to insert object in MongoDB 3.2 document? - java

I have a User
public class User {
private String name;
private String email;
public User () { }
public User(String name) {
this.name = name;
}
public User(String name, String email) {
this(name);
this.email = email;
}
// getters and setters
}
Also I have simple POJO Comment
public class Comment {
private String comment;
private Date date;
private String author;
public Comment() { }
public Comment(String comment, Date date, String author) {
this.comment = comment;
this.date = date;
this.author = author;
}
// getters and setters
}
How I want insert new user into collection with some kind of comments about him like this:
public static void main(String[] args) {
MongoClient client = new MongoClient();
MongoDatabase db = client.getDatabase("example");
MongoCollection<Document> collection = db.getCollection("object_arrays");
collection.drop();
List<Comment> reviews = new ArrayList<Comment>(){{
add(new Comment("cool guy", new Date(), "John Doe"));
add(new Comment("best joker", new Date(), "Vas Negas"));
add(new Comment("very stupid but very funny man", new Date(), "Bill Murphy"));
}};
Document user = new Document();
user.append("user", new User("0xFF", "email#email.com"))
.append("reviews", reviews)
.append("createDate", new Date());
collection.insertOne(user);
}
Unfortunately, I've got Exception:
Exception in thread "main" org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class com.mongodb.course.com.mongodb.course.model.User.
at org.bson.codecs.configuration.CodecCache.getOrThrow(CodecCache.java:46)
at org.bson.codecs.configuration.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:63)
at org.bson.codecs.configuration.ChildCodecRegistry.get(ChildCodecRegistry.java:51)
at org.bson.codecs.DocumentCodec.writeValue(DocumentCodec.java:174)
at org.bson.codecs.DocumentCodec.writeMap(DocumentCodec.java:189)
at org.bson.codecs.DocumentCodec.encode(DocumentCodec.java:131)
at org.bson.codecs.DocumentCodec.encode(DocumentCodec.java:45)
at org.bson.codecs.BsonDocumentWrapperCodec.encode(BsonDocumentWrapperCodec.java:63)
at org.bson.codecs.BsonDocumentWrapperCodec.encode(BsonDocumentWrapperCodec.java:29)
at com.mongodb.connection.InsertCommandMessage.writeTheWrites(InsertCommandMessage.java:101)
at com.mongodb.connection.InsertCommandMessage.writeTheWrites(InsertCommandMessage.java:43)
at com.mongodb.connection.BaseWriteCommandMessage.encodeMessageBodyWithMetadata(BaseWriteCommandMessage.java:129)
at com.mongodb.connection.RequestMessage.encodeWithMetadata(RequestMessage.java:160)
at com.mongodb.connection.WriteCommandProtocol.sendMessage(WriteCommandProtocol.java:212)
at com.mongodb.connection.WriteCommandProtocol.execute(WriteCommandProtocol.java:101)
at com.mongodb.connection.InsertCommandProtocol.execute(InsertCommandProtocol.java:67)
at com.mongodb.connection.InsertCommandProtocol.execute(InsertCommandProtocol.java:37)
at com.mongodb.connection.DefaultServer$DefaultServerProtocolExecutor.execute(DefaultServer.java:159)
at com.mongodb.connection.DefaultServerConnection.executeProtocol(DefaultServerConnection.java:286)
at com.mongodb.connection.DefaultServerConnection.insertCommand(DefaultServerConnection.java:115)
at com.mongodb.operation.MixedBulkWriteOperation$Run$2.executeWriteCommandProtocol(MixedBulkWriteOperation.java:455)
at com.mongodb.operation.MixedBulkWriteOperation$Run$RunExecutor.execute(MixedBulkWriteOperation.java:646)
at com.mongodb.operation.MixedBulkWriteOperation$Run.execute(MixedBulkWriteOperation.java:401)
at com.mongodb.operation.MixedBulkWriteOperation$1.call(MixedBulkWriteOperation.java:179)
at com.mongodb.operation.MixedBulkWriteOperation$1.call(MixedBulkWriteOperation.java:168)
at com.mongodb.operation.OperationHelper.withConnectionSource(OperationHelper.java:230)
at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:221)
at com.mongodb.operation.MixedBulkWriteOperation.execute(MixedBulkWriteOperation.java:168)
at com.mongodb.operation.MixedBulkWriteOperation.execute(MixedBulkWriteOperation.java:74)
at com.mongodb.Mongo.execute(Mongo.java:781)
at com.mongodb.Mongo$2.execute(Mongo.java:764)
at com.mongodb.MongoCollectionImpl.executeSingleWriteRequest(MongoCollectionImpl.java:515)
at com.mongodb.MongoCollectionImpl.insertOne(MongoCollectionImpl.java:306)
at com.mongodb.MongoCollectionImpl.insertOne(MongoCollectionImpl.java:297)
at com.mongodb.course.week3.ArrayListWithObject.main(ArrayListWithObject.java:34)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
I understand that Java driver for MongoDB can't convert my object into Document and it want some kind of converter. Also I know about Codec, CodecRegistry and CodecProvider interfaces. By the way, is there a simpler way to convert object into mongo document? Can you show me example how can I do it?
Thank you.

The problem with the code you posted is that it doesnt know by default how to serialise your pojo objects into Json to save them into the databse. You can do this with the MongoDB Java drivers, but you need to do some work to serialise the Comment ArrayList and User pojos. If you add some Jackson mapping code you can do this as follows:
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.bson.Document;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
public class Problem {
public static void main(String[] args) {
try (final MongoClient client = new MongoClient()) {
final MongoDatabase db = client.getDatabase("example");
final MongoCollection<Document> collection = db.getCollection("object_arrays");
collection.drop();
final List<Comment> reviews = new ArrayList<Comment>() {
{
add(new Comment("cool guy", new Date(), "John Doe"));
add(new Comment("best joker", new Date(), "Vas Negas"));
add(new Comment("very stupid but very funny man", new Date(), "Bill Murphy"));
}
};
final ObjectMapper mapper = new ObjectMapper();
final User user = new User("0xFF", "email#email.com");
try {
//Create a Document representation of the User object
final String userJson = mapper.writeValueAsString(user);
final Document userDoc = Document.parse(userJson);
//Convert the review ArrayList into a Mongo Document. Need to amend this if not using Java8
final List<Document> reviewDocs = reviews.stream().map(convertToJson())
.map(reviewJson -> Document.parse(reviewJson)).collect(Collectors.toList());
//Wrap it all up to it can be saved to the database
final Document wrapperDoc = new Document();
wrapperDoc.append("user", userDoc).append("reviews", reviewDocs).append("createDate", new Date());
collection.insertOne(wrapperDoc);
} catch (final JsonProcessingException e) {
e.printStackTrace();
}
}
}
private static Function<Comment, String> convertToJson() {
final ObjectMapper mapper = new ObjectMapper();
return review -> {
try {
return mapper.writeValueAsString(review);
} catch (final JsonProcessingException e) {
e.printStackTrace();
}
return "";
};
}
}
*this uses some Java8 code which you might need to change depending on which version of Java you're using
As the other answer on this question says there are frameworks out there that can combine the serialisation of Objects and interaction with MongoDB so that you dont need to hand-crank the serialisation code. For example Spring has a Mongo driver, and I have used another called Jongo, which I have found to be quite good.

If you want to work with Java objects like this, Morphia is your best bet. There is work being done now to support arbitrary Java classes like you're trying but it's not done yet.

Related

MongoDB java connection in Mongodb java driver 3.12.1

I have mongodb 4.0 install in my Ubuntu 18.04.
I am using java-mongo-driver 3.12.1
I am uploading my code you can see that i have check different type of connections but it's not working.
How to solve this problem?
Give this a try, let me know what you think...
package test;
public class Main {
public static void main(String[] args) {
com.mongodb.client.MongoClient client = connectToStandAlone();
com.mongodb.client.MongoDatabase db = client.getDatabase("javatest");
QueryData(db);
}
private static com.mongodb.client.MongoClient connectToStandAlone() {
// STANDALONE STILL REQUIRES HOSTS LIST WITH ONE ELEMENT...
java.util.ArrayList<com.mongodb.ServerAddress> hosts = new java.util.ArrayList<com.mongodb.ServerAddress>();
hosts.add(new com.mongodb.ServerAddress("127.0.0.1", 27017));
com.mongodb.MongoCredential mongoCredential = com.mongodb.MongoCredential.createScramSha1Credential("testuser", "admin", "mysecret".toCharArray());
com.mongodb.MongoClientSettings mongoClientSettings = com.mongodb.MongoClientSettings.builder()
.applyToClusterSettings(clusterSettingsBuilder -> clusterSettingsBuilder.hosts(hosts))
.credential(mongoCredential)
.writeConcern(com.mongodb.WriteConcern.W1)
.readConcern(com.mongodb.ReadConcern.MAJORITY)
.readPreference(com.mongodb.ReadPreference.nearest())
.retryWrites(true)
.build();
com.mongodb.client.MongoClient client = com.mongodb.client.MongoClients.create(mongoClientSettings);
return client;
}
private static void QueryData(com.mongodb.client.MongoDatabase db) {
// DRIVER DOES NOT HAVE collection.findOne().
com.mongodb.client.MongoCollection<org.bson.Document> collection = db.getCollection("people");
com.mongodb.client.MongoCursor<org.bson.Document> cursor = collection.find(com.mongodb.client.model.Filters.eq("testfield", true))
.sort(new org.bson.Document("review_date", -1))
.skip(5)
.limit(20)
.iterator();
while(cursor.hasNext()) {
org.bson.Document document = cursor.next();
String json = document.toJson();
System.out.println(json);
boolean testfieldValue = document.getBoolean("testfield");
String ssnValue = document.getString("ssn");
}
}
}
I found simple solution.
I have added more jar files,in total we need (bson-3.12.1.jar, mongodb-driver-core-3.12.1.jar, mongodb-driver-sync-3.12.1.jar, mongodb-driver-sync-3.12.1-javadoc.jar). These are available in enter link description here
Now my code is working fine
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
import java.util.Arrays;
public class MongoConnection {
public static void main(String[] args) {
try {
// Create Connection
MongoClient mongoClient = MongoClients.create("mongodb://localhost:27017");
System.out.println("MongoDB connected successfully");
//Accessing the database
MongoDatabase database = mongoClient.getDatabase("java");
System.out.println("MongoDB database connection ");
//Creating a collection
database.createCollection("first");
System.out.println("Collection created successfully");
//Accessing collection
MongoCollection<Document> collection = database.getCollection("first");
Document document = new Document("name", "Café Con Leche")
.append("contact", new Document("phone", "228-555-0149")
.append("email", "cafeconleche#example.com")
.append("location",Arrays.asList(-73.92502, 40.8279556)))
.append("stars", 3)
.append("categories", Arrays.asList("Bakery", "Coffee", "Pastries"));
collection.insertOne(document);
} catch (Exception e) {
e.printStackTrace();
}
}
}

Missing steps when running feature file in IntelliJ

Intellij keeps saying Undefined Step when running my feature file. However, I have copied the steps and put them into another package and added that package name in the "glue" parameter for Edit configurations. Still not working.
I've added the feature file and it doesn't show any missing step references. I am adding a screenshot. I have added all the steps in another package.
Please see below
The code for CountriesSteps is as follows
package Steps;
import Fixtures.Countries;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import org.junit.Assert;
public class CountriesSteps {
Countries countriesclass = new Countries();
#Given("^I generate a restful request for countries$")
public void iGenerateARestfulRequestForCountries() throws Throwable {
countriesclass.GetCountries();
}
#When("^I receive a successful country response (.*)$")
public void iReceiveASuccessfulCountryResponseResponse(int code) throws Throwable {
Assert.assertEquals(code, countriesclass.getsCode());
}
#When("^I receive a successful country response$")
public void iReceiveASuccessfulCountryResponse() throws Throwable {
Assert.assertEquals(200, countriesclass.getsCode());
}
#Then("^the api country response returns (.*)$")
public void theApiCountryResponseReturnsCountries(int countries) throws Throwable {
Assert.assertEquals(countries, countriesclass.getCount());
}
#Then("^the api country response returns (.*),(.*),(.*),(.*),(.*)$")
public void theApiCountryResponseReturnsCountriesNameCapitalPopulation(int countries, int index, String name, String capital, int population) throws Throwable {
Assert.assertEquals(countries, countriesclass.getCount());
}
#Then("^the api country response for Index (.*) returns (.*),(.*),(.*)$")
public void theApiCountryResponseForIndexIndexReturnsNameCapitalPopulation(int index, String name, String capital, int population) throws Throwable {
//Validate a few values from response
Assert.assertEquals(name, countriesclass.getcList().get(index).name);
Assert.assertEquals(capital, countriesclass.getcList().get(index).capital);
Assert.assertEquals(population, countriesclass.getcList().get(index).population);
}
}
And code for Countries is
package Fixtures;
import Models.CountriesData;
import com.jayway.restassured.response.Response;
import gherkin.deps.com.google.gson.Gson;
import gherkin.deps.com.google.gson.reflect.TypeToken;
import org.json.JSONArray;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import static com.jayway.restassured.RestAssured.get;
public class Countries {
private static String url;
private static int count;
private static int sCode;
private static List<CountriesData> cList;
public void GetCountries() throws Exception
{
try {
url = "http://restcountries.eu/rest/v1/all";
// make get request to fetch json response from restcountries
Response resp = get(url);
//Fetching response in JSON as a string then convert to JSON Array
JSONArray jsonResponse = new JSONArray(resp.asString());
count = jsonResponse.length(); // how many items in the array
sCode = resp.statusCode(); // status code of 200
//create new arraylist to match CountriesData
List<CountriesData> cDataList = new ArrayList<CountriesData>();
Gson gson = new Gson();
Type listType = new TypeToken<List<CountriesData>>() {}.getType();
cDataList = gson.fromJson(jsonResponse.toString(), listType);
cList = cDataList;
}
catch (Exception e)
{
System.out.println("There is an error connecting to the API: " + e);
e.getStackTrace();
}
}
//getters to return ('get) the values
public int getsCode() {
return sCode;
}
public int getCount() {
return count;
}
public List<CountriesData> getcList() {
return cList;
}
}
When creating a new step definition file, IntelliJ by default proposes file path of \IdeaProjects\RestAPI\src\main\resources\stepmethods.
Your step definition folder is \IdeaProjects\RestAPI\src\test\java\Steps. Make sure cucumber isn't looking in the wrong place.

jackson-dataformat-csv: Mapping number value without POJO

I'm trying to parse a CSV file using jackson-dataformat-csv and I want to map the numeric column to the Number java type.
CsvSchema schema = CsvSchema.builder().setUseHeader(true)
.addColumn("firstName", CsvSchema.ColumnType.STRING)
.addColumn("lastName", CsvSchema.ColumnType.STRING)
.addColumn("age", CsvSchema.ColumnType.NUMBER)
.build();
CsvMapper csvMapper = new CsvMapper();
MappingIterator<Map<String, Object>> mappingIterator = csvMapper
.readerFor(Map.class)
.with(schema)
.readValues(is);
while (mappingIterator.hasNext()) {
Map<String, Object> entryMap = mappingIterator.next();
Number age = (Number) entryMap.get("age");
}
I'm expecting entryMap.get("age") should be a Number, but I get String instead.
My CSV file:
firstName,lastName,age
John,Doe,21
Error,Name,-10
I know that CsvSchema works fine with POJOs, but I need to process arbitrary CSV schemas, so I can't create a new java class for every case.
Any way to parse CSV into a typed Map or Array?
Right now it is not possible to configure Map deserialisation using CsvSchema. Process uses com.fasterxml.jackson.databind.deser.std.MapDeserializer which right now does not check schema. We could write custom Map deserialiser. There is a question on GitHub: CsvMapper does not respect CsvSchema.ColumnType when using #JsonAnySetter where cowtowncoder answered:
At this point schema type is not used much for anything, but I agree
it should.
EDIT
I decided to take a look closer what we can do with that fact that com.fasterxml.jackson.databind.deser.std.MapDeserializer is used behind the scene. Implementing custom Map deserialiser which will take care about types would be tricky to implement and register but we can use knowledge about ValueInstantiator. Let's define new Map type which knows what to do with ColumnType info:
class CsvMap extends HashMap<String, Object> {
private final CsvSchema schema;
private final NumberFormat numberFormat = NumberFormat.getInstance();
public CsvMap(CsvSchema schema) {
this.schema = schema;
}
#Override
public Object put(String key, Object value) {
value = convertIfNeeded(key, value);
return super.put(key, value);
}
private Object convertIfNeeded(String key, Object value) {
CsvSchema.Column column = schema.column(key);
if (column.getType() == CsvSchema.ColumnType.NUMBER) {
try {
return numberFormat.parse(value.toString());
} catch (ParseException e) {
// leave it as it is
}
}
return value;
}
}
For new type without no-arg constructor we should create new ValueInstantiator:
class CsvMapInstantiator extends ValueInstantiator.Base {
private final CsvSchema schema;
public CsvMapInstantiator(CsvSchema schema) {
super(CsvMap.class);
this.schema = schema;
}
#Override
public Object createUsingDefault(DeserializationContext ctxt) {
return new CsvMap(schema);
}
#Override
public boolean canCreateUsingDefault() {
return true;
}
}
Example usage:
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.MappingIterator;
import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.databind.deser.ValueInstantiator;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.dataformat.csv.CsvMapper;
import com.fasterxml.jackson.dataformat.csv.CsvSchema;
import java.io.File;
import java.io.IOException;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.HashMap;
public class CsvApp {
public static void main(String[] args) throws IOException {
File csvFile = new File("./resource/test.csv").getAbsoluteFile();
CsvSchema schema = CsvSchema.builder()
.addColumn("firstName", CsvSchema.ColumnType.STRING)
.addColumn("lastName", CsvSchema.ColumnType.STRING)
.addColumn("age", CsvSchema.ColumnType.NUMBER)
.build().withHeader();
// Create schema aware map module
SimpleModule csvMapModule = new SimpleModule();
csvMapModule.addValueInstantiator(CsvMap.class, new CsvMapInstantiator(schema));
// register map
CsvMapper csvMapper = new CsvMapper();
csvMapper.registerModule(csvMapModule);
// get reader for CsvMap + schema
ObjectReader objectReaderWithSchema = csvMapper
.readerWithSchemaFor(CsvMap.class)
.with(schema);
MappingIterator<CsvMap> mappingIterator = objectReaderWithSchema.readValues(csvFile);
while (mappingIterator.hasNext()) {
CsvMap entryMap = mappingIterator.next();
Number age = (Number) entryMap.get("age");
System.out.println(age + " (" + age.getClass() + ")");
}
}
}
Above code for below CSV payload:
firstName,lastName,age
John,Doe,21
Error,Name,-10.1
prints:
21 (class java.lang.Long)
-10.1 (class java.lang.Double)
It looks like a hack but I wanted to show this possibility.
You can use univocity-parsers for this sort of thing. It's faster and way more flexible:
CsvParserSettingssettings = new CsvParserSettings(); //configure the parser if needed
CsvParser parser = new CsvParser(settings);
for (Record record : parser.iterateRecords(is)) {
Short age = record.getShort("age");
}
To get a typed map, tell the parser what is the type of the columns you are working with:
parser.getRecordMetadata().setTypeOfColumns(Short.class, "age" /*, and other column names*/);
//to get 0 instead of nulls when the field is empty in the file:
parser.getRecordMetadata().setDefaultValueOfColumns("0", "age", /*, and other column names*/);
// then parse
for (Record record : parser.iterateRecords(is)) {
Map<String,Object> map = record.toFieldMap();
}
Hope this helps
Disclaimer: I'm the author of this library. It's open source and free (Apache 2.0 license)

ContentModel cannot be resolved to a variable

I'm having this error :
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
ContentModel cannot be resolved to a variable
at test2CMIS.Test.main(Test.java:39)"
And I dont understand from where it comes, here is my code :
public class Test {
public static void main(String[] args){
Test atest = new Test();
Session session = atest.iniSession();
AuthenticationService authenticationService=null;
PersonService personService = null;
if (authenticationService.authenticationExists("test") == false)
{
authenticationService.createAuthentication("test", "changeMe".toCharArray());
PropertyMap ppOne = new PropertyMap(4);
ppOne.put(ContentModel.PROP_USERNAME, "test");
ppOne.put(ContentModel.PROP_FIRSTNAME, "firstName");
ppOne.put(ContentModel.PROP_LASTNAME, "lastName");
ppOne.put(ContentModel.PROP_EMAIL, "test"+"#example.com");
personService.createPerson(ppOne);
}
}
I did import the : import org.alfresco.model.ContentModel; and a lot of others librarys for my code.
Thx for help.
The code I'm using and I left some things that I tried too in comments so you can see what things I have done:
import java.io.File;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import org.alfresco.service.cmr.security.*;
import org.alfresco.error.AlfrescoRuntimeException;
import org.alfresco.model.ContentModel;
import java.util.Iterator;
import org.alfresco.repo.jscript.People;
import org.alfresco.repo.security.authentication.AuthenticationException;
import org.alfresco.service.cmr.security.AuthenticationService;
import org.alfresco.service.cmr.security.PersonService;
import org.alfresco.service.namespace.QName;
import org.alfresco.util.PropertyMap;
import org.apache.chemistry.opencmis.client.api.CmisObject;
import org.apache.chemistry.opencmis.client.api.Document;
import org.apache.chemistry.opencmis.client.api.Folder;
import org.apache.chemistry.opencmis.client.api.Session;
import org.apache.chemistry.opencmis.commons.PropertyIds;
import org.apache.chemistry.opencmis.commons.SessionParameter;
import org.apache.chemistry.opencmis.commons.enums.BindingType;
import org.apache.chemistry.opencmis.commons.enums.VersioningState;
import org.apache.chemistry.opencmis.commons.exceptions.CmisContentAlreadyExistsException;
import org.apache.chemistry.opencmis.commons.exceptions.CmisUnauthorizedException;
import org.apache.chemistry.opencmis.client.util.FileUtils;
import org.apache.chemistry.opencmis.client.runtime.SessionFactoryImpl;
public class Test {
public static void main(String[] args){
Test atest = new Test();
Session session = atest.iniSession();
AuthenticationService authenticationService=new AuthenticationServiceImpl();
PersonService personService = new PersonServiceImpl();
HashMap<QName, Serializable> properties = new HashMap<QName, Serializable>();
properties.put(ContentModel.PROP_USERNAME, "test");
properties.put(ContentModel.PROP_FIRSTNAME, "test");
properties.put(ContentModel.PROP_LASTNAME, "qsdqsd");
properties.put(ContentModel.PROP_EMAIL, "wshAlors#gmail.com");
properties.put(ContentModel.PROP_ENABLED, Boolean.valueOf(true));
properties.put(ContentModel.PROP_ACCOUNT_LOCKED, Boolean.valueOf(false));
personService.createPerson(properties);
authenticationService.createAuthentication("test", "changeme".toCharArray());
authenticationService.setAuthenticationEnabled("test", true);
authenticationService.getAuthenticationEnabled("Admin");
//String testAuthen = authenticationService.getCurrentTicket();
//System.out.println(testAuthen);
//QName username = QName.createQName("test");
//Map<QName,Serializable> propertiesUser = new HashMap<QName,Serializable>();
//propertiesUser.put(ContentModel.PROP_USERNAME,username);
//propertiesUser.put(ContentModel.PROP_FIRSTNAME,"test");
//propertiesUser.put(ContentModel.PROP_LASTNAME,"test");
//propertiesUser.put(ContentModel.PROP_EMAIL, "test#example.com");
//propertiesUser.put(ContentModel.PROP_PASSWORD,"0000");
//personService.createPerson(propertiesUser);
//if (authenticationService.authenticationExists("test") == false)
//{
// authenticationService.createAuthentication("test", "changeMe".toCharArray());
// PropertyMap ppOne = new PropertyMap(4);
// ppOne.put(ContentModel.PROP_USERNAME, "test");
// ppOne.put(ContentModel.PROP_FIRSTNAME, "test");
// ppOne.put(ContentModel.PROP_LASTNAME, "test");
// ppOne.put(ContentModel.PROP_EMAIL, "test#example.com");
//ppOne.put(ContentModel.PROP_JOBTITLE, "jobTitle");
// personService.createPerson(ppOne);
//}
}
public Session iniSession() {
Session session;
SessionFactoryImpl sf = SessionFactoryImpl.newInstance();
Map<String, String> parameters = new HashMap<String, String>();
Scanner reader = new Scanner(System.in);
System.out.println("Enter your logging : ");
String log = reader.nextLine();
System.out.println("Enter your password : ");
String pass = reader.nextLine();
parameters.put(SessionParameter.USER, log);
parameters.put(SessionParameter.PASSWORD, pass);
parameters.put(SessionParameter.BROWSER_URL, "http://127.0.0.1:8080/alfresco/api/-default-/public/cmis/versions/1.1/browser");
parameters.put(SessionParameter.BINDING_TYPE, BindingType.BROWSER.value());
parameters.put(SessionParameter.REPOSITORY_ID, "-default-");
try{
session = sf.createSession(parameters);
}catch(CmisUnauthorizedException cue){
session = null;
System.out.println("Wrong logging OR password !");
}
return session;
}
You are writing a runnable class which is not running the same process as Alfresco. In that sense, your class is running "remotely".
Because your class is running remotely to Alfresco, you are correct in using CMIS. But CMIS will only allow you to perform Create, Read, Update, and Delete (CRUD) functions against documents and folders in Alfresco. CMIS does not know how to create users or groups.
Your class will not be able to instantiate the AuthenticationService or PersonService. Those are part of the Alfresco Foundation API which only works when you are running in the same process as Alfresco, such as in an Action, a Behavior, or a Java-backed web script. In those cases, you will use Spring Dependency Injection to inject those services into your Java class. You would then put your class in a JAR that gets deployed into the Alfresco web application and loaded by the same classloader as Alfresco's.
If you want to create users remotely you should consider using the Alfresco REST API. Your runnable class can then use an HTTP client to invoke REST calls to create people and groups.
Thanks you for everything ! Thanks to you and researches I found out how to do it ! For the others who wonder how to do I'll post how I did and what site I used to understand it !
So you just need to manipulate JSON with Java because your alfresco people page (127.0.0.1:8080/alfresco/service/api/people) returns a JSON object, and you'll be able to create, delete, search... users! Thx again !
Sites :
https://api-explorer.alfresco.com/api-explorer/#/people
http://crunchify.com/json-manipulation-in-java-examples/
The code :
This is for creating an user :
public User createUser(String firstN, String lastN, String email, String pass, String authTicket) throws Exception{
try{
String url = "http://127.0.0.1:8080/alfresco/service/api/people?alf_ticket="+authTicket;
HttpClient httpclient = new HttpClient();
PostMethod mPost = new PostMethod(url);
//JSONObject obj = new JSONObject();
//JSONArray people = obj.getJSONArray("people");
JSONObject newUser = new JSONObject();
newUser.put("userName", firstN.toLowerCase().charAt(0)+lastN.toLowerCase());
newUser.put("enabled",true);
newUser.put("firstName",firstN);
newUser.put("lastName", lastN);
newUser.put("email", email);
newUser.put("quota",-1);
newUser.put("emailFreedDisable",false);
newUser.put("isDeleted",false);
newUser.put("isAdminAuthority",false);
newUser.put("password", pass);
//people.put(newUser);
//Response response = PostRequest(newUser.toString()));
StringRequestEntity requestEntity = new StringRequestEntity(
newUser.toString(),
"application/json",
"UTF-8");
mPost.setRequestEntity(requestEntity);
int statusCode2 = httpclient.executeMethod(mPost);
mPost.releaseConnection();
}catch(Exception e){
System.err.println("[ERROR] "+e);
}
return new User(firstN, lastN);
}
And if you want to get all the users you have on alfresco :
public ArrayList<User> getAllUsers(String authTicket)
{
ArrayList<User> allUsers = new ArrayList<>();
String lastName, firstName;
try{
String url = "http://127.0.0.1:8080/alfresco/service/api/people?alf_ticket="+authTicket;
HttpClient httpclient = new HttpClient();
GetMethod mPost = new GetMethod(url);
int statusCode1 = httpclient.executeMethod(mPost);
System.out.println("statusLine >>> "+statusCode1+"....."
+"\n status line \n"
+mPost.getStatusLine()+"\nbody \n"+mPost.getResponseBodyAsString());
JSONObject obj = new JSONObject(mPost.getResponseBodyAsString());
JSONArray people = obj.getJSONArray("people");
int n = people.length();
for(int i =0 ; i < n ; i++)
{
JSONObject peoples = people.getJSONObject(i);
User u = new User(peoples.getString("firstName"), peoples.getString("lastName"));
if (!allUsers.contains(u)){
allUsers.add(u);
}
}
}catch(Exception e){
System.err.println("[ERROR] "+e);
}
return(allUsers);
}

How to deserialize Json result into POJO using Restlet

I'm trying to deserialize Json result from a web service into POJO.
ClientResource clientResource = new ClientResource("http://itunes.apple.com/search?term=marc+jordan&media=music&entity=album");
AlbumInfoResource resource = clientResource.wrap(AlbumInfoResource.class);
AlbumInfo albumInfo = resource.retrieve();
The resulting albumInfo is null, no exception is thrown.
I'm new to Restlet, what I'm doing wrong?
Interface:
public interface AlbumInfoResource {
#Get
public AlbumInfo retrieve();
}
The Json result from the web service looks like this:
{
"resultCount": 49,
"results": [
{
"wrapperType": "collection",
"collectionType": "Album",
"artistId": 771969,
"collectionId": 205639995,
"amgArtistId": 4640,
"artistName": "Marc Jordan",
"collectionName": "This Is How Men Cry",
"collectionCensoredName": "This Is How Men Cry",
"artistViewUrl": "http://itunes.apple.com/us/artist/marc-jordan/id771969?uo=4",
"collectionViewUrl": "http://itunes.apple.com/us/album/this-is-how-men-cry/id205639995?uo=4",
"artworkUrl60": "http://a5.mzstatic.com/us/r30/Music/cd/3f/13/mzi.rxpvpvdd.60x60-50.jpg",
"artworkUrl100": "http://a1.mzstatic.com/us/r30/Music/cd/3f/13/mzi.rxpvpvdd.100x100-75.jpg",
"collectionPrice": 9.9,
"collectionExplicitness": "notExplicit",
"trackCount": 10,
"copyright": "1999 Cafe Productions Inc.",
"country": "USA",
"currency": "USD",
"releaseDate": "2006-11-07T08:00:00Z",
"primaryGenreName": "Jazz"
},
...
...
}
]
}
The AlbumInfo class:
public class AlbumInfo implements Serializable {
private static final long serialVersionUID = 1L;
private int _resultCount;
private ArrayList<Album> _albums;
public AlbumInfo() {
_albums = new ArrayList<Album>();
}
public AlbumInfo(int resultCount, ArrayList<Album> albums) {
_resultCount = resultCount;
_albums = albums;
}
public int getResultCount() {
return _resultCount;
}
public void setResultCount(int resultCount) {
_resultCount = resultCount;
}
public ArrayList<Album> getAlbums() {
return _albums;
}
public void setAlbums(ArrayList<Album> _albums) {
this._albums = _albums;
}
}
The Album class would be to big to post here, but I have mapped the elements as reasonable as I could.
If you haven't already, you need to add Restlet's JacksonConverter to the list of registered converters:
List<ConverterHelper> converters = Engine.getInstance().getRegisteredConverters();
converters.add(new JacksonConverter());
and, of course, add org.restlet.ext.jackson.jar to your build path.
Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB 2 (JSR-222) expert group.
The following is how it could be done with MOXy by leveraging JAXB annotations:
AlbumInfo
package forum9966753;
import java.io.Serializable;
import java.util.ArrayList;
import javax.xml.bind.annotation.*;
#XmlType(propOrder={"resultCount", "albums"})
public class AlbumInfo implements Serializable {
private static final long serialVersionUID = 1L;
private int _resultCount;
private ArrayList<Album> _albums;
public AlbumInfo() {
_albums = new ArrayList<Album>();
}
public AlbumInfo(int resultCount, ArrayList<Album> albums) {
_resultCount = resultCount;
_albums = albums;
}
public int getResultCount() {
return _resultCount;
}
public void setResultCount(int resultCount) {
_resultCount = resultCount;
}
#XmlElement(name="results")
public ArrayList<Album> getAlbums() {
return _albums;
}
public void setAlbums(ArrayList<Album> _albums) {
this._albums = _albums;
}
}
Album
Below is a scaled down version of your Album class:
package forum9966753;
public class Album {
private String wrapperType;
public String getWrapperType() {
return wrapperType;
}
public void setWrapperType(String wrapperType) {
this.wrapperType = wrapperType;
}
}
jaxb.properties
To specify MOxy as your JAXB provider you need to add a file called jaxb.properties in the same package as your domain classes with the following entry:
javax.xml.bind.context.factory = org.eclipse.persistence.jaxb.JAXBContextFactory
Demo
package forum9966753;
import java.io.InputStream;
import java.net.*;
import java.util.List;
import javax.xml.bind.*;
import javax.xml.transform.stream.StreamSource;
import org.example.Customer;
public class JavaSEClient {
private static final String MEDIA_TYPE = "application/json";
public static void main(String[] args) throws Exception {
String uri = "http://itunes.apple.com/search?term=marc+jordan&media=music&entity=album";
URL url = new URL(uri);
HttpURLConnection connection =
(HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Accept", MEDIA_TYPE);
JAXBContext jc = JAXBContext.newInstance(AlbumInfo.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
unmarshaller.setProperty("eclipselink.media-type", MEDIA_TYPE);
unmarshaller.setProperty("eclipselink.json.include-root", false);
InputStream xml = connection.getInputStream();
AlbumInfo albumInfo = unmarshaller.unmarshal(new StreamSource(xml), AlbumInfo.class).getValue();
connection.disconnect();
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty("eclipselink.media-type", MEDIA_TYPE);
marshaller.setProperty("eclipselink.json.include-root", false);
marshaller.marshal(albumInfo, System.out);
}
}
Output
Below is the output from running the demo code. As the sample domain model only contains a couple properties the output is much smaller than the output. The JAXB mappings can easily be applied to map the rest of the document.
{
"resultCount" : 49,
"results" : [ {
"wrapperType" : "collection"
} ]
}
For More Information
MOXy as Your JAX-RS JSON Provider - Client Side
Try using JAXB annotations or use Jersey for JSON mappings. This manual could be useful for you: link
Recently I had to develop an Android app with Restlet framework and I spent a lot of time to understand how to deserialize a JSONObject.
Here I'm going t explain my method.
I also published a complete Android App on GitHub here:
https://github.com/alchimya/android-restlet
Restlet 2.3.2 includes Gson lib.
With Gson is very simple to map and deserialize a resource.
1) Map your entity base with a class as following:
import com.google.gson.annotations.SerializedName;
import java.io.Serializable;
public class Album implements Serializable {
#SerializedName("wrapperType")
private String wrapperType;
#SerializedName("collectionType")
private String collectionType;
#SerializedName("artistId")
private String artistId;
public String getWrapperType() {
return wrapperType;
}
public void setWrapperType(String wrapperType) {
this.wrapperType = wrapperType;
}
public String getCollectionType() {
return collectionType;
}
public void setCollectionType(String collectionType) {
this.collectionType = collectionType;
}
public String getArtistId() {
return artistId;
}
public void setArtistId(String artistId) {
this.artistId = artistId;
}
......
......
......
}
Note: in the previous class each property has an annotation (#SerializedName) to map the corresponding JSON field. For further informations see this tutorial:
http://www.javacodegeeks.com/2011/01/android-json-parsing-gson-tutorial.html
2) Create a class with a List attribute:
public class Albums {
public List<Album> results;
}
3) Consume your resource with Restlet
String uri="http://itunes.apple.com/search?term=marc+jordan&media=music&entity=album";
ClientResource resource = new ClientResource(url);
Representation rep = resource.get();
JsonRepresentation represent = new JsonRepresentation(rep);
JSONObject jsonobject = represent.getJsonObject();
String jsonText = jsonobject.toString();
Gson gson = new Gson();
Albums response = gson.fromJson(jsonText, Albums.class);
Into response.results there will be all deserialized items.

Categories