Example usage for java.util Date setTime

List of usage examples for java.util Date setTime

Introduction

In this page you can find the example usage for java.util Date setTime.

Prototype

public void setTime(long time) 

Source Link

Document

Sets this Date object to represent a point in time that is time milliseconds after January 1, 1970 00:00:00 GMT.

Usage

From source file:org.apache.ws.security.message.TimestampTest.java

/**
 * This is a test for processing an Timestamp where the "Created" element is in the (near)
 * future. It should be accepted by default when it is created 30 seconds in the future, 
 * and then rejected once we configure "0 seconds" for future-time-to-live.
 *///from www  .j a  v a 2 s . c o m
@org.junit.Test
public void testNearFutureCreated() throws Exception {

    Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
    WSSecHeader secHeader = new WSSecHeader();
    secHeader.insertSecurityHeader(doc);

    Element timestampElement = doc.createElementNS(WSConstants.WSU_NS,
            WSConstants.WSU_PREFIX + ":" + WSConstants.TIMESTAMP_TOKEN_LN);

    DateFormat zulu = new XmlSchemaDateFormat();
    Element elementCreated = doc.createElementNS(WSConstants.WSU_NS,
            WSConstants.WSU_PREFIX + ":" + WSConstants.CREATED_LN);
    Date createdDate = new Date();
    long currentTime = createdDate.getTime() + 30000;
    createdDate.setTime(currentTime);
    elementCreated.appendChild(doc.createTextNode(zulu.format(createdDate)));
    timestampElement.appendChild(elementCreated);

    secHeader.getSecurityHeader().appendChild(timestampElement);

    if (LOG.isDebugEnabled()) {
        String outputString = org.apache.ws.security.util.XMLUtils.PrettyDocumentToString(doc);
        LOG.debug(outputString);
    }
    //
    // Do some processing
    //
    WSSConfig config = WSSConfig.getNewInstance();
    verify(doc, config);
    try {
        config.setTimeStampFutureTTL(0);
        verify(doc, config);
        fail("The timestamp validation should have failed");
    } catch (WSSecurityException ex) {
        assertTrue(ex.getErrorCode() == WSSecurityException.MESSAGE_EXPIRED);
    }
}

From source file:org.apache.ws.security.message.TimestampTest.java

/**
 * This is a test for processing an Timestamp where it contains no "Created" element.
 * This Timestamp should be rejected.//  w  w  w . ja  va 2  s. c  o  m
 */
@org.junit.Test
public void testNoCreated() throws Exception {

    Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
    WSSecHeader secHeader = new WSSecHeader();
    secHeader.insertSecurityHeader(doc);

    Element timestampElement = doc.createElementNS(WSConstants.WSU_NS,
            WSConstants.WSU_PREFIX + ":" + WSConstants.TIMESTAMP_TOKEN_LN);

    DateFormat zulu = new XmlSchemaDateFormat();
    Element elementCreated = doc.createElementNS(WSConstants.WSU_NS,
            WSConstants.WSU_PREFIX + ":" + WSConstants.EXPIRES_LN);
    Date createdDate = new Date();
    long currentTime = createdDate.getTime() + 300000;
    createdDate.setTime(currentTime);
    elementCreated.appendChild(doc.createTextNode(zulu.format(createdDate)));
    timestampElement.appendChild(elementCreated);

    secHeader.getSecurityHeader().appendChild(timestampElement);

    if (LOG.isDebugEnabled()) {
        String outputString = org.apache.ws.security.util.XMLUtils.PrettyDocumentToString(doc);
        LOG.debug(outputString);
    }
    //
    // Do some processing
    //
    WSSConfig wssConfig = WSSConfig.getNewInstance();
    wssConfig.setWsiBSPCompliant(true);
    try {
        verify(doc, wssConfig);
        fail("The timestamp validation should have failed on no Created element");
    } catch (WSSecurityException ex) {
        // expected
    }
}

From source file:org.apache.ws.security.message.TimestampTest.java

/**
 * This is a test for processing an Timestamp where it contains an "Expires" element before
 * the Created element. This Timestamp should be rejected as per the BSP spec.
 *//*from   w ww  . j  a v  a2s  .c  om*/
@org.junit.Test
public void testExpiresInFrontOfCreated() throws Exception {

    Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
    WSSecHeader secHeader = new WSSecHeader();
    secHeader.insertSecurityHeader(doc);

    Element timestampElement = doc.createElementNS(WSConstants.WSU_NS,
            WSConstants.WSU_PREFIX + ":" + WSConstants.TIMESTAMP_TOKEN_LN);

    DateFormat zulu = new XmlSchemaDateFormat();
    Element elementCreated = doc.createElementNS(WSConstants.WSU_NS,
            WSConstants.WSU_PREFIX + ":" + WSConstants.EXPIRES_LN);
    Date createdDate = new Date();
    long currentTime = createdDate.getTime() + 300000;
    createdDate.setTime(currentTime);
    elementCreated.appendChild(doc.createTextNode(zulu.format(createdDate)));
    timestampElement.appendChild(elementCreated);

    Element elementExpires = doc.createElementNS(WSConstants.WSU_NS,
            WSConstants.WSU_PREFIX + ":" + WSConstants.CREATED_LN);
    elementExpires.appendChild(doc.createTextNode(zulu.format(createdDate)));
    timestampElement.appendChild(elementExpires);

    secHeader.getSecurityHeader().appendChild(timestampElement);

    if (LOG.isDebugEnabled()) {
        String outputString = org.apache.ws.security.util.XMLUtils.PrettyDocumentToString(doc);
        LOG.debug(outputString);
    }
    //
    // Do some processing
    //
    WSSConfig wssConfig = WSSConfig.getNewInstance();
    wssConfig.setWsiBSPCompliant(true);
    try {
        verify(doc, wssConfig);
        fail("The timestamp validation should have failed");
    } catch (WSSecurityException ex) {
        // expected
    }
}

From source file:org.apache.ws.security.message.TimestampTest.java

/**
 * This is a test for processing an Timestamp where the "Created" element is greater than
 * the expiration time./*from w  w  w.j av  a2  s . com*/
 */
@org.junit.Test
public void testExpiresBeforeCreated() throws Exception {

    Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
    WSSecHeader secHeader = new WSSecHeader();
    secHeader.insertSecurityHeader(doc);

    Element timestampElement = doc.createElementNS(WSConstants.WSU_NS,
            WSConstants.WSU_PREFIX + ":" + WSConstants.TIMESTAMP_TOKEN_LN);

    DateFormat zulu = new XmlSchemaDateFormat();
    Element elementCreated = doc.createElementNS(WSConstants.WSU_NS,
            WSConstants.WSU_PREFIX + ":" + WSConstants.CREATED_LN);
    Date createdDate = new Date();
    long currentTime = createdDate.getTime() + 300000;
    createdDate.setTime(currentTime);
    elementCreated.appendChild(doc.createTextNode(zulu.format(createdDate)));
    timestampElement.appendChild(elementCreated);

    Date expiresDate = new Date();
    expiresDate.setTime(expiresDate.getTime() - 300000);

    Element elementExpires = doc.createElementNS(WSConstants.WSU_NS,
            WSConstants.WSU_PREFIX + ":" + WSConstants.EXPIRES_LN);
    elementExpires.appendChild(doc.createTextNode(zulu.format(expiresDate)));
    timestampElement.appendChild(elementExpires);

    secHeader.getSecurityHeader().appendChild(timestampElement);

    if (LOG.isDebugEnabled()) {
        String outputString = org.apache.ws.security.util.XMLUtils.PrettyDocumentToString(doc);
        LOG.debug(outputString);
    }
    //
    // Do some processing
    //
    try {
        verify(doc, WSSConfig.getNewInstance());
        fail("The timestamp validation should have failed");
    } catch (WSSecurityException ex) {
        assertTrue(ex.getErrorCode() == WSSecurityException.MESSAGE_EXPIRED);
    }
}

From source file:org.apache.ws.security.message.TimestampTest.java

/**
 * This is a test for processing an Timestamp where it contains a Created element with
 * a ValueType. This should be rejected as per the BSP spec.
 *//*from w  w w  .java2s. c om*/
@org.junit.Test
public void testCreatedValueType() throws Exception {

    Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
    WSSecHeader secHeader = new WSSecHeader();
    secHeader.insertSecurityHeader(doc);

    Element timestampElement = doc.createElementNS(WSConstants.WSU_NS,
            WSConstants.WSU_PREFIX + ":" + WSConstants.TIMESTAMP_TOKEN_LN);

    DateFormat zulu = new XmlSchemaDateFormat();
    Element elementCreated = doc.createElementNS(WSConstants.WSU_NS,
            WSConstants.WSU_PREFIX + ":" + WSConstants.CREATED_LN);
    Date createdDate = new Date();
    long currentTime = createdDate.getTime() + 300000;
    createdDate.setTime(currentTime);
    elementCreated.appendChild(doc.createTextNode(zulu.format(createdDate)));
    elementCreated.setAttributeNS(null, "ValueType", WSConstants.WSS_SAML_KI_VALUE_TYPE);
    timestampElement.appendChild(elementCreated);

    secHeader.getSecurityHeader().appendChild(timestampElement);

    if (LOG.isDebugEnabled()) {
        String outputString = org.apache.ws.security.util.XMLUtils.PrettyDocumentToString(doc);
        LOG.debug(outputString);
    }
    //
    // Do some processing
    //
    WSSConfig wssConfig = WSSConfig.getNewInstance();
    wssConfig.setWsiBSPCompliant(true);
    wssConfig.setValidator(WSSecurityEngine.TIMESTAMP, new NoOpValidator());
    try {
        verify(doc, wssConfig);
        fail("The timestamp validation should have failed");
    } catch (WSSecurityException ex) {
        //
    }

    // Now it should pass...
    wssConfig.setWsiBSPCompliant(false);
    verify(doc, wssConfig);
}

From source file:org.apache.ws.security.message.TimestampTest.java

/**
 * This is a test for processing an Timestamp where it contains a CustomElement. This should
 * be rejected as per the BSP spec.//from   www.j ava  2  s.  c o m
 */
@org.junit.Test
public void testCustomElement() throws Exception {

    Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG);
    WSSecHeader secHeader = new WSSecHeader();
    secHeader.insertSecurityHeader(doc);

    Element timestampElement = doc.createElementNS(WSConstants.WSU_NS,
            WSConstants.WSU_PREFIX + ":" + WSConstants.TIMESTAMP_TOKEN_LN);

    DateFormat zulu = new XmlSchemaDateFormat();
    Element elementCreated = doc.createElementNS(WSConstants.WSU_NS,
            WSConstants.WSU_PREFIX + ":" + WSConstants.EXPIRES_LN);
    Date createdDate = new Date();
    long currentTime = createdDate.getTime() + 300000;
    createdDate.setTime(currentTime);
    elementCreated.appendChild(doc.createTextNode(zulu.format(createdDate)));
    timestampElement.appendChild(elementCreated);

    Element elementCustom = doc.createElementNS(WSConstants.WSU_NS, WSConstants.WSU_PREFIX + ":" + "Custom");
    timestampElement.appendChild(elementCustom);

    secHeader.getSecurityHeader().appendChild(timestampElement);

    if (LOG.isDebugEnabled()) {
        String outputString = org.apache.ws.security.util.XMLUtils.PrettyDocumentToString(doc);
        LOG.debug(outputString);
    }
    //
    // Do some processing
    //
    WSSConfig wssConfig = WSSConfig.getNewInstance();
    wssConfig.setWsiBSPCompliant(true);
    try {
        verify(doc, wssConfig);
        fail("The timestamp validation should have failed");
    } catch (WSSecurityException ex) {
        //
    }

    // Now it should pass...
    wssConfig.setWsiBSPCompliant(false);
    verify(doc, wssConfig);
}

From source file:uk.ac.susx.tag.method51.webapp.supergui.SuperGuiRoutes.java

