Last active
December 11, 2015 07:08
-
-
Save ivv-private/4563992 to your computer and use it in GitHub Desktop.
My Code Samples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Solr Results to Domain Object Transformation. | |
Allows to map results to different object classes using distinguisher | |
tags: Solr, Springframework, Common Collections | |
*/ | |
@Component | |
public class SearchDaoImpl { | |
@Autowired | |
@Qualifier("search-dao-domain-transformer") | |
Transformer domainTransformer; | |
/** | |
* Appends highlight snippets to domain object | |
*/ | |
private static class HlTransformer implements Transformer { | |
private Map<String, Map<String, List<String>>> hl; | |
public HlTransformer(Map<String, Map<String, List<String>>> hl) { | |
this.hl = hl; | |
} | |
public Object transform(Object input) { | |
AbstractDomainBean bean = (AbstractDomainBean) input; | |
bean.setHighlighting(hl.get(bean.getUniqueId())); | |
return input; | |
} | |
} | |
public List<AbstractDomainBean> search(SolrQuery q) throws IOException, SolrServerException { | |
QueryResponse response = server.query(q); | |
Transformer transformer = chainedTransformer(new Transformer[]{ | |
domainTransformer, new HlTransformer(response.getHighlighting())}); | |
return collect(response.getResults(), transformer); | |
} | |
} | |
/** | |
* Matches prefix in the unique field | |
*/ | |
static class Prefix implements Predicate { | |
private String prefix; | |
public PrefixDistinguisher (String prefix) { | |
this.prefix = prefix; | |
} | |
public boolean evaluate (Object doc) { | |
String uid = (String) ((SolrDocument) doc).get ("uid"); | |
return StringUtils.startsWith (uid, prefix); | |
} | |
} | |
/** | |
* Transforms SolrDocument to domain object | |
*/ | |
public static class DomainTransformer implements Transformer { | |
private Class type; | |
private DocumentObjectBinder binder = new DocumentObjectBinder(); | |
public DomainTransformer(Class type) { | |
this.type = type; | |
} | |
public Object transform(Object input) { | |
SolrDocument doc = (SolrDocument) input; | |
return binder.getBean(type, doc); | |
} | |
} | |
/** | |
* Transforms results using prefixed field | |
* if uniqueId starts with "p" then get PBean ... | |
*/ | |
@Bean | |
@Qualifier("search-dao-domain-transformer") | |
Transformer domainTransformer = TransformerUtils | |
.switchTransformer ( | |
new Predicate[]{new Prefix ("p"), new Prefix ("c"), new Prefix ("r")}, | |
new Transformer[]{ | |
new Domain-Transformer (PBean.class), | |
new Domain-Transformer (CBean.class), | |
new Domain-Transformer (RBean.class)}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Import certificates into LDAP using canonical name | |
*/ | |
public class Import { | |
// canonical order | |
static List ldapOrder = ['c', 'o', 'st', 'l', 'ou', 'cn']; | |
// entries to process | |
static Map entries = [ | |
c: new Country(), | |
o: new Org(), | |
st: new State(), | |
l: new City(), | |
ou: new Unit(), | |
cn: [ca: new CA(), user: new User()] | |
] | |
static isCA(cert) { | |
return (cert.keyUsage[KEY_CERT_SIGN] || cert.keyUsage[CRL_SIGN]) | |
} | |
// to assosiative array | |
static keysToList(map) { | |
map.keySet().toList() | |
} | |
// parse string dname to map | |
static dnToMap(dn) { | |
def result = [:] | |
dn.findAll(/([^,=]+)=([^,]+)/) { | |
full, attr, value -> result[attr.trim().toLowerCase()] = value | |
} | |
result | |
} | |
// make canonical path | |
static makePath(dn) { | |
keysToList(dn) | |
.sort{ ldapOrder.indexOf(it) } | |
.findAll{ it in (ldapOrder-'cn') } | |
.inject("") { path, it -> | |
def entry = entries[it].clone() | |
entry.name = dn[it] | |
path = "${it}=${entry.name}${path}" | |
Yagll.bind(entry, [ent: path]) | |
path = ",${path}" | |
} | |
} | |
static install(cert) { | |
def dn = dnToMap(cert.subjectX500Principal.getName(X500Principal.RFC1779)) | |
makePath(dn) | |
def ldapDNString = keysToList(dn) | |
.sort{-ldapOrder.indexOf(it)} | |
.inject("") { name, it -> | |
name += ",${it}=${dn[it]}" | |
name | |
} - "," | |
def subject = entries['cn'][isCA(cert) ? 'ca' : 'user'].clone() | |
Yagll.lookup(subject, [ent: ldapDNString]) | |
subject.name = dn['cn'] | |
subject.acceptCertificate(cert) | |
Yagll.bind(subject, [ent: ldapDNString]) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Conkeror fucntion | |
// Show explanation of the selected word in minibuffer | |
// | |
require("string.js"); | |
function dict_popup_init (buffer) { | |
buffer.dict_mouseclick = function (event) { | |
let selected = trim_whitespace(buffer.top_frame.getSelection().toString()); | |
if (event.button == 0 || !selected) { | |
return; | |
} | |
let request = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"] | |
.createInstance(Ci.nsIXMLHttpRequest); | |
request.onload = function(aEvent) { | |
response = aEvent.target.responseXML; | |
let xslt = make_file(get_pref("conkeror.rcfile") + "/dict-yandex.xslt"); | |
let domParser = Cc["@mozilla.org/xmlextras/domparser;1"] | |
.createInstance(Ci.nsIDOMParser); | |
let xsltProc = Cc["@mozilla.org/document-transformer;1?type=xslt"] | |
.createInstance(Ci.nsIXSLTProcessor); | |
let serializer = Cc["@mozilla.org/xmlextras/xmlserializer;1"] | |
.createInstance(Ci.nsIDOMSerializer); | |
let xslDocument = domParser.parseFromString(read_text_file(xslt), "text/xml"); | |
xsltProc.importStylesheet(xslDocument); | |
let out = xsltProc.transformToDocument(response); | |
buffer.window.minibuffer.show(serializer.serializeToString(out.documentElement.firstChild)); | |
}; | |
request.onerror = function(aEvent) { | |
buffer.window.minibuffer.show("Error Status: " + aEvent.target.status); | |
}; | |
request.open("GET", "http://m.slovari.yandex.ru/translate.xml?text=" + selected, true); | |
request.overrideMimeType("text/xml"); | |
request.send(null); | |
}; | |
buffer.browser.addEventListener("click", buffer.dict_mouseclick, true); | |
} | |
define_global_mode("dict_popup_mode", | |
function enable () { | |
add_hook("create_buffer_hook", dict_popup_init); | |
for_each_buffer(dict_popup_init); | |
}, | |
function disable () { | |
remove_hook("create_buffer_hook", dict_popup_init); | |
for_each_buffer(function (b) { | |
b.browser.removeEventListener("click", b.dict_mouseover, true); | |
}); | |
}, | |
$doc = "Right click to show explanation of the selected word in minibuffer" | |
); | |
dict_popup_mode(true); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# Multiprocess search for classes in archives | |
# Example | |
# find-in-jar ~/.m2/repository/org/apache/solr/ Embedded | |
# Output: | |
# ~/.m2/repository/org/apache/solr/solr-core/3.5.0/solr-core-3.5.0.jar org/apache/solr/client/solrj/embedded/EmbeddedSolrServer.class | |
find "${1}" \( -iname '*jar' -o -iname '*war' \) -type f -printf \ | |
| 'jar -tf %h/%f | xargs -n1 echo %h/%f\0' \ | |
| xargs -0 -n1 -P10 bash --noediting -c \ | |
| grep "${2}" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0"?> | |
<!-- | |
Flushes all nodes inside f:feature | |
example web.xml (pre-processed) | |
... | |
<f:feature enabled="${use.statistics}"> | |
<servlet> | |
<servlet-name>stat-servlet</servlet-name> | |
<servlet-class>foo.bar.StatisticsServlet</servlet-class> | |
<servlet> | |
</f:feature> | |
... | |
<f:feature enabled="${use.statistics}"> | |
<servlet-mapping> | |
<servlet-name>stat-servlet</servlet-name> | |
<url-pattern>/s/*</url-pattern> | |
</servlet-mapping> | |
</f:feature> | |
... | |
mvn install -Duse.statistics=on | |
--> | |
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" | |
xmlns:f="http://foo.bar/features"> | |
<xsl:strip-space elements="*"/> | |
<xsl:output method="xml" indent="yes"/> | |
<xsl:template match="@*|node()"> | |
<xsl:copy> | |
<xsl:apply-templates select="@*|node()" /> | |
</xsl:copy> | |
</xsl:template> | |
<xsl:template match="f:feature[@enabled = 'on']"> | |
<xsl:apply-templates select="node()" /> | |
</xsl:template> | |
<xsl:template match="f:feature[@enabled != 'on']"/> | |
</xsl:stylesheet> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; | |
;; Search sources and make .jdbrc settings file | |
;; | |
(require 'find-lisp) | |
(defvar jde-sourcepath-root "~/projects" | |
"Root directory which contains a sources") | |
(defconst mvn-pom-file "pom.xml" | |
"POM file name") | |
(defvar mvn-jde-prune '("target" ".svn" "arch" "vendor" "examples" "tests" "test" "samples" "." ".." mvn-pom-file) | |
"Directories to skip while searching") | |
(defun jdb-setup-directory-predicate (file dir) | |
"Filter directories which has `jdb-sourcepath-java-file' files and prune its subdirs." | |
(not | |
(member file jdb-sourcepath-prune))) | |
(defun jdb-setup-file-predicate (file dir) | |
"Filter directories which has `jdb-sourcepath-java-file'" | |
(string= file mvn-pom-file)) | |
(defun jdb-setup () | |
"Find java-sourcepathes within `jdb-sourcepath-root' and write to `jdb-rcfile'" | |
(interactive) | |
(let ((sources | |
(delete-dups | |
(mapcar | |
(lambda (source-dir) | |
(let ((package | |
(jdb-setup-get-package-name | |
(car (directory-files source-dir t jdb-sourcepath-java-file))))) | |
(if package | |
(replace-regexp-in-string | |
(concat "." package) "" source-dir) ; cut package postfix with path delimiter | |
(message (format "error: %s" source-dir))))) | |
(find-lisp-find-files-internal | |
jdb-sourcepath-root | |
'jdb-setup-file-predicate | |
'jdb-setup-directory-predicate))))) | |
(save-excursion | |
(with-current-buffer | |
(find-file-noselect jdb-rcfile) | |
(goto-char (point-min)) | |
(if (re-search-forward "^use " nil t) | |
(delete-region (point) (line-end-position)) | |
(insert-string "use ")) | |
(insert (mapconcat 'identity sources ":")) | |
(save-buffer) | |
(kill-buffer))))) | |
(defun jdb-sourcepath-from-rc () | |
"Get list of sourcepathes from `jdb-rcfile' file" | |
(with-temp-buffer | |
(insert-file-contents-literally jdb-rcfile) | |
(goto-char (point-min)) | |
(if (re-search-forward "^use " nil t) | |
(split-string (buffer-substring (point) (line-end-position)) ":") | |
()))) | |
(provide 'jdb-sourcepath) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(comment | |
RSS feed processing. Fetch link content into summary attribite. | |
) | |
(ns feed-tables.fetch-link | |
(:use | |
[clojure.xml :only (emit parse)] | |
[clojure.contrib.zip-filter.xml :only (xml-> attr)]) | |
(:require [clojure.zip :as zip]) | |
(:import java.net.URL java.lang.StringBuilder | |
[java.io BufferedReader InputStreamReader] | |
[com.google.appengine.api.urlfetch HTTPRequest URLFetchServiceFactory])) | |
(def content-block #"(?s)(<div class=\"post_body\".*?post_body-->)") | |
(def charset-block #"(charset=(.*))") | |
(defn fetch-url-async [address] | |
(let [request (HTTPRequest. (URL. address)) | |
service (URLFetchServiceFactory/getURLFetchService)] | |
(. service fetchAsync request))) | |
(defn process-entry [content entry-loc] | |
(let [entry-loc entry-loc | |
content (last (re-find content-block content))] | |
(zip/append-child entry-loc {:tag :summary, :attrs {:type "html"}, :content (list (str "<![CDATA[ " content "]]>"))}))) | |
(defn xml-apply [loc f] | |
(loop [loc loc] | |
(if (zip/end? loc) | |
(zip/root loc) | |
(recur | |
(zip/next | |
(if (= (:tag (zip/node loc)) :entry) | |
(f loc) | |
loc)))))) | |
(defn get-charset [headers] | |
(let [content-type (first (filter #(. "Content-Type" equalsIgnoreCase (. % getName)) headers))] | |
(last (re-find charset-block (. content-type getValue))))) | |
(defn get-page [pages link] | |
(let [entry (first (filter #(= (:link %) link) pages)) | |
response (. (:content entry) get) | |
content (. response getContent) | |
charset (get-charset (. response getHeaders))] | |
(String. content charset))) | |
(defn process [url] | |
(let [loc (-> url parse zip/xml-zip) | |
base-url (first (xml-> loc (attr :xml:base))) | |
links (xml-> loc :entry :link (attr :href)) | |
pages (map #(array-map :link % :content (fetch-url-async (str base-url %))) links)] | |
(with-out-str | |
(emit (xml-apply loc #(process-entry | |
(get-page pages (first (xml-> % :link (attr :href)))) | |
%)))))) | |
(defn chunked-seq | |
([chunk-size blob-len] | |
(if (> chunk-size blob-len) | |
(list [0 (- blob-len 1)]) | |
(chunked-seq chunk-size blob-len [0 (- chunk-size 1)]))) | |
([chunk-size blob-len range] | |
(let [[start-index end-index] range | |
next-start-index (min blob-len (+ end-index 1)) | |
next-end-index (min blob-len (+ next-start-index (- chunk-size 1)))] | |
(if (= next-end-index blob-len) | |
(lazy-seq (cons range (cons [next-start-index next-end-index] ()))) | |
(lazy-seq (cons range (chunked-seq chunk-size blob-len [next-start-index next-end-index]))))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment