Search in sources :

Example 1 with DBCollection

use of com.mongodb.DBCollection in project jetty.project by eclipse.

the class MongoTest method main.

public static void main(String... args) throws Exception {
    Mongo m = new Mongo("127.0.0.1", 27017);
    DB db = m.getDB("mydb");
    Set<String> colls = db.getCollectionNames();
    System.err.println("Colls=" + colls);
    DBCollection coll = db.getCollection("testCollection");
    BasicDBObject key = new BasicDBObject("id", "1234");
    BasicDBObject sets = new BasicDBObject("name", "value");
    BasicDBObject upsert = new BasicDBObject("$set", sets);
    WriteResult result = coll.update(key, upsert, true, false);
    System.err.println(result.getLastError());
    while (coll.count() > 0) {
        DBObject docZ = coll.findOne();
        System.err.println("removing    " + docZ);
        if (docZ != null)
            coll.remove(docZ);
    }
}
Also used : DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) WriteResult(com.mongodb.WriteResult) Mongo(com.mongodb.Mongo) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) DB(com.mongodb.DB)

Example 2 with DBCollection

use of com.mongodb.DBCollection in project jetty.project by eclipse.

the class SessionExpiryTest method testChangeNewSessionTimeout.

@Test
public void testChangeNewSessionTimeout() throws Exception {
    String contextPath = "";
    String servletMapping = "/server";
    int inactivePeriod = 10;
    int scavengePeriod = 1;
    DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
    cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
    MongoSessionDataStoreFactory storeFactory = MongoTestHelper.newSessionDataStoreFactory();
    storeFactory.setGracePeriodSec(scavengePeriod);
    TestServer server1 = new TestServer(0, inactivePeriod, scavengePeriod, cacheFactory, storeFactory);
    ImmediateChangeTimeoutServlet servlet = new ImmediateChangeTimeoutServlet();
    ServletHolder holder = new ServletHolder(servlet);
    ServletContextHandler context = server1.addContext(contextPath);
    context.addServlet(holder, servletMapping);
    TestHttpSessionListener listener = new TestHttpSessionListener();
    context.getSessionHandler().addEventListener(listener);
    server1.start();
    int port1 = server1.getPort();
    try {
        HttpClient client = new HttpClient();
        client.start();
        String url = "http://localhost:" + port1 + contextPath + servletMapping;
        //change from the sessionmanager configured default
        inactivePeriod = 5;
        //make a request to set up a session on the server and change its inactive setting straight away
        ContentResponse response1 = client.GET(url + "?action=init&val=" + inactivePeriod);
        assertEquals(HttpServletResponse.SC_OK, response1.getStatus());
        String sessionCookie = response1.getHeaders().get("Set-Cookie");
        assertTrue(sessionCookie != null);
        // Mangle the cookie, replacing Path with $Path, etc.
        sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");
        String sessionId = TestServer.extractSessionId(sessionCookie);
        DBCollection sessions = MongoTestHelper.getCollection();
        verifySessionCreated(listener, sessionId);
        //verify that the session timeout is the new value and not the default
        verifySessionTimeout(sessions, sessionId, inactivePeriod);
    } finally {
        server1.stop();
    }
}
Also used : DBCollection(com.mongodb.DBCollection) DefaultSessionCacheFactory(org.eclipse.jetty.server.session.DefaultSessionCacheFactory) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) HttpClient(org.eclipse.jetty.client.HttpClient) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) TestServer(org.eclipse.jetty.server.session.TestServer) Test(org.junit.Test) AbstractSessionExpiryTest(org.eclipse.jetty.server.session.AbstractSessionExpiryTest)

Example 3 with DBCollection

use of com.mongodb.DBCollection in project jetty.project by eclipse.

the class SessionExpiryTest method testBigSessionExpiry.

