Example usage for org.apache.commons.codec.digest DigestUtils shaHex

List of usage examples for org.apache.commons.codec.digest DigestUtils shaHex

Introduction

In this page you can find the example usage for org.apache.commons.codec.digest DigestUtils shaHex.

Prototype

@Deprecated
    public static String shaHex(String data) 

Source Link

Usage

From source file:org.eclipse.skalli.core.feed.FeedManagerComponent.java

private void setEntryId(FeedEntry entry) {
    StringBuilder newId = new StringBuilder(entry.getProjectId().toString());
    Date published = entry.getPublished();
    if (published != null) {
        newId.append(Long.toString(published.getTime()));
    }//from w ww  .  j  av  a  2s .c om
    newId.append(entry.getSource());
    if (StringUtils.isNotBlank(entry.getId())) {
        newId.append(entry.getId());
    }
    entry.setId(DigestUtils.shaHex(newId.toString()));
}

From source file:org.eclipse.skalli.core.feed.FeedManagerComponentTest.java

@Test
public void testUpdateAllFeeds() throws Exception {

    final Date aTestDate = new Date(1318946441120L);
    HashMapFeedService hashMapfeedService = new HashMapFeedService();

    FeedManagerComponent feedManagerImpl = new FeedManagerComponent(false);
    feedManagerImpl.bindFeedPersistenceService(hashMapfeedService);

    //bindProjectService
    ProjectService projectServiceMock = createMock(ProjectService.class);
    Set<UUID> projectIds = new HashSet<UUID>();
    projectIds.add(PROJECT1_UUID);
    projectIds.add(PROJECT2_UUID);
    Project p1 = new Project();
    p1.setUuid(PROJECT1_UUID);// w  w  w.  jav  a  2 s  . c  o m
    Project p2 = new Project();
    p2.setUuid(PROJECT2_UUID);
    expect(projectServiceMock.keySet()).andReturn(projectIds);
    expect(projectServiceMock.getByUUID(PROJECT1_UUID)).andReturn(p1);
    expect(projectServiceMock.getByUUID(PROJECT2_UUID)).andReturn(p2);
    replay(projectServiceMock);
    feedManagerImpl.bindProjectService(projectServiceMock);

    //bindFeedProvider
    FeedProvider feedProviderMock = createMock(FeedProvider.class);
    expect(feedProviderMock.getFeedUpdaters(p1)).andReturn(
            Collections.singletonList(getFeedUpdaterMock(hashMapfeedService, aTestDate, "source-a")));
    expect(feedProviderMock.getFeedUpdaters(p2)).andReturn(
            Collections.singletonList(getFeedUpdaterMock(hashMapfeedService, aTestDate, "source-b")));
    replay(feedProviderMock);
    feedManagerImpl.bindFeedProvider(feedProviderMock);

    feedManagerImpl.updateAllFeeds();

    assertThat(hashMapfeedService.getEntries().size(), is(3));

    Entry foundEntry1 = findEntryByTitle(hashMapfeedService.getEntries(), "title1-source-a");
    Entry foundEntry2 = findEntryByTitle(hashMapfeedService.getEntries(), "title2-source-a");
    Entry foundEntry3 = findEntryByTitle(hashMapfeedService.getEntries(), "title1-source-b");

    assertNotNull(foundEntry1);
    assertNotNull(foundEntry2);
    assertNotNull(foundEntry3);

    assertNotNull(foundEntry1.getId());

    assertThat(foundEntry1.getId(),
            is(DigestUtils.shaHex(PROJECT1_UUID.toString() + aTestDate.getTime() + "source-a")));
    assertThat(foundEntry1.getSource(), is("source-a"));
    assertThat(foundEntry1.getProjectId(), is(PROJECT1_UUID));
    assertThat(foundEntry1.getPublished(), is(aTestDate));

    assertNotNull(foundEntry2.getId());
    assertThat(foundEntry2.getId(), is("a4e8621588347aa9192f95708bdb48c8e4fb2b68")); //the original + alwasGeneratedPart as shaHex
    assertThat(foundEntry2.getSource(), is("source-a"));
    assertThat(foundEntry2.getProjectId(), is(PROJECT1_UUID));
    assertThat(foundEntry2.getPublished(), is(aTestDate));

    assertNotNull(foundEntry3.getId());
    assertThat(foundEntry3.getId(),
            is(DigestUtils.shaHex(PROJECT2_UUID.toString() + aTestDate.getTime() + "source-b")));
    assertThat(foundEntry3.getSource(), is("source-b"));
    assertThat(foundEntry3.getProjectId(), is(PROJECT2_UUID));
    assertThat(foundEntry3.getPublished(), is(aTestDate));
}

