Skip to content

Instantly share code, notes, and snippets.

@aweigold
aweigold / build.gradle
Created March 29, 2015 20:56
Incorporating your README.md, doclet output, and reports with the gradle site plugin
buildscript {
repositories {
maven {
name = 'BintrayJCenter'
url = 'http://jcenter.bintray.com'
}
mavenCentral()
}
dependencies {
classpath 'us.carrclan.david.gradle:gradle-site-plugin:0.2.0'
@aweigold
aweigold / GetAuthMappings.groovy
Last active August 29, 2015 14:13
Auditing your Spring Security mappings with Groovy
//Add net.sf.corn:corn-cps:1.0.1 to your classpath
import net.sf.corn.cps.*
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.security.access.prepost.PreAuthorize
def classes = CPScanner.scanClasses(new PackageNameFilter("com.company.*"), new ClassFilter().appendAnnotation(Controller))
classes.each { clazz ->
clazz.getDeclaredMethods().each { method ->
RequestMapping mapping = method.getAnnotation(RequestMapping)
@aweigold
aweigold / DevWebResourceAdapter.java
Last active August 29, 2015 14:06
Adding grunt based javascript projects into Spring application in development
@Configuration
@Profile("dev")
public class DevWebResourceAdapter extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**")
.addResourceLocations("file:./submodule-web/app/");
registry.addResourceHandler("/bower_components/**")
.addResourceLocations("file:./submodule-web/bower_components/");
@aweigold
aweigold / binary UTF-8 String to string.groovy
Last active August 29, 2015 14:04
convert varbinary utf-8 to String
String s = '0x7B2274797065223A226469636F6D2E736572696573222C227374756479496E7374616E6365556964223A22312E332E362E312E342E312E32353430332E39373339343437353933333536392E363634302E32303133303531303033313330322E32222C226172636869766555726C223A22687474703A2F2F687474703A2F2F6C6F63616C686F73743A383730302F6D616E6167656D656E742F6170692F61726368697665732F64656661756C742F737475646965732F312E332E362E312E342E312E32353430332E39373339343437353933333536392E363634302E32303133303531303033313330322E32222C226D696E7455726C223A22687474703A2F2F6C6F63616C686F73743A383735302F646174612F6170692F737475646965732F312E332E362E312E342E312E32353430332E39373339343437353933333536392E363634302E32303133303531303033313330322E32222C22736572696573496E7374616E636555696473223A5B22312E332E362E312E342E312E32353430332E39373339343437353933333536392E363634302E32303133303531303033313330352E3135225D2C2261747472696275746573223A7B22426F6479506172744578616D696E6564223A224368657374222C2243617264696163506861736550657263656E74223A22222C22496D616765436F6D6D656E7473
@aweigold
aweigold / BinaryChecksum.groovy
Created October 24, 2013 20:07
SQL BINARY_CHECKSUM implementation in Groovy See http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=70832 for more information
long sum = 0
byte overflow;
String text = 'mystring'
for (int i =0; i< text.length(); i++){
sum = (long)((16* sum) ^ text.getBytes()[i])
overflow = (byte) (sum / 4294967296)
sum = sum - overflow * 4294967296
sum = sum ^ overflow
}
if (sum > 2147483647) {
@aweigold
aweigold / build.gradle
Last active December 20, 2015 23:38
Running custom builds for IntelliJ JUnit runner http://www.adamweigold.com/2013/08/intellij-idea-junit-running-and-non.html IntelliJ Idea provides some really awesome JUnit runners that allow you to right click test methods, classes, and packages, and quickly run/debug them. The default JUnit configuration runs Idea's "Make" prior to running you…
apply plugin: 'idea'
idea {
project {
ipr.withXml {
// Create a task to run gradle testClasses, which we will subsequiently bind to Default JUnit runner to
def runConfigComp = it.node.appendNode('component')
def runOpts = [
"default": "false",
"name": "testClasses for JUnit",
@aweigold
aweigold / SecurityContextWrappedMultipartRequestArgumentResolver.java
Last active December 20, 2015 23:29
Using a MultpartRequestResolver with Spring and using Spring Security concurrently http://www.adamweigold.com/2012/01/using-multpartrequestresolvers-with.html This will not work with Spring 3.1. This is due to the ServletRequestMethodArgumentResolver being added by default prior to custom argument resolvers in a private method in the RequestMapp…
public class SecurityContextWrappedMultipartRequestArgumentResolver implements WebArgumentResolver {
private final CommonsMultipartResolver commonsMultipartResolver;
public SecurityContextWrappedMultipartRequestArgumentResolver(){
this.commonsMultipartResolver = new CommonsMultipartResolver();
}
@Override
public Object resolveArgument(MethodParameter methodParameter, NativeWebRequest webRequest) throws Exception {
@aweigold
aweigold / ParentEntity.java
Created August 12, 2013 17:40
Hibernate, ElementCollection, and Transactions http://www.adamweigold.com/2011/11/hibernate-elementcollection-and.html Hibernate implemented @ElementCollection in the JPA by binding the persistence of the ElementCollection of a new entity at the end of the transaction, and NOT at the time you tell the EntityManager to persist. Under most use cas…
@Entity
@Table(name = "ParentEntity")
public class ParentEntity {
@Id
@GeneratedValue
@Column(name= "ParentId")
private long parentId;
@ElementCollection(fetch = FetchType.EAGER)
@aweigold
aweigold / Export DB to CSV with BCP.sql
Last active December 20, 2015 23:29
Exporting all tables to CSV in SQL Serverhttp://www.adamweigold.com/2011/11/exporting-all-tables-to-csv-in-sql.htmlThe following script will output a list of statements to use bcp to export all tables to CSV. To use another format, see the bcp documentation.
USE myDatabase
SELECT 'exec master..xp_cmdshell'
+ ' '''
+ 'bcp'
+ ' ' + TABLE_CATALOG + '.' + TABLE_SCHEMA + '.' + TABLE_NAME
+ ' out'
+ ' E:\releasedDB\prod\'
+ TABLE_CATALOG + '.' + TABLE_SCHEMA + '.' + TABLE_NAME + '.csv'
+ ' -c'
+ ' -t,'
@aweigold
aweigold / drop MS SQL Server DB.sql
Last active December 20, 2015 23:29
Deleting a database remotely with SQL Serverhttp://www.adamweigold.com/2011/11/deleting-database-remotely-with-sql.htmlThis should never be done on a production server, as it will open up security risks. This is useful for integration tests and utilities. Replace %dbName% appropriately
BEGIN
IF EXISTS (SELECT * FROM tempdb.sys.tables WHERE name LIKE '#dbFiles%')
DROP TABLE #dbFiles
END
EXEC master.dbo.sp_configure 'show advanced options', 1
RECONFIGURE
EXEC master.dbo.sp_configure 'xp_cmdshell', 1
RECONFIGURE