public SuperGuiRoutes(File configFile) throws IOException {
    final Options opts = Params.fromFile(Options.class, configFile);
    File projectDir = opts.projectsDir.get();

    Project p;/*  w ww . j a  va 2  s.  c  o m*/
    try {
        p = Project.loadProject(projectDir);
    } catch (IOException e) {
        p = Project.create(projectDir.getParentFile(), projectDir.getName());
    }
    projects = p;

    resourceDir = opts.superguiResourceDir.get();

    final ResourceRegistry resources = ResourceRegistry.getRegistry();

    resources.addResources(new SuperGUICore());
    resources.addResources(new SuperGUILib());

    resources.loadResources("supergui");
    resources.loadResources("supergui-lib");

    final boolean dev = opts.dev.get();
    final Date loaded = new Date();

    final ResourceHandler resourceHandler = new ResourceHandler() {
        {
            setMimeTypes(Routes.mimeTypes);
            setResourceBase("data/webapp/supergui/public");
        }

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request,
                HttpServletResponse response) throws IOException, ServletException {
            if (dev && (new Date().getTime() - loaded.getTime() > 1000)) {
                loaded.setTime(new Date().getTime());
                System.out.println("loading supergui resources");
                resources.loadResources("supergui");
            }
            super.handle(target, baseRequest, request, response);
        }
    };

    setResourceHandler(resourceHandler);

    GET("/", new HelpfulHandler() {
        @Override
        public Object getResult(String target, Request baseRequest, HttpServletRequest request,
                HttpServletResponse response) throws Exception {
            resourceHandler.handle("index.html", baseRequest, request, response);
            return null;
        }
    });

    route("/projects", new ProjectsRoutes(projects));
    route("/params", new ParamsRoutes());
    route("/jobs", new JobsRoutes(projects));
    route("/route/", new DynamicRoutes(configFile, projects));

    GET("/components", new SimpleHandler() {
        @Override
        public Object handle(Request request, HttpServletResponse response) throws Exception {
            return jsonObject("components", SuperGUIResources.listComponents());
        }
    });

    GET("/renderers", new SimpleHandler() {
        @Override
        public Object handle(Request request, HttpServletResponse response) throws Exception {
            return jsonObject("renderers", SuperGUIResources.listRenderers());
        }
    });

    route("/webapp", new Routes() {
        {

            GET("/output-path", new SimpleHandler() {
                @Override
                public Object handle(Request request, HttpServletResponse response) throws Exception {
                    return new StringResult(new File(opts.webappPath.get(), opts.outputPath).getPath());
                }
            });

            GET("/output-uri", new SimpleHandler() {
                @Override
                public Object handle(Request request, HttpServletResponse response) throws Exception {
                    return new StringResult(opts.outputPath);
                }
            });
        }
    });
}

From source file:com.xpn.xwiki.doc.XWikiAttachment.java

public void setDate(Date date) {
    // Make sure we drop milliseconds for consistency with the database
    if (date != null) {
        date.setTime((date.getTime() / 1000) * 1000);
    }/*w w  w.  j  a va2s .  c om*/

    this.date = date;
}

From source file:com.glandorf1.joe.wsprnetviewer.app.sync.WsprNetViewerSyncAdapter.java

/**
 * Clean up and parse the raw timestamp from the html.
 * Since there may be multiple records with the same timestamp (the resolution is only 1 minute),
 * make sure there is a mechanism to distinguish each of them.
 *///  w  ww  .  j  av  a2 s . c  om
public String parseTimestamp(String timestampStr) {
    SimpleDateFormat timestampFormatIn = new SimpleDateFormat(Utility.TIMESTAMP_FORMAT_WSPR);
    // TODO: getting parse exceptions-but maybe only in debug mode; SDF may not be suitable for
    //       use in static modules.
    //       Investigate using something like joda-time: http://www.joda.org/joda-time/
    try {
        Date inputTimestamp = timestampFormatIn.parse(timestampStr);
        inputTimestamp.setTime(inputTimestamp.getTime()); // + millisecondOffset); // can add a fake ms value to make timestamp unique
        String dbTimestamp = WsprNetContract.getDbTimestampString(inputTimestamp);
        return dbTimestamp;
    } catch (ParseException e) {
        Log.e(LOG_TAG, e.getMessage(), e);
        //e.printStackTrace();
        return timestampStr;
    }
}

From source file:org.apache.griffin.core.job.JobServiceImpl.java

public void setJobStartTime(Date jobStartTime, int interval) {
    long currentTimestamp = System.currentTimeMillis();
    long jobstartTimestamp = jobStartTime.getTime();
    //if jobStartTime is before currentTimestamp, reset it with a future time
    if (jobStartTime.before(new Date(currentTimestamp))) {
        long n = (currentTimestamp - jobstartTimestamp) / (long) (interval * 1000);
        jobstartTimestamp = jobstartTimestamp + (n + 1) * (long) (interval * 1000);
        jobStartTime.setTime(jobstartTimestamp);
    }/*from   ww  w  .j a  va 2s  . c o  m*/
}