From source file:org.eclipse.skalli.core.feed.jpa.JPAFeedServiceTest.java

@Test
public void testMergeWithFind() throws Exception {
    final String source = ("source-" + JPAFeedPersistenceServiceTest.class.getSimpleName()).substring(0,
            EntryJPA.SOURCE_LENGTH);/*  w  ww  .j  a  v a 2  s.co m*/
    final String title = "TestAllFields";
    final Date testDate = new Date(1318946441120L);
    final String author_name = "Jon Smith";
    final String author_email = "JonSmith@somehwere";
    final String content_value = "test content";
    final String link_title = "link title";
    final String link_href = "href info";

    FeedPersistenceService jPAFeedPersistenceService = getFeedPersistenceService();
    FeedEntry newEntry = jPAFeedPersistenceService.createEntry();
    newEntry.setSource(source);
    newEntry.setProjectId(allFieldsProjectUuid);
    newEntry.setTitle(title);
    newEntry.setPublished(testDate);
    newEntry.getAuthor().setName(author_name);
    newEntry.getAuthor().setEmail(author_email);
    newEntry.getContent().setType("text");
    newEntry.getContent().setValue(content_value);
    newEntry.getLink().setTitle(link_title);
    newEntry.getLink().setHref(link_href);

    final String id = DigestUtils
            .shaHex(newEntry.getProjectId().toString() + newEntry.getTitle() + newEntry.getSource());
    newEntry.setId(id);

    Collection<FeedEntry> entries = new ArrayList<FeedEntry>();
    entries.add(newEntry);
    jPAFeedPersistenceService.merge(entries);

    FeedService jPAFeedService = getFeedService();
    List<Entry> foundEntries = jPAFeedService.findEntries(allFieldsProjectUuid, 10);
    assertThat(foundEntries.size(), is(1));

    Entry foundEntry = foundEntries.get(0);
    assertNotNull(foundEntry);
    assertThat(foundEntry.getId(), is(id));
    assertThat(foundEntry.getSource(), is(source));
    assertThat(foundEntry.getProjectId(), is(allFieldsProjectUuid));
    assertThat(foundEntry.getTitle(), is(title));
    assertThat(foundEntry.getPublished(), is(testDate));
    assertThat(foundEntry.getAuthor().getName(), is(author_name));
    assertThat(foundEntry.getAuthor().getEmail(), is(author_email));
    assertThat(foundEntry.getContent().getType(), is("text"));
    assertThat(foundEntry.getContent().getValue(), is(content_value));
    assertThat(foundEntry.getLink().getTitle(), is(link_title));
    assertThat(foundEntry.getLink().getHref(), is(link_href));

    // now update the entry and check that it updated
    final String newTitle = "new changed Title";
    newEntry.setTitle(newTitle);
    entries = new ArrayList<FeedEntry>();
    entries.add(newEntry);
    jPAFeedPersistenceService.merge(entries);
    foundEntries = jPAFeedService.findEntries(allFieldsProjectUuid, 10);
    assertThat(foundEntries.size(), is(1));
    foundEntry = foundEntries.get(0);
    assertNotNull(foundEntry);
    assertThat(foundEntry.getId(), is(id));
    assertThat(foundEntry.getTitle(), is(newTitle));
}

From source file:org.eclipse.skalli.core.feed.jpa.JPAFeedServiceTest.java

private void updateId(FeedEntry newEntry) {
    // in this test we calculate the id via project, title and source.
    String id = newEntry.getProjectId().toString() + newEntry.getTitle() + newEntry.getSource();
    newEntry.setId(DigestUtils.shaHex(id));
}

From source file:org.efaps.webdav4vfs.util.Util.java

/**
 *
 * @param _object//from ww  w .j  a  va2  s  . c om
 * @return
 */
public static String getETag(final FileObject _object) {
    final String fileName = _object.getName().getPath();
    String lastModified = "";
    try {
        lastModified = String.valueOf(_object.getContent().getLastModifiedTime());
    } catch (final FileSystemException e) {
        // ignore error here
    }
    return DigestUtils.shaHex(fileName + lastModified);
}

From source file:org.freeciv.servlet.LoadServlet.java