@Test
public void testBigSessionExpiry() throws Exception {
    String contextPath = "";
    String servletMapping = "/server";
    //integer overflow
    int inactivePeriod = Integer.MAX_VALUE * 60;
    int scavengePeriod = 10;
    DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
    cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
    MongoSessionDataStoreFactory storeFactory = MongoTestHelper.newSessionDataStoreFactory();
    storeFactory.setGracePeriodSec(scavengePeriod);
    TestServer server1 = new TestServer(0, inactivePeriod, scavengePeriod, cacheFactory, storeFactory);
    ChangeTimeoutServlet servlet = new ChangeTimeoutServlet();
    ServletHolder holder = new ServletHolder(servlet);
    ServletContextHandler context = server1.addContext(contextPath);
    context.addServlet(holder, servletMapping);
    TestHttpSessionListener listener = new TestHttpSessionListener();
    context.getSessionHandler().addEventListener(listener);
    server1.start();
    int port1 = server1.getPort();
    try {
        HttpClient client = new HttpClient();
        client.start();
        String url = "http://localhost:" + port1 + contextPath + servletMapping;
        //make a request to set up a session on the server
        ContentResponse response1 = client.GET(url + "?action=init");
        assertEquals(HttpServletResponse.SC_OK, response1.getStatus());
        String sessionCookie = response1.getHeaders().get("Set-Cookie");
        assertTrue(sessionCookie != null);
        // Mangle the cookie, replacing Path with $Path, etc.
        sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");
        String sessionId = TestServer.extractSessionId(sessionCookie);
        DBCollection sessions = MongoTestHelper.getCollection();
        verifySessionCreated(listener, sessionId);
        //verify that the session timeout is set in mongo
        //SessionManager sets -1 if maxInactive < 0
        verifySessionTimeout(sessions, sessionId, -1);
        //get the session expiry time from mongo
        long expiry = getSessionExpiry(sessions, sessionId);
        assertEquals(0, expiry);
    } finally {
        server1.stop();
    }
}
Also used : DefaultSessionCacheFactory(org.eclipse.jetty.server.session.DefaultSessionCacheFactory) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) TestServer(org.eclipse.jetty.server.session.TestServer) DBCollection(com.mongodb.DBCollection) HttpClient(org.eclipse.jetty.client.HttpClient) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Test(org.junit.Test) AbstractSessionExpiryTest(org.eclipse.jetty.server.session.AbstractSessionExpiryTest)

Example 4 with DBCollection

use of com.mongodb.DBCollection in project jetty.project by eclipse.

the class SessionExpiryTest method changeSessionTimeout.

@Test
public void changeSessionTimeout() throws Exception {
    String contextPath = "";
    String servletMapping = "/server";
    int inactivePeriod = 10;
    int scavengePeriod = 1;
    DefaultSessionCacheFactory cacheFactory = new DefaultSessionCacheFactory();
    cacheFactory.setEvictionPolicy(SessionCache.NEVER_EVICT);
    MongoSessionDataStoreFactory storeFactory = MongoTestHelper.newSessionDataStoreFactory();
    storeFactory.setGracePeriodSec(scavengePeriod);
    TestServer server1 = new TestServer(0, inactivePeriod, scavengePeriod, cacheFactory, storeFactory);
    ChangeTimeoutServlet servlet = new ChangeTimeoutServlet();
    ServletHolder holder = new ServletHolder(servlet);
    ServletContextHandler context = server1.addContext(contextPath);
    context.addServlet(holder, servletMapping);
    TestHttpSessionListener listener = new TestHttpSessionListener();
    context.getSessionHandler().addEventListener(listener);
    server1.start();
    int port1 = server1.getPort();
    try {
        HttpClient client = new HttpClient();
        client.start();
        String url = "http://localhost:" + port1 + contextPath + servletMapping;
        //make a request to set up a session on the server
        ContentResponse response1 = client.GET(url + "?action=init");
        assertEquals(HttpServletResponse.SC_OK, response1.getStatus());
        String sessionCookie = response1.getHeaders().get("Set-Cookie");
        assertTrue(sessionCookie != null);
        // Mangle the cookie, replacing Path with $Path, etc.
        sessionCookie = sessionCookie.replaceFirst("(\\W)(P|p)ath=", "$1\\$Path=");
        String sessionId = TestServer.extractSessionId(sessionCookie);
        DBCollection sessions = MongoTestHelper.getCollection();
        verifySessionCreated(listener, sessionId);
        //verify that the session timeout is set in mongo
        verifySessionTimeout(sessions, sessionId, inactivePeriod);
        //get the session expiry time from mongo
        long expiry = getSessionExpiry(sessions, sessionId);
        //make another request to change the session timeout to a smaller value
        inactivePeriod = 5;
        Request request = client.newRequest(url + "?action=change&val=" + inactivePeriod);
        request.getHeaders().add("Cookie", sessionCookie);
        ContentResponse response2 = request.send();
        assertEquals(HttpServletResponse.SC_OK, response2.getStatus());
        //check the timeout in mongo
        verifySessionTimeout(sessions, sessionId, inactivePeriod);
        //check the session expiry time has decreased from previous value
        assertTrue(getSessionExpiry(sessions, sessionId) < expiry);
        expiry = getSessionExpiry(sessions, sessionId);
        //increase the session timeout
        inactivePeriod = 20;
        request = client.newRequest(url + "?action=change&val=" + inactivePeriod);
        request.getHeaders().add("Cookie", sessionCookie);
        response2 = request.send();
        assertEquals(HttpServletResponse.SC_OK, response2.getStatus());
        //verify that the session timeout is set in mongo
        verifySessionTimeout(sessions, sessionId, inactivePeriod);
        long latestExpiry = getSessionExpiry(sessions, sessionId);
        assertTrue(latestExpiry > expiry);
        assertTrue(getSessionAccessed(sessions, sessionId) + (1000L * inactivePeriod) <= getSessionExpiry(sessions, sessionId));
        //old inactive expired in 5, new inactive expired in 20
        assertTrue(latestExpiry >= 15);
    } finally {
        server1.stop();
    }
}
Also used : DefaultSessionCacheFactory(org.eclipse.jetty.server.session.DefaultSessionCacheFactory) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) Request(org.eclipse.jetty.client.api.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) TestServer(org.eclipse.jetty.server.session.TestServer) DBCollection(com.mongodb.DBCollection) HttpClient(org.eclipse.jetty.client.HttpClient) ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Test(org.junit.Test) AbstractSessionExpiryTest(org.eclipse.jetty.server.session.AbstractSessionExpiryTest)

