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);
}
}
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();
}
}
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();
}
}
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();
}
}
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;
}
Aggregations