@SuppressWarnings("unchecked")
public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {

    InputStream in = request.getInputStream();

    String encodedFile = IOUtils.toString(in, Charset.forName("UTF-8"));
    byte[] compressedFile = Base64.decodeBase64(encodedFile);

    String savename = "" + request.getParameter("savename");
    String username = "" + request.getParameter("username");
    String savegameHash = DigestUtils.shaHex(username + savename + encodedFile);

    if (!p.matcher(username).matches()) {
        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Invalid username");
        return;//from  w w  w .j  a  va2s  .  c o  m
    }

    Connection conn = null;
    try {
        Context env = (Context) (new InitialContext().lookup("java:comp/env"));
        DataSource ds = (DataSource) env.lookup("jdbc/freeciv_mysql");
        conn = ds.getConnection();

        PreparedStatement stmt = conn.prepareStatement(
                "select count(*) from savegames where username = ? and title = ? and digest = ?");
        stmt.setString(1, username);
        stmt.setString(2, savename);
        stmt.setString(3, savegameHash);

        ResultSet rs = stmt.executeQuery();
        if (rs.next()) {
            if (rs.getInt(1) != 1) {
                response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Invalid savegame");
                return;
            }
        } else {
            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Invalid savegame");
            return;
        }

        String relativeWebPath = "/savegames/" + username + ".sav.bz2";
        String absoluteDiskPath = getServletContext().getRealPath(relativeWebPath);
        File file = new File(absoluteDiskPath);
        FileUtils.writeByteArrayToFile(file, compressedFile);

    } catch (Exception err) {
        err.printStackTrace();
        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Savegame digest failed.");
        return;

    } finally {
        if (conn != null)
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
    }

    in.close();

    response.getOutputStream().print("success");

}

From source file:org.freeciv.servlet.SaveServlet.java

@SuppressWarnings("unchecked")
public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {

    String username = "" + request.getParameter("username");
    if (!p.matcher(username).matches()) {
        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Invalid username");
        return;// w w  w  .j ava 2s  .  c o m
    }

    String savename = "" + request.getParameter("savename");

    if (savename.length() == 0 || username.length() == 0 || savename.length() >= 64
            || username.length() >= 32) {
        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Invalid username or savename.");
        return;
    }

    String relativeWebPath = "/savegames/" + username + ".sav.bz2";
    String absoluteDiskPath = getServletContext().getRealPath(relativeWebPath);
    File file = new File(absoluteDiskPath);
    if (!file.exists()) {
        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Savegame file not found!");
        return;

    } else if (file.length() == 0 || file.length() > 5000000) {
        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Invalid file size of savegame.");
        return;

    }

    InputStream in = new FileInputStream(file);
    byte[] compressedFile = IOUtils.toByteArray(in);
    String encodedFile = new String(Base64.encodeBase64(compressedFile));
    String savegameHash = DigestUtils.shaHex(username + savename + encodedFile);

    Connection conn = null;
    try {
        Context env = (Context) (new InitialContext().lookup("java:comp/env"));
        DataSource ds = (DataSource) env.lookup("jdbc/freeciv_mysql");
        conn = ds.getConnection();

        PreparedStatement stmt = conn.prepareStatement("INSERT INTO savegames VALUES (NULL, ?,?,?)");
        stmt.setString(1, username);
        stmt.setString(2, savename);
        stmt.setString(3, savegameHash);
        stmt.executeUpdate();

    } catch (Exception err) {
        err.printStackTrace();
        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Savegame digest failed.");
        return;

    } finally {
        if (conn != null)
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
    }

    response.getOutputStream().print(encodedFile);
    in.close();

}

From source file:org.geoserver.platform.resource.FileLockProvider.java

private File getFile(String lockKey) {
    File locks = new File(root, "filelocks"); // avoid same directory as GWC
    locks.mkdirs();//w w w  .jav  a2s  .com
    String sha1 = DigestUtils.shaHex(lockKey);
    return new File(locks, sha1 + ".lock");
}

From source file:org.geoserver.platform.resource.MemoryLockProvider.java

private int getIndex(String lockKey) {
    // Simply hashing the lock key generated a significant number of collisions,
    // doing the SHA1 digest of it provides a much better distribution
    int idx = Math.abs(DigestUtils.shaHex(lockKey).hashCode() % locks.length);
    return idx;//from   w  ww.  j a v a 2  s . c om
}

From source file:org.geowebcache.locks.NIOLockProvider.java

private File getFile(String lockKey) {
    File locks = new File(root, "lockfiles");
    locks.mkdirs();// ww w  .j a  v  a2s .c o  m
    String sha1 = DigestUtils.shaHex(lockKey);
    return new File(locks, sha1 + ".lck");
}