Example 5 with DBCollection

use of com.mongodb.DBCollection in project morphia by mongodb.

the class DatastoreImpl method mapReduce.

@Override
@Deprecated
public <T> MapreduceResults<T> mapReduce(final MapreduceType type, final Query query, final Class<T> outputType, final MapReduceCommand baseCommand) {
    Assert.parametersNotNull("map", baseCommand.getMap());
    Assert.parameterNotEmpty("map", baseCommand.getMap());
    Assert.parametersNotNull("reduce", baseCommand.getReduce());
    Assert.parameterNotEmpty("reduce", baseCommand.getReduce());
    if (query.getOffset() != 0 || query.getFieldsObject() != null) {
        throw new QueryException("mapReduce does not allow the offset/retrievedFields query options.");
    }
    final OutputType outType = type.toOutputType();
    final DBCollection dbColl = query.getCollection();
    final MapReduceCommand cmd = new MapReduceCommand(dbColl, baseCommand.getMap(), baseCommand.getReduce(), baseCommand.getOutputTarget(), outType, query.getQueryObject());
    cmd.setFinalize(baseCommand.getFinalize());
    cmd.setScope(baseCommand.getScope());
    if (query.getLimit() > 0) {
        cmd.setLimit(query.getLimit());
    }
    if (query.getSortObject() != null) {
        cmd.setSort(query.getSortObject());
    }
    if (LOG.isTraceEnabled()) {
        LOG.info("Executing " + cmd.toString());
    }
    final EntityCache cache = createCache();
    MapreduceResults<T> results = new MapreduceResults<T>(dbColl.mapReduce(baseCommand));
    results.setType(type);
    if (MapreduceType.INLINE.equals(type)) {
        results.setInlineRequiredOptions(this, outputType, getMapper(), cache);
    } else {
        results.setQuery(newQuery(outputType, getDB().getCollection(results.getOutputCollectionName())));
    }
    return results;
}
Also used : DBCollection(com.mongodb.DBCollection) QueryException(org.mongodb.morphia.query.QueryException) EntityCache(org.mongodb.morphia.mapping.cache.EntityCache) MapReduceCommand(com.mongodb.MapReduceCommand) OutputType(com.mongodb.MapReduceCommand.OutputType)

Aggregations

DBCollection (com.mongodb.DBCollection)221 BasicDBObject (com.mongodb.BasicDBObject)127 DBObject (com.mongodb.DBObject)103 Test (org.junit.Test)76 DBCursor (com.mongodb.DBCursor)43 DB (com.mongodb.DB)40 MongoException (com.mongodb.MongoException)27 JSONObject (org.json.JSONObject)27 BasicDBObjectBuilder (com.mongodb.BasicDBObjectBuilder)18 ArrayList (java.util.ArrayList)18 BasicDBList (com.mongodb.BasicDBList)16 MongoClient (com.mongodb.MongoClient)16 MongoClientURI (com.mongodb.MongoClientURI)15 WriteResult (com.mongodb.WriteResult)12 FailedDBOperationException (edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException)12 HashMap (java.util.HashMap)11 List (java.util.List)11 Date (java.util.Date)10 Map (java.util.Map)10 Mongo (com.mongodb.Mongo)9