├── .gitignore ├── LICENSE ├── THIRDPARTYLICENSE.txt ├── build.properties.sample ├── build.xml ├── examples ├── adaptor-template-get-doc-contents.sh ├── adaptor-template-get-doc-ids.sh ├── filesystem-get-doc-contents.sh └── filesystem-get-doc-ids.sh ├── lib ├── JUnitLogFixFormatter.java ├── ant-eclipse-jvm1.2-1.0.jar ├── bcprov-jdk15-1.45.jar ├── commons-collections-3.1.jar ├── commons-daemon-1.0.15.jar ├── commons-fileupload-1.3.jar ├── commons-lang-2.1.jar ├── commons-logging-api-1.1.1.jar ├── gdata-core-1.0.jar ├── gdata-gsa-1.0.jar ├── gson-2.0.jar ├── guava-17.0.jar ├── hamcrest-core-1.3.jar ├── joda-time-1.6.jar ├── json_simple-1.1.jar ├── jsr305.jar ├── junit-4.11.jar ├── opencsv-2.3.jar ├── opensaml-2.3.2.jar ├── openws-1.3.1.jar ├── slf4j-api-1.5.10.jar ├── slf4j-jdk14-1.5.10.jar ├── velocity-1.5.jar ├── xercesImpl-2.11.0.jar ├── xml-apis-1.4.1.jar ├── xmlsec-1.4.3.jar └── xmltooling-1.2.2.jar ├── logging.properties ├── resources └── com │ └── google │ └── enterprise │ └── adaptor │ ├── TranslationStrings.properties │ └── resources │ ├── dashboard.css │ ├── dashboard.js │ ├── excanvas.min.js │ ├── index.html │ ├── jqplot.canvasAxisLabelRenderer.min.js │ ├── jqplot.canvasTextRenderer.min.js │ ├── jqplot.dateAxisRenderer.min.js │ ├── jqplot.highlighter.min.js │ ├── jquery-1.6.2.min.js │ ├── jquery.jqplot.min.css │ ├── jquery.jqplot.min.js │ ├── login-failed.html │ ├── login-indeterminate.html │ └── login.html ├── src ├── com │ └── google │ │ └── enterprise │ │ └── adaptor │ │ ├── AbortImmediatelyFilter.java │ │ ├── AbstractAdaptor.java │ │ ├── AbstractDocIdPusher.java │ │ ├── AbstractLazyOutputStream.java │ │ ├── Acl.java │ │ ├── AclTransform.java │ │ ├── Adaptor.java │ │ ├── AdaptorContext.java │ │ ├── AdministratorSecurityHandler.java │ │ ├── Application.java │ │ ├── AsyncDocIdPusher.java │ │ ├── AsyncDocIdSender.java │ │ ├── AuthnAuthority.java │ │ ├── AuthnIdentity.java │ │ ├── AuthnIdentityImpl.java │ │ ├── AuthzAuthority.java │ │ ├── AuthzStatus.java │ │ ├── BlockingQueueBatcher.java │ │ ├── CidrAddress.java │ │ ├── CircularBufferHandler.java │ │ ├── CommandStreamParser.java │ │ ├── Config.java │ │ ├── ConfigModificationEvent.java │ │ ├── ContentTransform.java │ │ ├── ContentTransformFactory.java │ │ ├── CronScheduler.java │ │ ├── CustomFormatter.java │ │ ├── Daemon.java │ │ ├── Dashboard.java │ │ ├── DashboardHandler.java │ │ ├── DocId.java │ │ ├── DocIdCodec.java │ │ ├── DocIdDecoder.java │ │ ├── DocIdEncoder.java │ │ ├── DocIdPusher.java │ │ ├── DocIdSender.java │ │ ├── DocRequest.java │ │ ├── DocumentHandler.java │ │ ├── DocumentTransform.java │ │ ├── DownloadDumpHandler.java │ │ ├── ExceptionHandler.java │ │ ├── ExceptionHandlers.java │ │ ├── FakeMap.java │ │ ├── FastFilterOutputStream.java │ │ ├── FeedArchiver.java │ │ ├── GroupPrincipal.java │ │ ├── GsaCommunicationHandler.java │ │ ├── GsaFeedFileArchiver.java │ │ ├── GsaFeedFileMaker.java │ │ ├── GsaFeedFileSender.java │ │ ├── GsaVersion.java │ │ ├── HashMapSession.java │ │ ├── HeartbeatHandler.java │ │ ├── HttpClientAdapter.java │ │ ├── HttpExchangeInTransportAdapter.java │ │ ├── HttpExchangeOutTransportAdapter.java │ │ ├── HttpExchanges.java │ │ ├── HttpServerScope.java │ │ ├── IOHelper.java │ │ ├── InternalErrorFilter.java │ │ ├── InvalidConfigurationException.java │ │ ├── JavaExec.java │ │ ├── Journal.java │ │ ├── LoggingFilter.java │ │ ├── Metadata.java │ │ ├── MetadataTransform.java │ │ ├── MetadataTransformPipeline.java │ │ ├── NamespacedSession.java │ │ ├── OneAtATimeRunnable.java │ │ ├── PollingIncrementalLister.java │ │ ├── Principal.java │ │ ├── RedirectHandler.java │ │ ├── RelativeTimeProvider.java │ │ ├── Request.java │ │ ├── Response.java │ │ ├── ReverseProxyHandler.java │ │ ├── RpcHandler.java │ │ ├── SamlBatchAuthzHandler.java │ │ ├── SamlIdentityProvider.java │ │ ├── SamlMetadata.java │ │ ├── SamlServiceProvider.java │ │ ├── SecurityManagerConfig.java │ │ ├── SensitiveValueCodec.java │ │ ├── SensitiveValueDecoder.java │ │ ├── Service.java │ │ ├── Session.java │ │ ├── SessionManager.java │ │ ├── ShutdownWaiter.java │ │ ├── SleepHandler.java │ │ ├── StartupException.java │ │ ├── StatRpcMethod.java │ │ ├── Status.java │ │ ├── StatusSource.java │ │ ├── SystemRelativeTimeProvider.java │ │ ├── SystemTimeProvider.java │ │ ├── TimeProvider.java │ │ ├── Translation.java │ │ ├── TranslationStatus.java │ │ ├── UnsupportedPlatformException.java │ │ ├── UserPrincipal.java │ │ ├── Watchdog.java │ │ ├── examples │ │ ├── AdaptorTemplate.java │ │ ├── AdaptorWithCrawlTimeMetadataTemplate.java │ │ ├── AdaptorWithRespondNoContentTemplate.java │ │ ├── AdaptorWithTwoParentOverrides.java │ │ ├── AddContentTypeToMetadata.java │ │ ├── DateSplitTransform.java │ │ ├── DbAdaptorTemplate.java │ │ ├── FileSystemAdaptor.java │ │ ├── GroupDefinitionsFromCsv.java │ │ ├── GroupDefinitionsScaleTester.java │ │ ├── GroupDefinitionsWriter.java │ │ ├── MetadataTransformExample.java │ │ └── helloworldconnector │ │ │ ├── HelloWorldAuthenticator.java │ │ │ ├── HelloWorldConnector.java │ │ │ ├── MetadataAddition.java │ │ │ └── SimpleAuthnIdentity.java │ │ ├── experimental │ │ ├── AclPopulator.java │ │ ├── MetadataExplorer.java │ │ ├── NamedResourceFirehose.java │ │ ├── SharePointStateFileAdaptor.java │ │ ├── Sim.java │ │ └── StresserAdaptor.java │ │ ├── package.html │ │ ├── prebuilt │ │ ├── Command.java │ │ ├── CommandLineAdaptor.java │ │ ├── CommandLineTransform.java │ │ ├── DateFilter.java │ │ ├── FileSystemAdaptor.java │ │ ├── FilterMimetypes.java │ │ ├── PrebuiltTransforms.java │ │ ├── RecursiveFileIterator.java │ │ ├── RegexFilter.java │ │ ├── SkipDocumentFilter.java │ │ └── StreamingCommand.java │ │ ├── secmgr │ │ ├── authncontroller │ │ │ └── ExportedState.java │ │ ├── common │ │ │ ├── AuthzStatus.java │ │ │ ├── SecurityManagerUtil.java │ │ │ ├── Stringify.java │ │ │ └── XmlUtil.java │ │ ├── config │ │ │ └── ConfigSingleton.java │ │ ├── http │ │ │ ├── HttpClientInterface.java │ │ │ └── HttpExchange.java │ │ ├── identity │ │ │ └── Group.java │ │ ├── json │ │ │ ├── GsonTypes.java │ │ │ ├── ProxyTypeAdapter.java │ │ │ ├── TypeAdapters.java │ │ │ └── TypeProxy.java │ │ ├── modules │ │ │ ├── AuthzResult.java │ │ │ └── SamlClient.java │ │ ├── saml │ │ │ ├── AttributeValue.java │ │ │ ├── AttributeValueBuilder.java │ │ │ ├── AttributeValueImpl.java │ │ │ ├── AttributeValueMarshaller.java │ │ │ ├── AttributeValueUnmarshaller.java │ │ │ ├── Group.java │ │ │ ├── GroupBuilder.java │ │ │ ├── GroupImpl.java │ │ │ ├── GroupMarshaller.java │ │ │ ├── GroupUnmarshaller.java │ │ │ ├── HTTPSOAP11MultiContextDecoder.java │ │ │ ├── HTTPSOAP11MultiContextEncoder.java │ │ │ ├── HttpExchangeToInTransport.java │ │ │ ├── HttpExchangeToOutTransport.java │ │ │ ├── OpenSamlUtil.java │ │ │ ├── SamlLogUtil.java │ │ │ ├── SecmgrCredential.java │ │ │ ├── SecmgrCredentialBuilder.java │ │ │ ├── SecmgrCredentialImpl.java │ │ │ ├── SecmgrCredentialMarshaller.java │ │ │ └── SecmgrCredentialUnmarshaller.java │ │ └── servlets │ │ │ └── ResponseParser.java │ │ └── testing │ │ ├── RecordingDocIdPusher.java │ │ ├── RecordingResponse.java │ │ ├── UnsupportedAdaptorContext.java │ │ ├── UnsupportedDocIdPusher.java │ │ ├── UnsupportedRequest.java │ │ └── UnsupportedResponse.java └── overview.html ├── test-resources ├── com │ └── google │ │ └── enterprise │ │ └── adaptor │ │ ├── GsaCommunicationHandlerTest.valid.jks │ │ ├── JavaExecTestChild.jar │ │ └── resources │ │ ├── DashboardHandlerTest.test.css │ │ ├── DashboardHandlerTest.test.html │ │ ├── DashboardHandlerTest.test.js │ │ └── DashboardHandlerTest.test.unknown ├── test-cacerts.jks └── test-keys.jks └── test └── com └── google └── enterprise └── adaptor ├── AbortImmediatelyFilterTest.java ├── AclTest.java ├── AclTransformTest.java ├── AdministratorSecurityHandlerTest.java ├── ApplicationTest.java ├── AsyncDocIdSenderTest.java ├── AuthnIdentityImplTest.java ├── AuthzByAclMockAdaptor.java ├── AuthzByPasswordMockAdaptor.java ├── BlockingQueueBatcherTest.java ├── CidrAddressTest.java ├── CommandStreamParserTest.java ├── ConfigTest.java ├── CronSchedulerTest.java ├── DaemonTest.java ├── DashboardHandlerTest.java ├── DashboardTest.java ├── DocIdCodecTest.java ├── DocIdSenderTest.java ├── DocIdTest.java ├── DocRequestTest.java ├── DocumentHandlerTest.java ├── DownloadDumpHandlerTest.java ├── ExceptionHandlersTest.java ├── FastFilterOutputStreamTest.java ├── GsaCommunicationHandlerTest.java ├── GsaFeedFileArchiverTest.java ├── GsaFeedFileMakerTest.java ├── GsaFeedFileSenderTest.java ├── GsaVersionTest.java ├── HeartbeatHandlerTest.java ├── HttpExchangeInTransportAdapterTest.java ├── HttpExchangeOutTransportAdapterTest.java ├── HttpExchangesTest.java ├── IOHelperTest.java ├── InternalErrorFilterTest.java ├── JavaExecTest.java ├── JournalTest.java ├── LoggingFilterTest.java ├── MetadataTest.java ├── MetadataTransformPipelineTest.java ├── MockAdaptor.java ├── MockConfig.java ├── MockDocIdCodec.java ├── MockFile.java ├── MockHttpClient.java ├── MockHttpContext.java ├── MockHttpExchange.java ├── MockHttpHandler.java ├── MockHttpServer.java ├── MockHttpsContext.java ├── MockHttpsExchange.java ├── MockHttpsServer.java ├── MockJournal.java ├── MockSslSession.java ├── MockStatus.java ├── MockStatusSource.java ├── MockTimeProvider.java ├── MockWatchdog.java ├── NamespacedSessionTest.java ├── OneAtATimeRunnableTest.java ├── PrincipalTest.java ├── PrivateMockAdaptor.java ├── RecordTest.java ├── ReverseProxyHandlerTest.java ├── RpcHandlerTest.java ├── SamlBatchAuthzHandlerTest.java ├── SamlIdentityProviderTest.java ├── SamlServiceProviderTest.java ├── SensitiveValueCodecTest.java ├── SessionManagerTest.java ├── ShutdownWaiterTest.java ├── SleepHandlerTest.java ├── StatRpcMethodTest.java ├── TestHelper.java ├── WatchdogTest.java ├── examples └── MetadataTransformExampleTest.java ├── experimental └── SimTest.java ├── prebuilt ├── CommandLineAdaptorTest.java ├── CommandLineTransformTest.java ├── CommandTest.java ├── DateFilterTest.java ├── FilterMimetypesTest.java ├── PrebuiltTransformsTest.java ├── RecursiveFileIteratorTest.java ├── RegexFilterTest.java ├── SkipDocumentFilterTest.java └── StreamingCommandTest.java └── testing ├── RecordingDocIdPusherTest.java └── RecordingResponseTest.java /.gitignore: -------------------------------------------------------------------------------- 1 | # Normal private files 2 | build/ 3 | dist/ 4 | build.properties 5 | adaptor-config.properties 6 | cacerts.jks 7 | keys.jks 8 | adaptor.crt 9 | gsa.crt 10 | *.class 11 | 12 | # IntelliJ files 13 | workspace/ 14 | classes/ 15 | .idea 16 | Plexi.iml 17 | 18 | # NetBeans files 19 | nbproject/ 20 | -------------------------------------------------------------------------------- /build.properties.sample: -------------------------------------------------------------------------------- 1 | ## 2 | # These properties are specific to the individual's development environment. 3 | # 4 | 5 | # Default JDK 6 installation location for Mac OS X. 6 | # However Macs haven't shipped with Java 6 for years. 7 | jdk6.home = /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home 8 | 9 | # JDK 6 bootclasspath. 10 | build.bootclasspath = ${jdk6.home}/jre/lib/classes.jar:${jdk6.home}/jre/lib/jsse.jar:${jdk6.home}/jre/lib/jce.jar 11 | -------------------------------------------------------------------------------- /examples/adaptor-template-get-doc-contents.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # Example gives the bytes of a document referenced with id. 3 | # 4 | # Shell equivalent of 5 | # com.google.enterprise.adaptor.examples.AdaptorTemplate.getDocContent 6 | 7 | echo -e "GSA Adaptor Data Version 1 [\n]" 8 | if test "5555" = "$1"; then 9 | echo "id=$1" 10 | echo content 11 | echo "This is the body of document 5555 from shell script" 12 | elif test "9999" = "$1"; then 13 | echo "id=$1" 14 | echo content 15 | echo "This is the body of document 9999 from shell script" 16 | else 17 | echo "id=$1" 18 | echo not-found 19 | fi 20 | -------------------------------------------------------------------------------- /examples/adaptor-template-get-doc-ids.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # Example gives list of document ids that you'd like on the GSA. 3 | # 4 | # Shell equivalent of 5 | # com.google.enterprise.adaptor.examples.AdaptorTemplate.getDocIds 6 | 7 | echo -e "GSA Adaptor Data Version 1 [\n]" 8 | echo "id=5555" 9 | echo "id=9999" 10 | -------------------------------------------------------------------------------- /examples/filesystem-get-doc-contents.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # Example gives bytes of file with particular id. 3 | 4 | dir="/tmp/foo-bar/" 5 | id="$1" 6 | fn="$dir/$id" 7 | 8 | echo -e "GSA Adaptor Data Version 1 [\n]" 9 | echo "id=$id" 10 | 11 | if test -f "$fn" && test -r "$fn"; then 12 | echo "content" 13 | cat "$fn" 14 | else 15 | echo not-found 16 | fi 17 | -------------------------------------------------------------------------------- /examples/filesystem-get-doc-ids.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # Example gives list of document ids in a directory. 3 | 4 | dir="/tmp/foo-bar/" 5 | cd "$dir" 6 | 7 | echo -e "GSA Adaptor Data Version 1 [\n]" 8 | echo id-list 9 | # Note: \n delimiter requires no file names contain it. 10 | find . -type f 11 | -------------------------------------------------------------------------------- /lib/ant-eclipse-jvm1.2-1.0.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/googlegsa/library/4a3b24d885b465cb990a7520900f7cdb56d01dd8/lib/ant-eclipse-jvm1.2-1.0.jar -------------------------------------------------------------------------------- /lib/bcprov-jdk15-1.45.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/googlegsa/library/4a3b24d885b465cb990a7520900f7cdb56d01dd8/lib/bcprov-jdk15-1.45.jar -------------------------------------------------------------------------------- /lib/commons-collections-3.1.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/googlegsa/library/4a3b24d885b465cb990a7520900f7cdb56d01dd8/lib/commons-collections-3.1.jar -------------------------------------------------------------------------------- /lib/commons-daemon-1.0.15.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/googlegsa/library/4a3b24d885b465cb990a7520900f7cdb56d01dd8/lib/commons-daemon-1.0.15.jar -------------------------------------------------------------------------------- /lib/commons-fileupload-1.3.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/googlegsa/library/4a3b24d885b465cb990a7520900f7cdb56d01dd8/lib/commons-fileupload-1.3.jar -------------------------------------------------------------------------------- /lib/commons-lang-2.1.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/googlegsa/library/4a3b24d885b465cb990a7520900f7cdb56d01dd8/lib/commons-lang-2.1.jar -------------------------------------------------------------------------------- /lib/commons-logging-api-1.1.1.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/googlegsa/library/4a3b24d885b465cb990a7520900f7cdb56d01dd8/lib/commons-logging-api-1.1.1.jar -------------------------------------------------------------------------------- /lib/gdata-core-1.0.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/googlegsa/library/4a3b24d885b465cb990a7520900f7cdb56d01dd8/lib/gdata-core-1.0.jar -------------------------------------------------------------------------------- /lib/gdata-gsa-1.0.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/googlegsa/library/4a3b24d885b465cb990a7520900f7cdb56d01dd8/lib/gdata-gsa-1.0.jar -------------------------------------------------------------------------------- /lib/gson-2.0.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/googlegsa/library/4a3b24d885b465cb990a7520900f7cdb56d01dd8/lib/gson-2.0.jar -------------------------------------------------------------------------------- /lib/guava-17.0.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/googlegsa/library/4a3b24d885b465cb990a7520900f7cdb56d01dd8/lib/guava-17.0.jar -------------------------------------------------------------------------------- /lib/hamcrest-core-1.3.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/googlegsa/library/4a3b24d885b465cb990a7520900f7cdb56d01dd8/lib/hamcrest-core-1.3.jar -------------------------------------------------------------------------------- /lib/joda-time-1.6.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/googlegsa/library/4a3b24d885b465cb990a7520900f7cdb56d01dd8/lib/joda-time-1.6.jar -------------------------------------------------------------------------------- /lib/json_simple-1.1.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/googlegsa/library/4a3b24d885b465cb990a7520900f7cdb56d01dd8/lib/json_simple-1.1.jar -------------------------------------------------------------------------------- /lib/jsr305.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/googlegsa/library/4a3b24d885b465cb990a7520900f7cdb56d01dd8/lib/jsr305.jar -------------------------------------------------------------------------------- /lib/junit-4.11.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/googlegsa/library/4a3b24d885b465cb990a7520900f7cdb56d01dd8/lib/junit-4.11.jar -------------------------------------------------------------------------------- /lib/opencsv-2.3.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/googlegsa/library/4a3b24d885b465cb990a7520900f7cdb56d01dd8/lib/opencsv-2.3.jar -------------------------------------------------------------------------------- /lib/opensaml-2.3.2.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/googlegsa/library/4a3b24d885b465cb990a7520900f7cdb56d01dd8/lib/opensaml-2.3.2.jar -------------------------------------------------------------------------------- /lib/openws-1.3.1.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/googlegsa/library/4a3b24d885b465cb990a7520900f7cdb56d01dd8/lib/openws-1.3.1.jar -------------------------------------------------------------------------------- /lib/slf4j-api-1.5.10.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/googlegsa/library/4a3b24d885b465cb990a7520900f7cdb56d01dd8/lib/slf4j-api-1.5.10.jar -------------------------------------------------------------------------------- /lib/slf4j-jdk14-1.5.10.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/googlegsa/library/4a3b24d885b465cb990a7520900f7cdb56d01dd8/lib/slf4j-jdk14-1.5.10.jar -------------------------------------------------------------------------------- /lib/velocity-1.5.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/googlegsa/library/4a3b24d885b465cb990a7520900f7cdb56d01dd8/lib/velocity-1.5.jar -------------------------------------------------------------------------------- /lib/xercesImpl-2.11.0.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/googlegsa/library/4a3b24d885b465cb990a7520900f7cdb56d01dd8/lib/xercesImpl-2.11.0.jar -------------------------------------------------------------------------------- /lib/xml-apis-1.4.1.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/googlegsa/library/4a3b24d885b465cb990a7520900f7cdb56d01dd8/lib/xml-apis-1.4.1.jar -------------------------------------------------------------------------------- /lib/xmlsec-1.4.3.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/googlegsa/library/4a3b24d885b465cb990a7520900f7cdb56d01dd8/lib/xmlsec-1.4.3.jar -------------------------------------------------------------------------------- /lib/xmltooling-1.2.2.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/googlegsa/library/4a3b24d885b465cb990a7520900f7cdb56d01dd8/lib/xmltooling-1.2.2.jar -------------------------------------------------------------------------------- /logging.properties: -------------------------------------------------------------------------------- 1 | .level = INFO 2 | com.google.enterprise.adaptor.level = ALL 3 | 4 | handlers = java.util.logging.ConsoleHandler 5 | #handlers = java.util.logging.ConsoleHandler,java.util.logging.FileHandler 6 | 7 | java.util.logging.ConsoleHandler.level = FINEST 8 | java.util.logging.ConsoleHandler.formatter = com.google.enterprise.adaptor.CustomFormatter 9 | # If your terminal can't handle colors and auto-detection fails, then use 10 | # com.google.enterprise.adaptor.CustomFormatter$NoColor 11 | 12 | java.util.logging.FileHandler.formatter=com.google.enterprise.adaptor.CustomFormatter$NoColor 13 | java.util.logging.FileHandler.pattern=logs/adaptor.%g.log 14 | java.util.logging.FileHandler.limit=10485760 15 | java.util.logging.FileHandler.count=5 16 | -------------------------------------------------------------------------------- /resources/com/google/enterprise/adaptor/resources/jquery.jqplot.min.css: -------------------------------------------------------------------------------- 1 | .jqplot-target{position:relative;color:#666;font-family:"Trebuchet MS",Arial,Helvetica,sans-serif;font-size:1em;}.jqplot-axis{font-size:.75em;}.jqplot-xaxis{margin-top:10px;}.jqplot-x2axis{margin-bottom:10px;}.jqplot-yaxis{margin-right:10px;}.jqplot-y2axis,.jqplot-y3axis,.jqplot-y4axis,.jqplot-y5axis,.jqplot-y6axis,.jqplot-y7axis,.jqplot-y8axis,.jqplot-y9axis{margin-left:10px;margin-right:10px;}.jqplot-axis-tick,.jqplot-xaxis-tick,.jqplot-yaxis-tick,.jqplot-x2axis-tick,.jqplot-y2axis-tick,.jqplot-y3axis-tick,.jqplot-y4axis-tick,.jqplot-y5axis-tick,.jqplot-y6axis-tick,.jqplot-y7axis-tick,.jqplot-y8axis-tick,.jqplot-y9axis-tick{position:absolute;}.jqplot-xaxis-tick{top:0;left:15px;vertical-align:top;}.jqplot-x2axis-tick{bottom:0;left:15px;vertical-align:bottom;}.jqplot-yaxis-tick{right:0;top:15px;text-align:right;}.jqplot-y2axis-tick,.jqplot-y3axis-tick,.jqplot-y4axis-tick,.jqplot-y5axis-tick,.jqplot-y6axis-tick,.jqplot-y7axis-tick,.jqplot-y8axis-tick,.jqplot-y9axis-tick{left:0;top:15px;text-align:left;}.jqplot-xaxis-label{margin-top:10px;font-size:11pt;position:absolute;}.jqplot-x2axis-label{margin-bottom:10px;font-size:11pt;position:absolute;}.jqplot-yaxis-label{margin-right:10px;font-size:11pt;position:absolute;}.jqplot-y2axis-label,.jqplot-y3axis-label,.jqplot-y4axis-label,.jqplot-y5axis-label,.jqplot-y6axis-label,.jqplot-y7axis-label,.jqplot-y8axis-label,.jqplot-y9axis-label{font-size:11pt;position:absolute;}table.jqplot-table-legend,table.jqplot-cursor-legend{background-color:rgba(255,255,255,0.6);border:1px solid #ccc;position:absolute;font-size:.75em;}td.jqplot-table-legend{vertical-align:middle;}td.jqplot-table-legend>div{border:1px solid #ccc;padding:.2em;}div.jqplot-table-legend-swatch{width:0;height:0;border-top-width:.35em;border-bottom-width:.35em;border-left-width:.6em;border-right-width:.6em;border-top-style:solid;border-bottom-style:solid;border-left-style:solid;border-right-style:solid;}.jqplot-title{top:0;left:0;padding-bottom:.5em;font-size:1.2em;}table.jqplot-cursor-tooltip{border:1px solid #ccc;font-size:.75em;}.jqplot-cursor-tooltip{border:1px solid #ccc;font-size:.75em;white-space:nowrap;background:rgba(208,208,208,0.5);padding:1px;}.jqplot-highlighter-tooltip{border:1px solid #ccc;font-size:.75em;white-space:nowrap;background:rgba(208,208,208,0.5);padding:1px;}.jqplot-point-label{font-size:.75em;}td.jqplot-cursor-legend-swatch{vertical-align:middle;text-align:center;}div.jqplot-cursor-legend-swatch{width:1.2em;height:.7em;} -------------------------------------------------------------------------------- /src/com/google/enterprise/adaptor/AbortImmediatelyFilter.java: -------------------------------------------------------------------------------- 1 | // Copyright 2013 Google Inc. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor; 16 | 17 | import com.sun.net.httpserver.Filter; 18 | import com.sun.net.httpserver.HttpExchange; 19 | 20 | import java.io.IOException; 21 | 22 | /** Filter that aborts the request when server is under high load. */ 23 | class AbortImmediatelyFilter extends Filter { 24 | @Override 25 | public String description() { 26 | return "Filter that aborts the request when server is under high load"; 27 | } 28 | 29 | @Override 30 | public void doFilter(HttpExchange ex, Filter.Chain chain) throws IOException { 31 | // Checking abortImmediately is part of a hack to immediately reject clients 32 | // when the work queue grows too long. 33 | if (HttpExchanges.abortImmediately.get() != null) { 34 | throw new IOException( 35 | "Aborting request because server is under high load"); 36 | } 37 | chain.doFilter(ex); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/com/google/enterprise/adaptor/AbstractAdaptor.java: -------------------------------------------------------------------------------- 1 | // Copyright 2011 Google Inc. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor; 16 | 17 | import java.util.logging.Logger; 18 | 19 | /** 20 | * Provides a reasonable default implementation for most {@link Adaptor} 21 | * methods. Extending classes only need to implement {@link Adaptor#getDocIds} 22 | * and {@link Adaptor#getDocContent}. 23 | */ 24 | public abstract class AbstractAdaptor implements Adaptor { 25 | private static final Logger log 26 | = Logger.getLogger(AbstractAdaptor.class.getName()); 27 | 28 | /** 29 | * {@inheritDoc} 30 | * 31 | *

This implementation does nothing. 32 | */ 33 | @Override 34 | public void initConfig(Config config) {} 35 | 36 | /** 37 | * {@inheritDoc} 38 | * 39 | *

This implementation does nothing. 40 | */ 41 | @Override 42 | public void init(AdaptorContext context) throws Exception {} 43 | 44 | /** 45 | * {@inheritDoc} 46 | * 47 | *

This implementation does nothing. 48 | */ 49 | @Override 50 | public void destroy() {} 51 | 52 | /** 53 | * Standard main for all adaptors (including those not extending 54 | * AbstractAdaptor). 55 | * 56 | *

This method starts the HTTP server for serving doc contents, schedules 57 | * sending {@code DocId}s on a schedule, and optionally sends {@code DocId}s 58 | * on startup. 59 | * @param adaptor to be initialized 60 | * @param args command line params passed to config 61 | * @return the application instance in use 62 | */ 63 | public static Application main(Adaptor adaptor, String[] args) { 64 | return Application.main(adaptor, args); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/com/google/enterprise/adaptor/AbstractDocIdPusher.java: -------------------------------------------------------------------------------- 1 | // Copyright 2011 Google Inc. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor; 16 | 17 | import static com.google.enterprise.adaptor.DocIdPusher.FeedType.INCREMENTAL; 18 | 19 | import java.util.ArrayList; 20 | import java.util.Collection; 21 | import java.util.List; 22 | import java.util.Map; 23 | 24 | /** 25 | * This class provides an implementation of the forwarding methods of 26 | * the {@link DocIdPusher} interface. 27 | */ 28 | public abstract class AbstractDocIdPusher implements DocIdPusher { 29 | /** {@inheritDoc} */ 30 | @Override 31 | public DocId pushDocIds(Iterable docIds) 32 | throws InterruptedException { 33 | return pushDocIds(docIds, null); 34 | } 35 | 36 | /** {@inheritDoc} */ 37 | @Override 38 | public DocId pushDocIds(Iterable docIds, 39 | ExceptionHandler handler) 40 | throws InterruptedException { 41 | List records = new ArrayList(); 42 | for (DocId docId : docIds) { 43 | records.add(new Record.Builder(docId).build()); 44 | } 45 | Record record = pushRecords(records, handler); 46 | return record == null ? null : record.getDocId(); 47 | } 48 | 49 | /** {@inheritDoc} */ 50 | @Override 51 | public Record pushRecords(Iterable records) 52 | throws InterruptedException { 53 | return pushRecords(records, null); 54 | } 55 | 56 | /** {@inheritDoc} */ 57 | @Override 58 | public DocId pushNamedResources(Map resources) 59 | throws InterruptedException { 60 | return pushNamedResources(resources, null); 61 | } 62 | 63 | /** {@inheritDoc} */ 64 | @Override 65 | public GroupPrincipal pushGroupDefinitions( 66 | Map> defs, 67 | boolean caseSensitive) throws InterruptedException { 68 | return pushGroupDefinitions(defs, caseSensitive, INCREMENTAL, null, null); 69 | } 70 | 71 | /** {@inheritDoc} */ 72 | @Override 73 | public GroupPrincipal pushGroupDefinitions( 74 | Map> defs, 75 | boolean caseSensitive, ExceptionHandler handler) 76 | throws InterruptedException { 77 | return pushGroupDefinitions(defs, caseSensitive, INCREMENTAL, null, 78 | handler); 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /src/com/google/enterprise/adaptor/AbstractLazyOutputStream.java: -------------------------------------------------------------------------------- 1 | // Copyright 2011 Google Inc. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor; 16 | 17 | import java.io.IOException; 18 | import java.io.OutputStream; 19 | 20 | /** 21 | * OutputStream that passes all calls to the {@code OutputStream} provided by 22 | * {@link #retrieveOs}, but calls {@code retrieveOs()} only once needed. 23 | */ 24 | abstract class AbstractLazyOutputStream extends OutputStream { 25 | private OutputStream os; 26 | 27 | @Override 28 | public void close() throws IOException { 29 | loadOs(); 30 | os.close(); 31 | } 32 | 33 | @Override 34 | public void flush() throws IOException { 35 | loadOs(); 36 | os.flush(); 37 | } 38 | 39 | @Override 40 | public void write(byte[] b, int off, int len) throws IOException { 41 | loadOs(); 42 | os.write(b, off, len); 43 | } 44 | 45 | @Override 46 | public void write(byte[] b) throws IOException { 47 | write(b, 0, b.length); 48 | } 49 | 50 | @Override 51 | public void write(int b) throws IOException { 52 | loadOs(); 53 | os.write(b); 54 | } 55 | 56 | protected void loadOs() throws IOException { 57 | if (os == null) { 58 | os = retrieveOs(); 59 | } 60 | } 61 | 62 | /** 63 | * Retrieve the real {@code OutputStream}. Will only be called once. 64 | */ 65 | protected abstract OutputStream retrieveOs() throws IOException; 66 | } 67 | -------------------------------------------------------------------------------- /src/com/google/enterprise/adaptor/AsyncDocIdPusher.java: -------------------------------------------------------------------------------- 1 | // Copyright 2014 Google Inc. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor; 16 | 17 | /** 18 | * Interface that allows asynchronous at-will pushing of {@code DocId}s 19 | * to the GSA. 20 | */ 21 | public interface AsyncDocIdPusher { 22 | /** 23 | * Push a {@code DocId} asynchronously to the GSA. The {@code DocId} is 24 | * enqueued and sent in the next batch to the GSA. If the queue is full, 25 | * then the item will be dropped and a warning will be logged. 26 | * 27 | * @param docId that is to be crawled 28 | * @return {@code true} if the DocId was accepted, {@code false} otherwise 29 | */ 30 | public boolean pushDocId(DocId docId); 31 | 32 | /** 33 | * Push a {@code Record} asynchronously to the GSA. The {@code Record} 34 | * is enqueued and sent in the next batch to the GSA. If the queue is full, 35 | * then the item will be dropped and a warning will be logged. 36 | * 37 | * @param record that is to be crawled 38 | * @return {@code true} if the Record was accepted, {@code false} otherwise 39 | */ 40 | public boolean pushRecord(DocIdPusher.Record record); 41 | 42 | /** 43 | * Push a named resource asynchronously to the GSA. The named resource is 44 | * enqueued and sent in the next batch to the GSA. If the queue is full, 45 | * then the item will be dropped and a warning will be logged. 46 | * 47 | *

Named resources are {@code DocId}s without any content or metadata, 48 | * that only exist for ACL inheritance. These {@code DocId} will never be 49 | * visible to the user and have no meaning outside of ACL processing. 50 | * 51 | * @param docId name or label of the access control list 52 | * @param acl is the access control list mapped onto by docId 53 | * @return {@code true} if the NamedResource was accepted, {@code false} 54 | * otherwise 55 | */ 56 | public boolean pushNamedResource(DocId docId, Acl acl); 57 | } 58 | -------------------------------------------------------------------------------- /src/com/google/enterprise/adaptor/AuthnIdentity.java: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Google Inc. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor; 16 | 17 | import java.util.Set; 18 | 19 | /** 20 | * User identification information for understanding who a user is or if they 21 | * are allowed to access a resource. 22 | */ 23 | public interface AuthnIdentity { 24 | /** 25 | * Gets the user principal. This value will always be available. 26 | * 27 | * @return the user's identifier. 28 | */ 29 | public UserPrincipal getUser(); 30 | 31 | /** 32 | * Gets the user's password. 33 | * 34 | * @return the user's password, or {@code null} if it is unavailable. 35 | */ 36 | public String getPassword(); 37 | 38 | /** 39 | * Gets all the groups a user belongs to in an immutable set. 40 | * 41 | * @return the user's groups, or {@code null} if they are unavailable. 42 | */ 43 | public Set getGroups(); 44 | } 45 | -------------------------------------------------------------------------------- /src/com/google/enterprise/adaptor/AuthnIdentityImpl.java: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Google Inc. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor; 16 | 17 | import java.util.Collections; 18 | import java.util.HashSet; 19 | import java.util.Set; 20 | 21 | /** 22 | * Immutable implementation of {@link AuthnIdentity}. 23 | */ 24 | class AuthnIdentityImpl implements AuthnIdentity { 25 | private final UserPrincipal user; 26 | private final String password; 27 | private final Set groups; 28 | 29 | private AuthnIdentityImpl(UserPrincipal user, String password, 30 | Set groups) { 31 | this.user = user; 32 | this.password = password; 33 | this.groups = groups; 34 | } 35 | 36 | /** 37 | * {@inheritDoc} 38 | */ 39 | @Override 40 | public UserPrincipal getUser() { 41 | return user; 42 | } 43 | 44 | /** 45 | * {@inheritDoc} 46 | */ 47 | @Override 48 | public String getPassword() { 49 | return password; 50 | } 51 | 52 | /** 53 | * {@inheritDoc} 54 | */ 55 | @Override 56 | public Set getGroups() { 57 | return groups; 58 | } 59 | 60 | @Override 61 | public String toString() { 62 | return "AuthnIdentity(" + user + "," 63 | + (password == null ? "no password" : "contains password") + "," 64 | + "groups=" + groups + ")"; 65 | } 66 | 67 | /** 68 | * Builder for creating {@link AuthnIdentityImpl} instances. 69 | */ 70 | public static class Builder { 71 | private UserPrincipal user; 72 | private String password; 73 | private Set groups; 74 | 75 | /** 76 | * Construct new builder. All values are initialized to {@code null}, except 77 | * for user. 78 | * 79 | * @param user non-{@code null} user 80 | */ 81 | public Builder(UserPrincipal user) { 82 | setUser(user); 83 | } 84 | 85 | public AuthnIdentityImpl build() { 86 | return new AuthnIdentityImpl(user, password, groups); 87 | } 88 | 89 | public Builder setUser(UserPrincipal user) { 90 | if (user == null) { 91 | throw new NullPointerException(); 92 | } 93 | this.user = user; 94 | return this; 95 | } 96 | 97 | public Builder setPassword(String password) { 98 | this.password = password; 99 | return this; 100 | } 101 | 102 | public Builder setGroups(Set groups) { 103 | if (groups == null) { 104 | this.groups = null; 105 | return this; 106 | } 107 | this.groups = Collections.unmodifiableSet( 108 | new HashSet(groups)); 109 | return this; 110 | } 111 | } 112 | } 113 | -------------------------------------------------------------------------------- /src/com/google/enterprise/adaptor/AuthzAuthority.java: -------------------------------------------------------------------------------- 1 | // Copyright 2013 Google Inc. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor; 16 | 17 | import java.io.IOException; 18 | import java.util.Collection; 19 | import java.util.Map; 20 | 21 | /** 22 | * Interface for adaptors capable of authorizing users. 23 | * 24 | *

Instances of this interface are typically registered with {@link 25 | * AdaptorContext#setAuthzAuthority}. 26 | */ 27 | public interface AuthzAuthority { 28 | /** 29 | * Determines whether the user identified is allowed to access the {@code 30 | * DocId}s. The user is either anonymous or assumed to be previously 31 | * authenticated. If an anonymous user is denied access to a document, then 32 | * the caller may prompt the user to go through an authentication process and 33 | * then try again. 34 | * 35 | *

Returns {@link AuthzStatus#PERMIT} for {@link DocId}s the user is 36 | * allowed to access. Retutrns {@link AuthzStatus#DENY} for {@code DocId}s the 37 | * user is not allowed to access. If the document exists, {@link 38 | * AuthzStatus#INDETERMINATE} will not be returned for that {@code DocId}. 39 | * 40 | *

If the document doesn't exist, then there are several possibilities. If 41 | * the repository is fully-public then it will return {@code PERMIT}. This 42 | * will allow the caller to provide a cached version of the file to the user 43 | * or call {@link Adaptor#getDocContent} which should call {@link 44 | * Response#respondNotFound}. If the adaptor is not sensitive to users knowing 45 | * that certain documents do not exist, then it will return {@code 46 | * INDETERMINATE}. This will be interpreted as the document does not exist; no 47 | * cached copy will be provided to the user but the user may be informed the 48 | * document doesn't exist. Highly sensitive repositories may return {@code 49 | * DENY}. 50 | * 51 | *

If you experience a fatal error, feel free to throw an {@link 52 | * IOException} or {@link RuntimeException}. In the case of an error, the 53 | * users will be denied access to the resources. 54 | * 55 | * @param userIdentity user to authorize, or {@code null} for anonymous 56 | * users 57 | * @param ids Collection of {@code DocId}s that need to be checked 58 | * @return an {@code AuthzStatus} for each {@code DocId} provided in {@code 59 | * ids} 60 | * @throws IOException on failure 61 | */ 62 | public Map isUserAuthorized(AuthnIdentity userIdentity, 63 | Collection ids) throws IOException; 64 | } 65 | -------------------------------------------------------------------------------- /src/com/google/enterprise/adaptor/AuthzStatus.java: -------------------------------------------------------------------------------- 1 | // Copyright 2011 Google Inc. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor; 16 | 17 | /** 18 | * Authorization status codes. 19 | */ 20 | public enum AuthzStatus { 21 | /** The authorization is granted. */ 22 | PERMIT("Access PERMITTED"), 23 | /** The authorization is explicitly forbidden. */ 24 | DENY("Access DENIED"), 25 | /** 26 | * Permission is neither granted nor forbidden. If a consumer receives this 27 | * code it may decide to try other means to get an explicit decision (i.e., 28 | * {@code PERMIT} or {@code DENY}). 29 | */ 30 | INDETERMINATE("No access decision"); 31 | 32 | private final String description; 33 | 34 | private AuthzStatus(String description) { 35 | this.description = description; 36 | } 37 | 38 | /** @return String description of decision */ 39 | public String getDescription() { 40 | return description; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/com/google/enterprise/adaptor/CircularBufferHandler.java: -------------------------------------------------------------------------------- 1 | // Copyright 2011 Google Inc. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor; 16 | 17 | import java.util.logging.Formatter; 18 | import java.util.logging.Handler; 19 | import java.util.logging.LogRecord; 20 | import java.util.logging.SimpleFormatter; 21 | 22 | /** 23 | * Logging Handler that keeps a circular buffer of recent log messages for later 24 | * outputting to a stream or other Handler. It does not clear the buffer after 25 | * outputting the messages. This class is thread-safe. 26 | */ 27 | class CircularBufferHandler extends Handler { 28 | private static final int DEFAULT_SIZE = 1000; 29 | private LogRecord[] buffer; 30 | private int head, tail; 31 | 32 | public CircularBufferHandler() { 33 | this(DEFAULT_SIZE); 34 | } 35 | 36 | public CircularBufferHandler(int size) { 37 | buffer = new LogRecord[size]; 38 | } 39 | 40 | @Override 41 | public synchronized void flush() {} 42 | 43 | @Override 44 | public synchronized void close() { 45 | buffer = null; 46 | } 47 | 48 | @Override 49 | public synchronized void publish(LogRecord record) { 50 | if (!isLoggable(record)) { 51 | return; 52 | } 53 | buffer[tail] = record; 54 | tail = (tail + 1) % buffer.length; 55 | if (head == tail) { 56 | head = (head + 1) % buffer.length; 57 | } 58 | } 59 | 60 | public String writeOut() { 61 | return writeOut(new SimpleFormatter()); 62 | } 63 | 64 | public synchronized String writeOut(Formatter formatter) { 65 | StringBuilder sb = new StringBuilder(); 66 | for (int i = head; i != tail; i = (i + 1) % buffer.length) { 67 | sb.append(formatter.format(buffer[i])); 68 | } 69 | return sb.toString(); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/com/google/enterprise/adaptor/ConfigModificationEvent.java: -------------------------------------------------------------------------------- 1 | // Copyright 2011 Google Inc. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor; 16 | 17 | import java.util.Collections; 18 | import java.util.EventObject; 19 | import java.util.Set; 20 | 21 | /** 22 | * Represents configuration modification event information. 23 | */ 24 | class ConfigModificationEvent extends EventObject { 25 | protected Config oldConfig; 26 | protected Set modifiedKeys; 27 | 28 | public ConfigModificationEvent(Config source, Config oldConfig, 29 | Set modifiedKeys) { 30 | super(source); 31 | this.oldConfig = oldConfig; 32 | this.modifiedKeys = Collections.unmodifiableSet(modifiedKeys); 33 | } 34 | 35 | public Config getNewConfig() { 36 | return (Config) source; 37 | } 38 | 39 | public Config getOldConfig() { 40 | return oldConfig; 41 | } 42 | 43 | /** 44 | * Keys whose values were changed. 45 | */ 46 | public Set getModifiedKeys() { 47 | return modifiedKeys; 48 | } 49 | 50 | @Override 51 | public String toString() { 52 | return "ConfigModificationEvent(modifiedKeys: " + modifiedKeys + ")"; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/com/google/enterprise/adaptor/DocId.java: -------------------------------------------------------------------------------- 1 | // Copyright 2011 Google Inc. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor; 16 | 17 | /** 18 | * Refers to a unique document in repository. It is a thin wrapper of {@link 19 | * String} that adds meaning to the String and prevents the string from being 20 | * confused with others that do not refer to documents. 21 | * 22 | *

You provide a {@code DocId} to methods like {@link DocIdPusher#pushDocIds} 23 | * to inform the GSA about the document's existance, so that it can crawl and 24 | * index it. When the GSA requests that document's contents, {@link 25 | * Request#getDocId} will have the same unique id as {@code DocId} you provided. 26 | * However, just because a {@code DocId} is provided, does not mean the value it 27 | * represents exists or ever existed. The GSA can query for documents that have 28 | * been deleted and users can query for documents that never existed. 29 | */ 30 | public class DocId implements Comparable { 31 | private final String uniqId; // Not null. 32 | 33 | /** 34 | * Construct an identifier based on {@code id}. 35 | * 36 | * @param id non-{@code null} document identifier 37 | */ 38 | public DocId(String id) { 39 | if (id == null) { 40 | throw new NullPointerException(); 41 | } 42 | this.uniqId = id; 43 | } 44 | 45 | /** 46 | * Returns the string identifier provided to the constructor. 47 | * @return String id 48 | */ 49 | public String getUniqueId() { 50 | return uniqId; 51 | } 52 | 53 | /** Generates a string useful for debugging that contains the unique id. */ 54 | @Override 55 | public String toString() { 56 | return "DocId(" + uniqId + ")"; 57 | } 58 | 59 | /** 60 | * Determines equality based on the unique id string. 61 | */ 62 | @Override 63 | public boolean equals(Object o) { 64 | if (null == o || !getClass().equals(o.getClass())) { 65 | return false; 66 | } 67 | DocId d = (DocId) o; 68 | return this.uniqId.equals(d.uniqId); 69 | } 70 | 71 | /** 72 | * Generates a hash code based on the unique id string. 73 | */ 74 | @Override 75 | public int hashCode() { 76 | return this.uniqId.hashCode(); 77 | } 78 | 79 | /** 80 | * Provides comparison for ids based on the unique id string. 81 | */ 82 | @Override 83 | public int compareTo(DocId docId) { 84 | return uniqId.compareTo(docId.uniqId); 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /src/com/google/enterprise/adaptor/DocIdDecoder.java: -------------------------------------------------------------------------------- 1 | // Copyright 2011 Google Inc. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor; 16 | 17 | import java.net.URI; 18 | 19 | /** 20 | * Allows converting from a URI to a unvalidated DocId. 21 | * 22 | * @see DocIdEncoder 23 | */ 24 | interface DocIdDecoder { 25 | /** 26 | * Extract the DocId from the provided URI. Note that the DocId is not 27 | * guaranteed to have been created by the adaptor library; they can easily be 28 | * faked. 29 | */ 30 | public DocId decodeDocId(URI uri); 31 | } 32 | -------------------------------------------------------------------------------- /src/com/google/enterprise/adaptor/DocIdEncoder.java: -------------------------------------------------------------------------------- 1 | // Copyright 2011 Google Inc. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor; 16 | 17 | import java.net.URI; 18 | 19 | /** 20 | * Allows generating a URI for a particular DocId. 21 | * 22 | * @see DocIdDecoder 23 | */ 24 | public interface DocIdEncoder { 25 | /** 26 | * @param docId to be encoded 27 | * @return URI encoding of provided DocId 28 | */ 29 | public URI encodeDocId(DocId docId); 30 | } 31 | -------------------------------------------------------------------------------- /src/com/google/enterprise/adaptor/DocumentTransform.java: -------------------------------------------------------------------------------- 1 | // Copyright 2011 Google Inc. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor; 16 | 17 | import java.util.Map; 18 | 19 | /** 20 | * Backward-compatibility name for MetadataTransform. 21 | */ 22 | @Deprecated 23 | public interface DocumentTransform { 24 | 25 | @Deprecated 26 | public void transform(Metadata metadata, Map params); 27 | } 28 | -------------------------------------------------------------------------------- /src/com/google/enterprise/adaptor/ExceptionHandler.java: -------------------------------------------------------------------------------- 1 | // Copyright 2011 Google Inc. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor; 16 | 17 | /** 18 | * Interface for handling errors and handling retrying policy. 19 | */ 20 | public interface ExceptionHandler { 21 | /** 22 | * Determine how to proceed after an exception was thrown. The thrown 23 | * exception is provided as well as the number of times the call has been 24 | * attempted. It is fine to call {@code Thread.sleep()} before returning. 25 | * 26 | * @param ex is describing a problem 27 | * @param ntries is number of consecutive failure for same operation 28 | * @return {@code true} for immediate retry, {@code false} to abort 29 | * @throws InterruptedException if backoff throws it 30 | */ 31 | public boolean handleException(Exception ex, int ntries) 32 | throws InterruptedException; 33 | } 34 | -------------------------------------------------------------------------------- /src/com/google/enterprise/adaptor/FakeMap.java: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Google Inc. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor; 16 | 17 | import java.util.AbstractMap; 18 | import java.util.Collections; 19 | import java.util.Map; 20 | import java.util.Set; 21 | 22 | /** 23 | * Fake modifiable map that doesn't store anything. 24 | */ 25 | class FakeMap extends AbstractMap { 26 | @Override 27 | public Set> entrySet() { 28 | return Collections.emptySet(); 29 | } 30 | 31 | @Override 32 | public V put(K key, V value) { 33 | return null; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/com/google/enterprise/adaptor/FastFilterOutputStream.java: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Google Inc. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor; 16 | 17 | import java.io.IOException; 18 | import java.io.OutputStream; 19 | 20 | /** 21 | * {@link FilterOutputStream} replacement that uses {@link 22 | * #write(byte[],int,int)} for all writes. This class is not thread-safe. 23 | */ 24 | class FastFilterOutputStream extends OutputStream { 25 | private byte[] singleByte = new byte[1]; 26 | // Protected to mimic FilterOutputStream. 27 | protected OutputStream out; 28 | 29 | /** 30 | * Construct instance with {@code out = null}. Extending class must ensure 31 | * that {@code out} is handled correctly. 32 | */ 33 | protected FastFilterOutputStream() {} 34 | 35 | /** 36 | * Construct instance with provided {@code out}. {@code out} is not permitted 37 | * to be {@code null} via this constructor. 38 | */ 39 | public FastFilterOutputStream(OutputStream out) { 40 | if (out == null) { 41 | throw new NullPointerException(); 42 | } 43 | this.out = out; 44 | } 45 | 46 | /** 47 | * Calls {@code out.close()}. 48 | */ 49 | @Override 50 | public void close() throws IOException { 51 | out.close(); 52 | } 53 | 54 | /** 55 | * Calls {@code out.flush()}. 56 | */ 57 | @Override 58 | public void flush() throws IOException { 59 | out.flush(); 60 | } 61 | 62 | /** 63 | * Calls {@code out.write(b, off, len)}. 64 | */ 65 | @Override 66 | public void write(byte[] b, int off, int len) throws IOException { 67 | out.write(b, off, len); 68 | } 69 | 70 | /** 71 | * Calls {@link #write(byte[],int,int)}. This does not call {@code out}'s 72 | * write, but instead this class's write. There is no reason to override this 73 | * method. 74 | */ 75 | @Override 76 | public void write(byte[] b) throws IOException { 77 | write(b, 0, b.length); 78 | } 79 | 80 | /** 81 | * Calls {@link #write(byte[],int,int)}. This does not call {@code out}'s 82 | * write, but instead this class's write. There is no reason to override this 83 | * method. 84 | */ 85 | @Override 86 | public void write(int b) throws IOException { 87 | singleByte[0] = (byte) b; 88 | write(singleByte, 0, 1); 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /src/com/google/enterprise/adaptor/FeedArchiver.java: -------------------------------------------------------------------------------- 1 | // Copyright 2014 Google Inc. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor; 16 | 17 | /** 18 | * Takes an XML feed destined for the GSA and makes a copy in the 19 | * configured feed archive directory. The feed archive directory is 20 | * specified using the {@code feed.archiveDirectory} configuration property. 21 | */ 22 | interface FeedArchiver { 23 | /** 24 | * Save the supplied XML string as a file in the feed archive 25 | * directory. The file's name will start with the feed name 26 | * and have a {@code .xml} extension. 27 | * 28 | * @param feedName the name of the feed or datasource 29 | * @param feedXml the XML string that will be saved 30 | */ 31 | public void saveFeed(String feedName, String feedXml); 32 | 33 | /** 34 | * Save the supplied XML string as a file in the feed archive 35 | * directory. The file's name will start with {@code FAILED-}, 36 | * followed by the feed name, and have a {@code .xml} extension. 37 | * 38 | * @param feedName the name of the feed or datasource 39 | * @param feedXml the XML string that will be saved 40 | */ 41 | public void saveFailedFeed(String feedName, String feedXml); 42 | } 43 | -------------------------------------------------------------------------------- /src/com/google/enterprise/adaptor/GroupPrincipal.java: -------------------------------------------------------------------------------- 1 | // Copyright 2013 Google Inc. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor; 16 | 17 | import java.util.Collection; 18 | import java.util.Set; 19 | import java.util.TreeSet; 20 | 21 | /** 22 | * Represents group. 23 | */ 24 | public final class GroupPrincipal extends Principal { 25 | public GroupPrincipal(String name, String namespace) { 26 | super(name, namespace); 27 | } 28 | 29 | public GroupPrincipal(String name) { 30 | super(name); 31 | } 32 | 33 | /** Always returns {@code false}. */ 34 | @Override 35 | public boolean isUser() { 36 | return false; 37 | } 38 | 39 | /** Always returns {@code true}. */ 40 | @Override 41 | public boolean isGroup() { 42 | return true; 43 | } 44 | 45 | static Set makeSet(Collection names) { 46 | if (null == names) { 47 | return null; 48 | } 49 | Set groups = new TreeSet(); 50 | for (String n : names) { 51 | groups.add(new GroupPrincipal(n)); 52 | } 53 | return groups; 54 | } 55 | 56 | static Set makeSet(Collection names, 57 | String namespace) { 58 | if (null == names) { 59 | return null; 60 | } 61 | Set groups = new TreeSet(); 62 | for (String n : names) { 63 | groups.add(new GroupPrincipal(n, namespace)); 64 | } 65 | return groups; 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/com/google/enterprise/adaptor/GsaFeedFileArchiver.java: -------------------------------------------------------------------------------- 1 | // Copyright 2014 Google Inc. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor; 16 | 17 | import com.google.common.base.Strings; 18 | import com.google.common.io.Files; 19 | 20 | import java.io.File; 21 | import java.io.IOException; 22 | import java.nio.charset.Charset; 23 | import java.util.logging.Level; 24 | import java.util.logging.Logger; 25 | 26 | /** 27 | * Takes an XML feed file destined for the GSA and makes a copy in the 28 | * configured feed archive directory. The feed archive directory is 29 | * specified using the {@code feed.archiveDirectory} configuration property. 30 | */ 31 | class GsaFeedFileArchiver implements FeedArchiver { 32 | private static final Logger log = 33 | Logger.getLogger(GsaFeedFileArchiver.class.getName()); 34 | 35 | private static final Charset CHARSET_UTF8 = Charset.forName("UTF-8"); 36 | private final File archiveDir; 37 | 38 | public GsaFeedFileArchiver(String archiveDirectory) { 39 | this.archiveDir = Strings.isNullOrEmpty(archiveDirectory) 40 | ? null : new File(archiveDirectory); 41 | } 42 | 43 | public void saveFeed(String feedName, String feedXml) { 44 | if (archiveDir != null) { 45 | try { 46 | File file = File.createTempFile(feedName + "-", ".xml", archiveDir); 47 | Files.write(feedXml, file, CHARSET_UTF8); 48 | } catch (IOException e) { 49 | log.log(Level.WARNING, "failed to archive feed file", e); 50 | } 51 | } 52 | } 53 | 54 | public void saveFailedFeed(String feedName, String feedXml) { 55 | saveFeed("FAILED-" + feedName, feedXml); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/com/google/enterprise/adaptor/GsaVersion.java: -------------------------------------------------------------------------------- 1 | // Copyright 2013 Google Inc. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor; 16 | 17 | import java.io.IOException; 18 | import java.io.InputStream; 19 | import java.net.URL; 20 | import java.net.URLConnection; 21 | import java.nio.charset.Charset; 22 | import java.util.logging.Level; 23 | import java.util.logging.Logger; 24 | import java.util.regex.Matcher; 25 | import java.util.regex.Pattern; 26 | 27 | /** Acquires and provides GSA's version. */ 28 | final class GsaVersion { 29 | private static final Logger log 30 | = Logger.getLogger(GsaVersion.class.getName()); 31 | private static final Charset charset = Charset.forName("UTF-8"); 32 | 33 | private String ver; // example: 7.2.1-1 34 | private int parts[] = new int[4]; 35 | 36 | private static final Pattern VERSION_FORMAT 37 | = Pattern.compile("^(\\d+)\\.(\\d+)\\.(\\d+)\\-(\\d+)$"); 38 | 39 | GsaVersion(String version) { 40 | ver = version; 41 | Matcher m = VERSION_FORMAT.matcher(ver); 42 | if (!m.matches()) { 43 | throw new IllegalArgumentException( 44 | "GSA version is incorrectly formated : " + ver); 45 | } 46 | parts[0] = Integer.parseInt(m.group(1)); 47 | parts[1] = Integer.parseInt(m.group(2)); 48 | parts[2] = Integer.parseInt(m.group(3)); 49 | parts[3] = Integer.parseInt(m.group(4)); 50 | } 51 | 52 | /* Requsts entire detailed version string and returns it. */ 53 | static GsaVersion get(String host, boolean securely) throws IOException { 54 | String protocol = securely ? "https" : "http"; 55 | URL url = new URL(protocol, host, "/sw_version.txt"); 56 | log.log(Level.FINE, "about to ask GSA for {0}", url); 57 | URLConnection conn = url.openConnection(); 58 | InputStream in = conn.getInputStream(); 59 | String ver = IOHelper.readInputStreamToString(in, charset); 60 | ver = ver.replaceAll("\\s", ""); 61 | return new GsaVersion(ver); 62 | } 63 | 64 | /** Provides entire version string gotten from GSA, eg. 7.2.1-1 */ 65 | @Override 66 | public String toString() { 67 | return ver; 68 | } 69 | 70 | public boolean isAtLeast(String minimum) { 71 | GsaVersion min = new GsaVersion(minimum); 72 | for (int i = 0; i < parts.length; i++) { 73 | if (parts[i] < min.parts[i]) { 74 | return false; 75 | } 76 | if (parts[i] > min.parts[i]) { 77 | return true; 78 | } 79 | // parts[i] == min.parts[i] are equal 80 | } 81 | return true; // all parts were the same 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /src/com/google/enterprise/adaptor/HashMapSession.java: -------------------------------------------------------------------------------- 1 | // Copyright 2011 Google Inc. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor; 16 | 17 | import java.util.HashMap; 18 | import java.util.Map; 19 | 20 | /** 21 | * Generic session-state container, but intended for authn bookkeeping. 22 | */ 23 | class HashMapSession implements Session { 24 | private final Map state = new HashMap(); 25 | 26 | /** 27 | * Set attribute value, replacing existing value if it already exists. 28 | */ 29 | public synchronized void setAttribute(String key, Object value) { 30 | state.put(key, value); 31 | } 32 | 33 | /** 34 | * Get attribute value. 35 | */ 36 | public synchronized Object getAttribute(String key) { 37 | return state.get(key); 38 | } 39 | 40 | /** 41 | * Remove attribute value. 42 | */ 43 | public synchronized Object removeAttribute(String key) { 44 | return state.remove(key); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/com/google/enterprise/adaptor/HttpServerScope.java: -------------------------------------------------------------------------------- 1 | // Copyright 2013 Google Inc. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor; 16 | 17 | import com.sun.net.httpserver.HttpContext; 18 | import com.sun.net.httpserver.HttpHandler; 19 | import com.sun.net.httpserver.HttpServer; 20 | 21 | import java.io.Closeable; 22 | import java.util.ArrayList; 23 | import java.util.List; 24 | 25 | /** 26 | * Provides a scope within an HttpServer. The current implementation simply 27 | * allows adding contexts and having them be cleaned up during {@link #close}, 28 | * as well as namespacing the contexts with a prefix. 29 | * 30 | *

After {@link #close}d, instance may not be reused. 31 | */ 32 | class HttpServerScope implements Closeable { 33 | private final HttpServer server; 34 | private final String contextPrefix; 35 | private final List contexts = new ArrayList(); 36 | private boolean closed; 37 | 38 | public HttpServerScope(HttpServer server, String contextPrefix) { 39 | this.server = server; 40 | this.contextPrefix = contextPrefix; 41 | } 42 | 43 | public synchronized HttpContext createContext( 44 | String path, HttpHandler handler) { 45 | if (closed) { 46 | throw new IllegalStateException("Closed"); 47 | } 48 | HttpContext context = server.createContext(contextPrefix + path, handler); 49 | contexts.add(context); 50 | return context; 51 | } 52 | 53 | /** 54 | * Removes the registered contexts and prevents future requests from being 55 | * processed. Does not impact currently-running requests. 56 | */ 57 | @Override 58 | public synchronized void close() { 59 | for (HttpContext context : contexts) { 60 | server.removeContext(context); 61 | } 62 | contexts.clear(); 63 | closed = true; 64 | } 65 | 66 | public HttpServer getHttpServer() { 67 | return server; 68 | } 69 | 70 | public String getContextPrefix() { 71 | return contextPrefix; 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /src/com/google/enterprise/adaptor/InternalErrorFilter.java: -------------------------------------------------------------------------------- 1 | // Copyright 2013 Google Inc. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor; 16 | 17 | import com.sun.net.httpserver.Filter; 18 | import com.sun.net.httpserver.HttpExchange; 19 | 20 | import java.io.IOException; 21 | import java.net.HttpURLConnection; 22 | import java.util.logging.Level; 23 | import java.util.logging.Logger; 24 | 25 | /** 26 | * Filter that sends 500 Internal Error instead of having the error close the 27 | * connection, when possible. 28 | */ 29 | class InternalErrorFilter extends Filter { 30 | private static final Logger log 31 | = Logger.getLogger(InternalErrorFilter.class.getName()); 32 | 33 | @Override 34 | public String description() { 35 | return "Filter sends HTTP 500 when an exception occurs, when possible"; 36 | } 37 | 38 | @Override 39 | public void doFilter(HttpExchange ex, Filter.Chain chain) throws IOException { 40 | try { 41 | chain.doFilter(ex); 42 | } catch (Exception e) { 43 | // We want to send 500 Internal Error if the response headers have not 44 | // been sent, but HttpExchange doesn't provide a way to learn if they have 45 | // been sent. 46 | if (!HttpExchanges.headersSent(ex)) { 47 | log.log(Level.WARNING, "Unexpected exception during request", e); 48 | HttpExchanges.cannedRespond(ex, HttpURLConnection.HTTP_INTERNAL_ERROR, 49 | Translation.HTTP_INTERNAL_ERROR); 50 | } else { 51 | // The headers have already been sent, so all we can do is throw the 52 | // exception up and allow the server to kill the connection. 53 | if (e instanceof RuntimeException) { 54 | throw (RuntimeException) e; 55 | } else { 56 | throw (IOException) e; 57 | } 58 | } 59 | } 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/com/google/enterprise/adaptor/InvalidConfigurationException.java: -------------------------------------------------------------------------------- 1 | // Copyright 2014 Google Inc. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor; 16 | 17 | /** 18 | * Thrown for unrecoverable configuration errors. Recoverable configuration 19 | * problems (such as server off-line) should throw something other than 20 | * StartupException, so that the retry with backoff logic will be used. 21 | */ 22 | public class InvalidConfigurationException extends StartupException { 23 | /** 24 | * Constructs a new InvalidConfigurationException with no message and no 25 | * cause. 26 | */ 27 | public InvalidConfigurationException() { 28 | super(); 29 | } 30 | 31 | /** 32 | * Constructs a InvalidConfigurationException with a supplied message but no 33 | * cause. 34 | * 35 | * @param message the message. Can be retrieved by the {@link #getMessage()} 36 | * method. 37 | */ 38 | public InvalidConfigurationException(String message) { 39 | super(message); 40 | } 41 | 42 | /** 43 | * Constructs a InvalidConfigurationException with message and cause. 44 | * 45 | * @param message the message. Can be retrieved by the {@link #getMessage()} 46 | * method. 47 | * @param cause failure cause 48 | */ 49 | public InvalidConfigurationException(String message, Throwable cause) { 50 | super(message, cause); 51 | } 52 | 53 | /** 54 | * Constructs a InvalidConfigurationException with specified cause, copying 55 | * its message if cause is non-{@code null}. 56 | * 57 | * @param cause failure cause 58 | */ 59 | public InvalidConfigurationException(Throwable cause) { 60 | super(cause); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/com/google/enterprise/adaptor/NamespacedSession.java: -------------------------------------------------------------------------------- 1 | // Copyright 2013 Google Inc. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor; 16 | 17 | /** A forwarding session that namespaces its keys. */ 18 | class NamespacedSession implements Session { 19 | private final Session session; 20 | private final String prefix; 21 | 22 | public NamespacedSession(Session session, String prefix) { 23 | if (session == null || prefix == null) { 24 | throw new NullPointerException(); 25 | } 26 | this.session = session; 27 | this.prefix = prefix; 28 | } 29 | 30 | @Override 31 | public void setAttribute(String key, Object value) { 32 | key = mapKey(key); 33 | session.setAttribute(key, value); 34 | } 35 | 36 | @Override 37 | public Object getAttribute(String key) { 38 | key = mapKey(key); 39 | return session.getAttribute(key); 40 | } 41 | 42 | @Override 43 | public Object removeAttribute(String key) { 44 | key = mapKey(key); 45 | return session.removeAttribute(key); 46 | } 47 | 48 | private String mapKey(String key) { 49 | return prefix + key; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/com/google/enterprise/adaptor/RedirectHandler.java: -------------------------------------------------------------------------------- 1 | // Copyright 2011 Google Inc. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor; 16 | 17 | import com.sun.net.httpserver.HttpExchange; 18 | import com.sun.net.httpserver.HttpHandler; 19 | 20 | import java.io.IOException; 21 | import java.net.HttpURLConnection; 22 | import java.net.URI; 23 | import java.net.URISyntaxException; 24 | 25 | /** 26 | * HTTP Handler that responds with a fixed redirect. The redirect only occurs if 27 | * this handler's path is identical to the requested path, otherwise the 28 | * response is 404. 29 | */ 30 | class RedirectHandler implements HttpHandler { 31 | private final String redirectPath; 32 | 33 | /** 34 | * @param redirectPath relative or absolute path to redirect clients to 35 | */ 36 | public RedirectHandler(String redirectPath) { 37 | this.redirectPath = redirectPath; 38 | } 39 | 40 | @Override 41 | public void handle(HttpExchange ex) throws IOException { 42 | if (!ex.getRequestURI().getPath().equals(ex.getHttpContext().getPath())) { 43 | HttpExchanges.cannedRespond(ex, HttpURLConnection.HTTP_NOT_FOUND, 44 | Translation.HTTP_NOT_FOUND); 45 | return; 46 | } 47 | 48 | URI base = HttpExchanges.getRequestUri(ex); 49 | URI path; 50 | try { 51 | path = new URI(redirectPath); 52 | } catch (URISyntaxException e) { 53 | throw new IOException("Could not construct URI"); 54 | } 55 | HttpExchanges.sendRedirect(ex, base.resolve(path)); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/com/google/enterprise/adaptor/RelativeTimeProvider.java: -------------------------------------------------------------------------------- 1 | // Copyright 2013 Google Inc. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor; 16 | 17 | import java.util.concurrent.TimeUnit; 18 | 19 | /** Interface for classes that can provide a relative time stamp. */ 20 | interface RelativeTimeProvider { 21 | /** Returns a time stamp that can be used for calculating elapsed time. */ 22 | public long relativeTime(TimeUnit timeUnit); 23 | } 24 | -------------------------------------------------------------------------------- /src/com/google/enterprise/adaptor/SecurityManagerConfig.java: -------------------------------------------------------------------------------- 1 | // Copyright 2011 Google Inc. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor; 16 | 17 | import com.google.enterprise.adaptor.secmgr.authncontroller.ExportedState; 18 | import com.google.enterprise.adaptor.secmgr.config.ConfigSingleton; 19 | import com.google.gson.GsonBuilder; 20 | 21 | class SecurityManagerConfig { 22 | static { 23 | ConfigSingleton.setGsonRegistrations( 24 | new ConfigSingleton.GsonRegistrations() { 25 | @Override 26 | public void register(GsonBuilder builder) { 27 | ExportedState.registerTypeAdapters(builder); 28 | } 29 | }); 30 | } 31 | 32 | /** 33 | * Does nothing, but provides a straight-forward way of initializing the class 34 | * (for static functionality to run). 35 | */ 36 | public static void load() {} 37 | } 38 | -------------------------------------------------------------------------------- /src/com/google/enterprise/adaptor/SensitiveValueDecoder.java: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Google Inc. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor; 16 | 17 | /** 18 | * Provides parsing of sensitive values that can be plain text, obfuscated, or 19 | * encrypted. 20 | */ 21 | public interface SensitiveValueDecoder { 22 | /** 23 | * Decode an encoded sensitive string into its original string. 24 | * @param nonReadable is input needing decoding 25 | * @return String in readable form 26 | * @throws IllegalArgumentException if {@code nonReadable} is unable to be 27 | * decoded 28 | */ 29 | public String decodeValue(String nonReadable); 30 | } 31 | -------------------------------------------------------------------------------- /src/com/google/enterprise/adaptor/Session.java: -------------------------------------------------------------------------------- 1 | // Copyright 2011 Google Inc. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor; 16 | 17 | /** 18 | * Generic session-state container, but intended for authn bookkeeping. 19 | */ 20 | public interface Session { 21 | /** 22 | * Set attribute value, replacing existing value if it already exists. 23 | * @param key key 24 | * @param value value 25 | */ 26 | public void setAttribute(String key, Object value); 27 | 28 | /** 29 | * Get attribute value. 30 | * @param key key 31 | * @return Object value 32 | */ 33 | public Object getAttribute(String key); 34 | 35 | /** 36 | * Remove attribute value. 37 | * @param key key 38 | * @return Object value 39 | */ 40 | public Object removeAttribute(String key); 41 | } 42 | -------------------------------------------------------------------------------- /src/com/google/enterprise/adaptor/SleepHandler.java: -------------------------------------------------------------------------------- 1 | // Copyright 2011 Google Inc. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor; 16 | 17 | import com.sun.net.httpserver.HttpExchange; 18 | import com.sun.net.httpserver.HttpHandler; 19 | 20 | import java.io.IOException; 21 | import java.net.HttpURLConnection; 22 | import java.nio.charset.Charset; 23 | import java.util.logging.Level; 24 | import java.util.logging.Logger; 25 | 26 | /** Handler that simply sleeps for a given amount of time. */ 27 | class SleepHandler implements HttpHandler { 28 | private static final Logger log 29 | = Logger.getLogger(SleepHandler.class.getName()); 30 | 31 | private final Charset charset = Charset.forName("UTF-8"); 32 | private final long sleepDurationMillis; 33 | 34 | public SleepHandler(long sleepDurationMillis) { 35 | this.sleepDurationMillis = sleepDurationMillis; 36 | } 37 | 38 | @Override 39 | public void handle(HttpExchange ex) throws IOException { 40 | if (!"GET".equals(ex.getRequestMethod())) { 41 | HttpExchanges.cannedRespond(ex, HttpURLConnection.HTTP_BAD_METHOD, 42 | Translation.HTTP_BAD_METHOD); 43 | return; 44 | } 45 | if (!ex.getRequestURI().getPath().equals(ex.getHttpContext().getPath())) { 46 | HttpExchanges.cannedRespond(ex, HttpURLConnection.HTTP_NOT_FOUND, 47 | Translation.HTTP_NOT_FOUND); 48 | return; 49 | } 50 | try { 51 | Thread.sleep(sleepDurationMillis); 52 | } catch (InterruptedException ie) { 53 | log.log(Level.WARNING, "Request interrupted", ie); 54 | HttpExchanges.respond(ex, HttpURLConnection.HTTP_INTERNAL_ERROR, 55 | "text/plain", "Interrupted".getBytes(charset)); 56 | return; 57 | } 58 | HttpExchanges.respond(ex, HttpURLConnection.HTTP_OK, "text/plain", 59 | "Done".getBytes(charset)); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/com/google/enterprise/adaptor/StartupException.java: -------------------------------------------------------------------------------- 1 | // Copyright 2014 Google Inc. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor; 16 | 17 | /** 18 | * Thrown for unrecoverable startup errors, such as fatal configuration 19 | * errors or running on the wrong platform. StartupExceptions will bypass 20 | * the retry with back-off recovery logic of {@link Application} and immediately 21 | * terminate the adaptor. 22 | */ 23 | public class StartupException extends RuntimeException { 24 | /** 25 | * Constructs a new StartupException with no message and no cause. 26 | */ 27 | public StartupException() { 28 | super(); 29 | } 30 | 31 | /** 32 | * Constructs a StartupException with a supplied message but no cause. 33 | * 34 | * @param message the message. Can be retrieved by the {@link #getMessage()} 35 | * method. 36 | */ 37 | public StartupException(String message) { 38 | super(message); 39 | } 40 | 41 | /** 42 | * Constructs a StartupException with message and cause. 43 | * 44 | * @param message the message. Can be retrieved by the {@link #getMessage()} 45 | * method. 46 | * @param cause failure cause 47 | */ 48 | public StartupException(String message, Throwable cause) { 49 | super(message, cause); 50 | } 51 | 52 | /** 53 | * Constructs a StartupException with specified cause, copying its message if 54 | * cause is non-{@code null}. 55 | * 56 | * @param cause failure cause 57 | */ 58 | public StartupException(Throwable cause) { 59 | super(cause); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/com/google/enterprise/adaptor/StatusSource.java: -------------------------------------------------------------------------------- 1 | // Copyright 2011 Google Inc. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor; 16 | 17 | import java.util.Locale; 18 | 19 | /** 20 | * A source of {@link Status} messages. 21 | */ 22 | public interface StatusSource { 23 | /** 24 | * Retrieve the current status. This method should return quickly, within the 25 | * order of ten milliseconds. That necessitates that most implementations 26 | * perform their status checks outside of this method. This method should 27 | * never return {@code null}. 28 | * @return Status current Status 29 | */ 30 | public Status retrieveStatus(); 31 | 32 | /** 33 | * Get the name of this source, for displaying to the user. This method should 34 | * never return {@code null}. 35 | * 36 | * @param locale non-{@code null} locale for localization. 37 | * @return String name of this source 38 | */ 39 | public String getName(Locale locale); 40 | } 41 | -------------------------------------------------------------------------------- /src/com/google/enterprise/adaptor/SystemRelativeTimeProvider.java: -------------------------------------------------------------------------------- 1 | // Copyright 2013 Google Inc. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor; 16 | 17 | import java.util.concurrent.TimeUnit; 18 | 19 | /** A {@link RelativeTimeProvider} that uses {@link System#nanoTime}. */ 20 | class SystemRelativeTimeProvider implements RelativeTimeProvider { 21 | @Override 22 | public long relativeTime(TimeUnit timeUnit) { 23 | return timeUnit.convert(System.nanoTime(), TimeUnit.NANOSECONDS); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/com/google/enterprise/adaptor/SystemTimeProvider.java: -------------------------------------------------------------------------------- 1 | // Copyright 2011 Google Inc. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor; 16 | 17 | /** 18 | * A {@link TimeProvider} that uses {@link System#currentTimeMillis} 19 | */ 20 | class SystemTimeProvider implements TimeProvider { 21 | public long currentTimeMillis() { 22 | return System.currentTimeMillis(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/com/google/enterprise/adaptor/TimeProvider.java: -------------------------------------------------------------------------------- 1 | // Copyright 2011 Google Inc. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor; 16 | 17 | /** 18 | * Inteface for classes that can provide the current time. 19 | */ 20 | interface TimeProvider { 21 | public long currentTimeMillis(); 22 | } 23 | -------------------------------------------------------------------------------- /src/com/google/enterprise/adaptor/Translation.java: -------------------------------------------------------------------------------- 1 | // Copyright 2011 Google Inc. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor; 16 | 17 | import java.text.MessageFormat; 18 | import java.util.Locale; 19 | import java.util.ResourceBundle; 20 | 21 | /** 22 | * Enum for all translation keys. All user-visible messages should exist in our 23 | * resource bundle and have its key here. 24 | */ 25 | enum Translation { 26 | AUTHN_RETRY, 27 | AUTHN_UNKNOWN_SESSION, 28 | AUTHN_NOT_STARTED, 29 | AUTHZ_BAD_QUERY_NO_RESOURCE, 30 | AUTHZ_BAD_QUERY_NO_SUBJECT, 31 | AUTHZ_BAD_QUERY_NOT_SAME_USER, 32 | HTTP_BAD_REQUEST_INVALID_JSON, 33 | HTTP_BAD_REQUEST_ERROR_DECODING, 34 | HTTP_BAD_REQUEST_SECURITY_ERROR, 35 | HTTP_FORBIDDEN, 36 | HTTP_FORBIDDEN_AUTHN_FAILURE, 37 | HTTP_FORBIDDEN_SECMGR, 38 | HTTP_NOT_FOUND, 39 | HTTP_BAD_METHOD, 40 | HTTP_CONFLICT_INVALID_HEADER, 41 | HTTP_INTERNAL_ERROR, 42 | STATS_CONFIG_NONE, 43 | STATS_VERSION_UNKNOWN, 44 | STATUS_CRAWLING, 45 | STATUS_CRAWLING_NO_ACCESSES_IN_PAST_DAY, 46 | STATUS_ERROR_RATE, 47 | STATUS_ERROR_RATE_RATE, 48 | STATUS_FEED, 49 | STATUS_FEED_INTERRUPTED, 50 | STATUS_JAVA_VERSION, 51 | STATUS_JAVA_VERSION_SUPPORTED, 52 | STATUS_JAVA_VERSION_UNKNOWN, 53 | STATUS_JAVA_VERSION_UNSUPPORTED, 54 | ; 55 | 56 | /** 57 | * @throws java.util.MissingResourceException if it could not find a string 58 | * for the default locale 59 | */ 60 | @Override 61 | public String toString() { 62 | return toString(Locale.getDefault()); 63 | } 64 | 65 | /** 66 | * @throws java.util.MissingResourceException if it could not find a string 67 | * for the provided {@code locale} 68 | */ 69 | public String toString(Locale locale) { 70 | String localeClassStr = "com.google.enterprise.adaptor.TranslationStrings"; 71 | return ResourceBundle.getBundle(localeClassStr, locale) 72 | .getString(name()); 73 | } 74 | 75 | /** 76 | * @throws java.util.MissingResourceException if it could not find a string 77 | * for the default locale 78 | */ 79 | public String toString(Object... params) { 80 | return toString(Locale.getDefault(), params); 81 | } 82 | 83 | /** 84 | * @throws java.util.MissingResourceException if it could not find a string 85 | * for the provided {@code locale} 86 | */ 87 | public String toString(Locale locale, Object... params) { 88 | String translation = toString(locale); 89 | return new MessageFormat(translation, locale) 90 | .format(params, new StringBuffer(), null).toString(); 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /src/com/google/enterprise/adaptor/TranslationStatus.java: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Google Inc. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor; 16 | 17 | import java.util.Locale; 18 | 19 | class TranslationStatus implements Status { 20 | private final Code code; 21 | private final Translation message; 22 | private final Object[] params; 23 | 24 | public TranslationStatus(Code code) { 25 | this(code, null); 26 | } 27 | 28 | public TranslationStatus(Code code, Translation message, Object... params) { 29 | if (code == null) { 30 | throw new NullPointerException("Code must not be null"); 31 | } 32 | this.code = code; 33 | this.message = message; 34 | this.params = params; 35 | } 36 | 37 | @Override 38 | public Code getCode() { 39 | return code; 40 | } 41 | 42 | @Override 43 | public String getMessage(Locale locale) { 44 | return message == null ? null : message.toString(locale, params); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/com/google/enterprise/adaptor/UnsupportedPlatformException.java: -------------------------------------------------------------------------------- 1 | // Copyright 2014 Google Inc. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor; 16 | 17 | /** 18 | * Thrown if the adaptor cannot be run on this OS environment. 19 | */ 20 | public class UnsupportedPlatformException extends StartupException { 21 | /** 22 | * Constructs a new UnsupportedPlatformException with no message. 23 | */ 24 | public UnsupportedPlatformException() { 25 | super(); 26 | } 27 | 28 | /** 29 | * Constructs a UnsupportedPlatformException with a supplied message. 30 | * 31 | * @param message the message. Can be retrieved by the {@link #getMessage()} 32 | * method. 33 | */ 34 | public UnsupportedPlatformException(String message) { 35 | super(message); 36 | } 37 | 38 | /** 39 | * Constructs a UnsupportedPlatformException with a supplied message and 40 | * cause. 41 | * 42 | * @param message the message. Can be retrieved by the {@link #getMessage()} 43 | * method. 44 | * @param cause failure cause 45 | */ 46 | public UnsupportedPlatformException(String message, Throwable cause) { 47 | super(message, cause); 48 | } 49 | 50 | /** 51 | * Constructs a UnsupportedPlatformException with specified cause, copying its 52 | * message if cause is non-{@code null}. 53 | * 54 | * @param cause failure cause 55 | */ 56 | public UnsupportedPlatformException(Throwable cause) { 57 | super(cause); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/com/google/enterprise/adaptor/UserPrincipal.java: -------------------------------------------------------------------------------- 1 | // Copyright 2013 Google Inc. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor; 16 | 17 | /** 18 | * Represents user. 19 | */ 20 | public final class UserPrincipal extends Principal { 21 | public UserPrincipal(String name, String namespace) { 22 | super(name, namespace); 23 | } 24 | 25 | public UserPrincipal(String name) { 26 | super(name); 27 | } 28 | 29 | /** Always returns {@code true}. */ 30 | @Override 31 | public boolean isUser() { 32 | return true; 33 | } 34 | 35 | /** Always returns {@code false}. */ 36 | @Override 37 | public boolean isGroup() { 38 | return false; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/com/google/enterprise/adaptor/examples/AdaptorTemplate.java: -------------------------------------------------------------------------------- 1 | // Copyright 2011 Google Inc. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor.examples; 16 | 17 | import com.google.enterprise.adaptor.AbstractAdaptor; 18 | import com.google.enterprise.adaptor.DocId; 19 | import com.google.enterprise.adaptor.DocIdPusher; 20 | import com.google.enterprise.adaptor.Request; 21 | import com.google.enterprise.adaptor.Response; 22 | 23 | import java.io.IOException; 24 | import java.io.OutputStream; 25 | import java.nio.charset.Charset; 26 | import java.util.ArrayList; 27 | import java.util.logging.Logger; 28 | 29 | /** 30 | * Demonstrates what code is necessary for putting public 31 | * content onto a GSA. The key operations are: 32 | *

  1. providing document ids 33 | *
  2. providing document bytes given a document id
34 | */ 35 | public class AdaptorTemplate extends AbstractAdaptor { 36 | private static final Logger log 37 | = Logger.getLogger(AdaptorTemplate.class.getName()); 38 | private Charset encoding = Charset.forName("UTF-8"); 39 | 40 | /** Gives list of document ids that you'd like on the GSA. */ 41 | @Override 42 | public void getDocIds(DocIdPusher pusher) throws InterruptedException { 43 | ArrayList mockDocIds = new ArrayList(); 44 | /* Replace this mock data with code that lists your repository. */ 45 | mockDocIds.add(new DocId("1001")); 46 | mockDocIds.add(new DocId("1002")); 47 | pusher.pushDocIds(mockDocIds); 48 | } 49 | 50 | /** Gives the bytes of a document referenced with id. */ 51 | @Override 52 | public void getDocContent(Request req, Response resp) throws IOException { 53 | DocId id = req.getDocId(); 54 | String str; 55 | if ("1001".equals(id.getUniqueId())) { 56 | str = "Document 1001 says hello and apple orange"; 57 | } else if ("1002".equals(id.getUniqueId())) { 58 | str = "Document 1002 says hello and banana strawberry"; 59 | } else { 60 | resp.respondNotFound(); 61 | return; 62 | } 63 | resp.setContentType("text/plain; charset=utf-8"); 64 | OutputStream os = resp.getOutputStream(); 65 | os.write(str.getBytes(encoding)); 66 | } 67 | 68 | /** Call default main for adaptors. 69 | * @param args argv 70 | */ 71 | public static void main(String[] args) { 72 | AbstractAdaptor.main(new AdaptorTemplate(), args); 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/com/google/enterprise/adaptor/examples/GroupDefinitionsWriter.java: -------------------------------------------------------------------------------- 1 | // Copyright 2013 Google Inc. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor.examples; 16 | 17 | import com.google.enterprise.adaptor.AbstractAdaptor; 18 | import com.google.enterprise.adaptor.DocIdPusher; 19 | import com.google.enterprise.adaptor.GroupPrincipal; 20 | import com.google.enterprise.adaptor.Principal; 21 | import com.google.enterprise.adaptor.Request; 22 | import com.google.enterprise.adaptor.Response; 23 | import com.google.enterprise.adaptor.UserPrincipal; 24 | 25 | import java.io.IOException; 26 | import java.nio.charset.Charset; 27 | import java.util.Arrays; 28 | import java.util.List; 29 | import java.util.Map; 30 | import java.util.TreeMap; 31 | import java.util.logging.Logger; 32 | 33 | /** Demonstrates sending group definitions to GSA. */ 34 | public class GroupDefinitionsWriter extends AbstractAdaptor { 35 | private static final Logger log 36 | = Logger.getLogger(GroupDefinitionsWriter.class.getName()); 37 | private static Charset encoding = Charset.forName("UTF-8"); 38 | 39 | private static UserPrincipal u(String name) { 40 | return new UserPrincipal(name); 41 | } 42 | 43 | private static Map> defineSomeGroups() { 44 | Map> groups 45 | = new TreeMap>(); 46 | groups.put(new GroupPrincipal("coppers"), Arrays.asList( 47 | u("JohnLuther"), u("GerryBoyle"), u("DirtyHarry"))); 48 | groups.put(new GroupPrincipal("Q"), Arrays.asList( 49 | u("OliviaDAbo"), u("JohnDeLancie"))); 50 | groups.put(new GroupPrincipal("all"), Arrays.asList( 51 | u("DaffyDuck"), u("BugsBunny"), new GroupPrincipal("coppers"))); 52 | return groups; 53 | } 54 | 55 | @Override 56 | public void getDocIds(DocIdPusher pusher) throws InterruptedException { 57 | boolean caseSensitive = true; 58 | pusher.pushGroupDefinitions(defineSomeGroups(), caseSensitive); 59 | } 60 | 61 | @Override 62 | public void getDocContent(Request req, Response res) throws IOException { 63 | res.respondNotFound(); 64 | } 65 | 66 | public static void main(String args[]) { 67 | AbstractAdaptor.main(new GroupDefinitionsWriter(), args); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/com/google/enterprise/adaptor/examples/MetadataTransformExample.java: -------------------------------------------------------------------------------- 1 | // Copyright 2013 Google Inc. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor.examples; 16 | 17 | import com.google.enterprise.adaptor.Metadata; 18 | import com.google.enterprise.adaptor.MetadataTransform; 19 | 20 | import java.util.Collections; 21 | import java.util.HashSet; 22 | import java.util.Map; 23 | import java.util.Set; 24 | import java.util.logging.Level; 25 | import java.util.logging.Logger; 26 | 27 | /** 28 | * Example transform which will move values from one key to another. 29 | *

30 | * For example the values under key "creator" could be moved to 31 | * be under the key "author". 32 | */ 33 | public class MetadataTransformExample implements MetadataTransform { 34 | private static final Logger log 35 | = Logger.getLogger(MetadataTransformExample.class.getName()); 36 | private final String src; 37 | private final String dest; 38 | 39 | private MetadataTransformExample(String originalKey, String changedKey) { 40 | if (null == originalKey || null == changedKey) { 41 | throw new NullPointerException(); 42 | } 43 | this.src = originalKey; 44 | this.dest = changedKey; 45 | if (src.equals(dest)) { 46 | log.log(Level.WARNING, "original and destination key the same: {0}", src); 47 | } 48 | } 49 | 50 | /** Makes transform from config with "src" and "dest" keys. 51 | * @param cfg configuration 52 | * @return transform 53 | */ 54 | public static MetadataTransformExample create(Map cfg) { 55 | return new MetadataTransformExample(cfg.get("src"), cfg.get("dest")); 56 | } 57 | 58 | @Override 59 | public void transform(Metadata metadata, Map params) { 60 | if (src.equals(dest)) { 61 | return; 62 | } 63 | Set valuesToMove = metadata.getAllValues(src); 64 | if (valuesToMove.isEmpty()) { 65 | log.log(Level.FINE, "no values for {0}. Skipping", src); 66 | } else { 67 | log.log(Level.FINE, "moving values from {0} to {1}: {2}", 68 | new Object[] {src, dest, valuesToMove}); 69 | Set valuesAlreadyThere = metadata.getAllValues(dest); 70 | metadata.set(dest, combine(valuesToMove, valuesAlreadyThere)); 71 | log.log(Level.FINER, "deleting source {0}", src); 72 | metadata.set(src, Collections.emptySet()); 73 | } 74 | } 75 | 76 | private Set combine(Set s1, Set s2) { 77 | Set combined = new HashSet(s1); 78 | combined.addAll(s2); 79 | return combined; 80 | } 81 | 82 | @Override 83 | public String toString() { 84 | return "MetadataTransformExample(src=" + src + ",dest=" + dest + ")"; 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /src/com/google/enterprise/adaptor/examples/helloworldconnector/SimpleAuthnIdentity.java: -------------------------------------------------------------------------------- 1 | package com.google.enterprise.adaptor.examples.helloworldconnector; 2 | 3 | // Copyright 2014 Google Inc. All Rights Reserved. 4 | // 5 | // Licensed under the Apache License, Version 2.0 (the "License"); 6 | // you may not use this file except in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, software 12 | // distributed under the License is distributed on an "AS IS" BASIS, 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | // See the License for the specific language governing permissions and 15 | // limitations under the License. 16 | 17 | import com.google.enterprise.adaptor.AuthnIdentity; 18 | import com.google.enterprise.adaptor.GroupPrincipal; 19 | import com.google.enterprise.adaptor.UserPrincipal; 20 | 21 | import java.util.Collection; 22 | import java.util.Collections; 23 | import java.util.Set; 24 | import java.util.TreeSet; 25 | 26 | /** 27 | * Simple implementation of AuthnIdentity 28 | */ 29 | class SimpleAuthnIdentity implements AuthnIdentity { 30 | 31 | private UserPrincipal user; 32 | private Set groups; 33 | 34 | public SimpleAuthnIdentity(String uid) throws NullPointerException { 35 | if (uid == null) { 36 | throw new NullPointerException("Null user not allowed"); 37 | } 38 | this.user = new UserPrincipal(uid); 39 | } 40 | 41 | //Constructor with user & single group 42 | public SimpleAuthnIdentity(String uid, String gid) 43 | throws NullPointerException { 44 | this(uid); 45 | this.groups = new TreeSet(); 46 | if (gid != null && !"".equals(gid)) { 47 | this.groups.addAll(Collections.singleton(new GroupPrincipal(gid))); 48 | } 49 | this.groups = 50 | Collections.unmodifiableSet(this.groups); 51 | } 52 | 53 | // Constructor with user & groups 54 | public SimpleAuthnIdentity(String uid, Collection gids) 55 | throws NullPointerException { 56 | this(uid); 57 | this.groups = new TreeSet(); 58 | for (String n : gids) { 59 | if (n != null && !"".equals(n)) { 60 | this.groups.addAll(Collections.singleton(new GroupPrincipal(n))); 61 | } 62 | } 63 | this.groups = 64 | Collections.unmodifiableSet(this.groups); 65 | } 66 | 67 | @Override 68 | public UserPrincipal getUser() { 69 | return user; 70 | } 71 | 72 | /** 73 | * Returns null in this example since we don't do anything with the 74 | * password, but getPassword() must be implemented for AuthnIdentity 75 | */ 76 | @Override 77 | public String getPassword() { 78 | return null; 79 | } 80 | 81 | @Override 82 | public Set getGroups() { 83 | return groups; 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /src/com/google/enterprise/adaptor/package.html: -------------------------------------------------------------------------------- 1 | 2 | Adaptor interfaces and implementation. Uses combination of a 3 | document feed and an accompanying HTTP server. 4 |

5 | Feed files, with lists of ids encoded as URLs, are pushed to GSA. 6 |

7 | GSA crawls by making requests for file contents. 8 | 9 | -------------------------------------------------------------------------------- /src/com/google/enterprise/adaptor/secmgr/common/AuthzStatus.java: -------------------------------------------------------------------------------- 1 | // Copyright 2010 Google Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor.secmgr.common; 16 | 17 | /** 18 | * Authorization Status codes. 19 | *

    20 | *
  • {@code PERMIT} means that authorization is granted.
  • 21 | *
  • {@code DENY} means that authorization is positively denied.
  • 22 | *
  • {@code INDETERMINATE} means that permission is neither granted nor 23 | * denied. If a consumer receives this code, it may decide to try other means 24 | * to get a positive decision (i.e. {@code PERMIT} or {@code DENY}).
  • 25 | *
26 | */ 27 | public enum AuthzStatus { 28 | PERMIT("Access PERMITTED"), 29 | DENY("Access DENIED"), 30 | INDETERMINATE("No access decision"); 31 | 32 | private final String description; 33 | 34 | private AuthzStatus(String description) { 35 | this.description = description; 36 | } 37 | 38 | public String getDescription() { 39 | return description; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/com/google/enterprise/adaptor/secmgr/common/SecurityManagerUtil.java: -------------------------------------------------------------------------------- 1 | // Copyright 2009 Google Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor.secmgr.common; 16 | 17 | import com.google.common.annotations.VisibleForTesting; 18 | 19 | import java.util.logging.Logger; 20 | 21 | import javax.annotation.concurrent.ThreadSafe; 22 | 23 | /** 24 | * Utilities useful throughout the security manager. 25 | */ 26 | @ThreadSafe 27 | public class SecurityManagerUtil { 28 | private static final Logger LOGGER = Logger.getLogger(SecurityManagerUtil.class.getName()); 29 | 30 | // don't instantiate 31 | private SecurityManagerUtil() { 32 | throw new UnsupportedOperationException(); 33 | } 34 | 35 | /** 36 | * Annotate a log message with a given session ID. This should be implemented 37 | * in the session manager, but can't be due to cyclic build dependencies. 38 | * 39 | * @param sessionId The session ID to annotate the message with. 40 | * @param message The log message to annotate. 41 | * @return The annotated log message. 42 | */ 43 | public static String sessionLogMessage(String sessionId, String message) { 44 | return "sid " + ((sessionId != null) ? sessionId : "?") + ": " + message; 45 | } 46 | 47 | /** 48 | * Is a given remote "before" time valid? In other words, is it possible that 49 | * the remote "before" time is less than or equal to the remote "now" time? 50 | * 51 | * @param before A before time from a remote host. 52 | * @param now The current time on this host. 53 | * @return True if the before time might not have passed on the remote host. 54 | */ 55 | public static boolean isRemoteBeforeTimeValid(long before, long now) { 56 | return before - CLOCK_SKEW_TIME <= now; 57 | } 58 | 59 | /** 60 | * Is a given remote "on or after" time valid? In other words, is it possible 61 | * that the remote "on or after" time is greater than the remote "now" time? 62 | * 63 | * @param onOrAfter An on-or-after time from a remote host. 64 | * @param now The current time on this host. 65 | * @return True if the remote time might have passed on the remote host. 66 | */ 67 | public static boolean isRemoteOnOrAfterTimeValid(long onOrAfter, long now) { 68 | return onOrAfter + CLOCK_SKEW_TIME > now; 69 | } 70 | 71 | @VisibleForTesting 72 | public static long getClockSkewTime() { 73 | return CLOCK_SKEW_TIME; 74 | } 75 | 76 | private static final long CLOCK_SKEW_TIME = 5000; 77 | } 78 | -------------------------------------------------------------------------------- /src/com/google/enterprise/adaptor/secmgr/config/ConfigSingleton.java: -------------------------------------------------------------------------------- 1 | // Copyright 2008 Google Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor.secmgr.config; 16 | 17 | import com.google.common.base.Preconditions; 18 | import com.google.gson.Gson; 19 | import com.google.gson.GsonBuilder; 20 | 21 | import java.util.logging.Logger; 22 | 23 | import javax.annotation.concurrent.GuardedBy; 24 | import javax.annotation.concurrent.ThreadSafe; 25 | 26 | /** 27 | * A singleton class to access configured parameters. 28 | */ 29 | @ThreadSafe 30 | public class ConfigSingleton { 31 | private static final Logger LOGGER = Logger.getLogger(ConfigSingleton.class.getName()); 32 | 33 | @GuardedBy("class") private static Gson gson; 34 | 35 | private ConfigSingleton() { 36 | } 37 | 38 | public static synchronized void setGsonRegistrations(GsonRegistrations registrations) { 39 | GsonBuilder builder = new GsonBuilder(); 40 | builder.setPrettyPrinting(); 41 | registrations.register(builder); 42 | gson = builder.create(); 43 | } 44 | 45 | /** A type to use for passing in Gson registrations. */ 46 | public interface GsonRegistrations { 47 | public void register(GsonBuilder builder); 48 | } 49 | 50 | public static synchronized Gson getGson() { 51 | Preconditions.checkNotNull(gson); 52 | return gson; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/com/google/enterprise/adaptor/secmgr/http/HttpClientInterface.java: -------------------------------------------------------------------------------- 1 | // Copyright 2008 Google Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor.secmgr.http; 16 | 17 | import com.google.common.collect.ListMultimap; 18 | 19 | import java.net.URL; 20 | 21 | import javax.annotation.Nonnull; 22 | import javax.annotation.Nullable; 23 | 24 | /** 25 | * An abstraction to hide HttpClient behind. 26 | * This allows HTTP transport to be mocked for testing. 27 | */ 28 | public interface HttpClientInterface { 29 | /** 30 | * Create a new HTTP POST exchange object. 31 | * 32 | * @param url The URL to send the request to. 33 | * @param parameters The POST parameters. 34 | * @return A new HTTP exchange object. 35 | */ 36 | public HttpExchange postExchange(@Nonnull URL url, 37 | @Nullable ListMultimap parameters); 38 | } 39 | -------------------------------------------------------------------------------- /src/com/google/enterprise/adaptor/secmgr/identity/Group.java: -------------------------------------------------------------------------------- 1 | // Copyright 2014 Google Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor.secmgr.identity; 16 | 17 | import com.google.common.base.Preconditions; 18 | import com.google.common.base.Strings; 19 | 20 | import java.util.Arrays; 21 | 22 | import javax.annotation.Nonnull; 23 | import javax.annotation.Nullable; 24 | 25 | /** 26 | * Class that contains the info related to a group. 27 | */ 28 | public class Group { 29 | @Nonnull private final String name; 30 | @Nonnull private final String namespace; 31 | @Nullable private final String domain; 32 | 33 | private Group(String name, String namespace, @Nullable String domain) { 34 | Preconditions.checkNotNull(name); 35 | this.name = name; 36 | this.namespace = namespace; 37 | this.domain = domain; 38 | } 39 | 40 | @Nonnull 41 | public static Group make(String name, String namespace, 42 | @Nullable String domain) { 43 | return new Group(name, namespace, domain); 44 | } 45 | 46 | @Nonnull 47 | public static Group make(String name, String namespace) { 48 | return new Group(name, namespace, null); 49 | } 50 | 51 | @Nonnull 52 | public String getName() { 53 | return name; 54 | } 55 | 56 | @Nonnull 57 | public String getNamespace() { 58 | return namespace; 59 | } 60 | 61 | public String getDomain() { 62 | return domain; 63 | } 64 | 65 | @Override 66 | public int hashCode() { 67 | return Arrays.hashCode(new Object[] { namespace, name, domain }); 68 | } 69 | 70 | private static boolean oequals(Object a, Object b) { 71 | return (a == b) || (a != null && a.equals(b)); 72 | } 73 | 74 | @Override 75 | public boolean equals(Object object) { 76 | if (object == this) { 77 | return true; 78 | } 79 | if (!(object instanceof Group)) { 80 | return false; 81 | } 82 | Group group = (Group) object; 83 | return oequals(name, group.getName()) 84 | && oequals(namespace, group.getNamespace()) 85 | && oequals(domain, group.getDomain()); 86 | } 87 | 88 | @Override 89 | public String toString() { 90 | return (Strings.isNullOrEmpty(domain)) ? namespace + ":" + name 91 | : namespace + ":" + name + "@" + domain; 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /src/com/google/enterprise/adaptor/secmgr/json/TypeProxy.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2011 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.google.enterprise.adaptor.secmgr.json; 17 | 18 | /** 19 | * This interface is used by {@link ProxyTypeAdapter} to handle immutable types. 20 | * In addition to methods specified by this interface, the proxy must implement 21 | * a no-argument constructor (for deserialization) and a one-argument 22 | * constructor where the argument is an instance of {@code } (for 23 | * serialization). 24 | * 25 | * @param The type that this is a proxy for. 26 | */ 27 | public interface TypeProxy { 28 | /** 29 | * Creates an instance of {@code } from this proxy. Assumes that the proxy 30 | * has been appropriately initialized. 31 | */ 32 | public S build(); 33 | } 34 | -------------------------------------------------------------------------------- /src/com/google/enterprise/adaptor/secmgr/saml/AttributeValue.java: -------------------------------------------------------------------------------- 1 | // Copyright 2009 Google Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor.secmgr.saml; 16 | 17 | /** This is a necessary extension of the OpenSAML AttributeValue element. */ 18 | public interface AttributeValue 19 | extends org.opensaml.saml2.core.AttributeValue { 20 | 21 | /** 22 | * Set the content of the AttributeValue element. 23 | * Also sets the content type to xs:string. 24 | * @param value The content value. 25 | */ 26 | public void setValue(String value); 27 | } 28 | -------------------------------------------------------------------------------- /src/com/google/enterprise/adaptor/secmgr/saml/AttributeValueBuilder.java: -------------------------------------------------------------------------------- 1 | // Copyright 2009 Google Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor.secmgr.saml; 16 | 17 | import org.opensaml.common.impl.AbstractSAMLObjectBuilder; 18 | import org.opensaml.saml2.core.AttributeValue; 19 | 20 | /** This class should be part of OpenSAML but is missing from there. */ 21 | public class AttributeValueBuilder 22 | extends AbstractSAMLObjectBuilder { 23 | @Override 24 | public AttributeValue buildObject() { 25 | return new AttributeValueImpl(); 26 | } 27 | 28 | @Override 29 | public AttributeValue buildObject(String nsUri, String localName, String nsPrefix) { 30 | return new AttributeValueImpl(nsUri, localName, nsPrefix); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/com/google/enterprise/adaptor/secmgr/saml/AttributeValueImpl.java: -------------------------------------------------------------------------------- 1 | // Copyright 2009 Google Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor.secmgr.saml; 16 | 17 | import org.opensaml.common.xml.SAMLConstants; 18 | import org.opensaml.xml.AbstractExtensibleXMLObject; 19 | 20 | import javax.xml.XMLConstants; 21 | import javax.xml.namespace.QName; 22 | 23 | /** This class should be part of OpenSAML but is missing from there. */ 24 | public class AttributeValueImpl 25 | extends AbstractExtensibleXMLObject 26 | implements AttributeValue { 27 | 28 | private static final QName XSI_TYPE = 29 | new QName(XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, "type"); 30 | private static final String XS_STRING = "xs:string"; 31 | 32 | private String value; 33 | 34 | public AttributeValueImpl() { 35 | super(SAMLConstants.SAML20_NS, 36 | DEFAULT_ELEMENT_LOCAL_NAME, 37 | SAMLConstants.SAML20_PREFIX); 38 | } 39 | 40 | public AttributeValueImpl(String nsUri, String localName, String nsPrefix) { 41 | super(nsUri, localName, nsPrefix); 42 | } 43 | 44 | public String getValue() { 45 | return value; 46 | } 47 | 48 | public void setValue(String value) { 49 | this.value = prepareForAssignment(this.value, value); 50 | this.getUnknownAttributes().put(XSI_TYPE, XS_STRING); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/com/google/enterprise/adaptor/secmgr/saml/AttributeValueMarshaller.java: -------------------------------------------------------------------------------- 1 | // Copyright 2009 Google Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor.secmgr.saml; 16 | 17 | import org.opensaml.common.impl.AbstractSAMLObjectMarshaller; 18 | import org.opensaml.xml.XMLObject; 19 | import org.opensaml.xml.util.XMLHelper; 20 | import org.w3c.dom.Element; 21 | 22 | /** This class should be part of OpenSAML but is missing from there. */ 23 | public class AttributeValueMarshaller 24 | extends AbstractSAMLObjectMarshaller { 25 | @Override 26 | protected void marshallElementContent(XMLObject xmlObject, Element domElement) { 27 | XMLHelper.appendTextContent(domElement, AttributeValueImpl.class.cast(xmlObject).getValue()); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/com/google/enterprise/adaptor/secmgr/saml/AttributeValueUnmarshaller.java: -------------------------------------------------------------------------------- 1 | // Copyright 2009 Google Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor.secmgr.saml; 16 | 17 | import org.opensaml.common.impl.AbstractSAMLObjectUnmarshaller; 18 | import org.opensaml.xml.XMLObject; 19 | 20 | /** This class should be part of OpenSAML but is missing from there. */ 21 | public class AttributeValueUnmarshaller extends AbstractSAMLObjectUnmarshaller { 22 | @Override 23 | protected void processElementContent(XMLObject xmlObject, String elementContent) { 24 | AttributeValueImpl.class.cast(xmlObject).setValue(elementContent); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/com/google/enterprise/adaptor/secmgr/saml/Group.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.google.enterprise.adaptor.secmgr.saml; 17 | 18 | import org.opensaml.common.SAMLObject; 19 | 20 | import javax.annotation.Nonnull; 21 | import javax.annotation.Nullable; 22 | import javax.annotation.ParametersAreNonnullByDefault; 23 | import javax.xml.XMLConstants; 24 | import javax.xml.namespace.QName; 25 | 26 | /** 27 | * A SAML extension added to AuthzDecisionQuery messages by the secmgr. 28 | */ 29 | @ParametersAreNonnullByDefault 30 | public interface Group extends SAMLObject { 31 | public static final QName DEFAULT_ELEMENT_NAME 32 | = new QName(OpenSamlUtil.GOOGLE_NS_URI, "Group", OpenSamlUtil.GOOGLE_NS_PREFIX); 33 | public static final QName NAME_ATTRIB_NAME 34 | = new QName(XMLConstants.NULL_NS_URI, "name", XMLConstants.DEFAULT_NS_PREFIX); 35 | public static final QName NAMESPACE_ATTRIB_NAME 36 | = new QName(XMLConstants.NULL_NS_URI, "namespace", XMLConstants.DEFAULT_NS_PREFIX); 37 | public static final QName DOMAIN_ATTRIB_NAME 38 | = new QName(XMLConstants.NULL_NS_URI, "domain", XMLConstants.DEFAULT_NS_PREFIX); 39 | 40 | @Nonnull 41 | public String getName(); 42 | 43 | @Nonnull 44 | public String getNamespace(); 45 | 46 | @Nullable 47 | public String getDomain(); 48 | 49 | public void setName(String name); 50 | 51 | public void setNamespace(String namespace); 52 | 53 | public void setDomain(@Nullable String domain); 54 | } 55 | -------------------------------------------------------------------------------- /src/com/google/enterprise/adaptor/secmgr/saml/GroupBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.google.enterprise.adaptor.secmgr.saml; 17 | 18 | import org.opensaml.common.impl.AbstractSAMLObjectBuilder; 19 | 20 | /** A factory for {@link Group}. */ 21 | final class GroupBuilder extends AbstractSAMLObjectBuilder { 22 | @Override 23 | public Group buildObject() { 24 | return buildObject( 25 | Group.DEFAULT_ELEMENT_NAME.getNamespaceURI(), 26 | Group.DEFAULT_ELEMENT_NAME.getLocalPart(), 27 | Group.DEFAULT_ELEMENT_NAME.getPrefix()); 28 | } 29 | 30 | @Override 31 | public Group buildObject(String nsUri, String localName, String nsPrefix) { 32 | return new GroupImpl(nsUri, localName, nsPrefix); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/com/google/enterprise/adaptor/secmgr/saml/GroupImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.google.enterprise.adaptor.secmgr.saml; 17 | 18 | import com.google.common.base.Preconditions; 19 | import com.google.common.collect.ImmutableList; 20 | 21 | import org.opensaml.common.impl.AbstractSAMLObject; 22 | import org.opensaml.xml.XMLObject; 23 | 24 | import java.util.List; 25 | 26 | import javax.annotation.ParametersAreNonnullByDefault; 27 | 28 | /** An implementation for {@link Group}. */ 29 | @ParametersAreNonnullByDefault 30 | final class GroupImpl extends AbstractSAMLObject implements Group { 31 | private String name; 32 | private String namespace; 33 | private String domain; 34 | 35 | GroupImpl(String nsUri, String localName, String nsPrefix) { 36 | super(nsUri, localName, nsPrefix); 37 | } 38 | 39 | @Override 40 | public String getName() { 41 | Preconditions.checkState(name != null, "Name must be non-null"); 42 | return name; 43 | } 44 | 45 | @Override 46 | public String getNamespace() { 47 | Preconditions.checkState(namespace != null, "Namespace must be non-null"); 48 | return namespace; 49 | } 50 | 51 | @Override 52 | public String getDomain() { 53 | return domain; 54 | } 55 | 56 | @Override 57 | public void setName(String name) { 58 | Preconditions.checkState(name != null, "Name must be non-null"); 59 | this.name = name; 60 | } 61 | 62 | @Override 63 | public void setNamespace(String namespace) { 64 | Preconditions.checkState(namespace != null, "Namespace must be non-null"); 65 | this.namespace = namespace; 66 | } 67 | 68 | @Override 69 | public void setDomain(String domain) { 70 | this.domain = domain; 71 | } 72 | 73 | @Override 74 | public List getOrderedChildren() { 75 | // This object has no child elements. 76 | return ImmutableList.of(); 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /src/com/google/enterprise/adaptor/secmgr/saml/GroupMarshaller.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.google.enterprise.adaptor.secmgr.saml; 17 | 18 | import org.opensaml.common.impl.AbstractSAMLObjectMarshaller; 19 | import org.opensaml.xml.XMLObject; 20 | import org.opensaml.xml.util.XMLHelper; 21 | import org.w3c.dom.Element; 22 | 23 | /** A marshaller (object to XML converter) for {@link Group}. */ 24 | final class GroupMarshaller extends AbstractSAMLObjectMarshaller { 25 | @Override 26 | protected void marshallAttributes(XMLObject xmlObject, Element domElement) { 27 | Group object = (Group) xmlObject; 28 | XMLHelper.marshallAttribute(Group.NAME_ATTRIB_NAME, object.getName(), domElement, false); 29 | XMLHelper.marshallAttribute(Group.NAMESPACE_ATTRIB_NAME, object.getNamespace(), 30 | domElement, false); 31 | if (object.getDomain() != null) { 32 | XMLHelper.marshallAttribute(Group.DOMAIN_ATTRIB_NAME, object.getDomain(), domElement, false); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/com/google/enterprise/adaptor/secmgr/saml/GroupUnmarshaller.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.google.enterprise.adaptor.secmgr.saml; 17 | 18 | import com.google.enterprise.adaptor.secmgr.common.XmlUtil; 19 | 20 | import org.opensaml.common.impl.AbstractSAMLObjectUnmarshaller; 21 | import org.opensaml.xml.XMLObject; 22 | import org.opensaml.xml.io.UnmarshallingException; 23 | import org.w3c.dom.Attr; 24 | import org.w3c.dom.Element; 25 | 26 | /** An unmarshaller (XML to object converter) for {@link Group}. */ 27 | final class GroupUnmarshaller extends AbstractSAMLObjectUnmarshaller { 28 | 29 | @Override 30 | public XMLObject unmarshall(Element domElement) 31 | throws UnmarshallingException { 32 | Group object = (Group) super.unmarshall(domElement); 33 | try { 34 | object.getName(); 35 | object.getNamespace(); 36 | object.getDomain(); 37 | } catch (IllegalStateException e) { 38 | throw new UnmarshallingException(e); 39 | } 40 | return object; 41 | } 42 | 43 | @Override 44 | protected void processAttribute(XMLObject xmlObject, Attr attribute) 45 | throws UnmarshallingException { 46 | Group object = (Group) xmlObject; 47 | if (XmlUtil.attributeHasQname(attribute, Group.NAME_ATTRIB_NAME)) { 48 | try { 49 | object.setName(attribute.getValue()); 50 | } catch (IllegalArgumentException e) { 51 | throw new UnmarshallingException(e); 52 | } 53 | } else if (XmlUtil.attributeHasQname(attribute, Group.NAMESPACE_ATTRIB_NAME)) { 54 | try { 55 | object.setNamespace(attribute.getValue()); 56 | } catch (IllegalArgumentException e) { 57 | throw new UnmarshallingException(e); 58 | } 59 | } else if (XmlUtil.attributeHasQname(attribute, Group.DOMAIN_ATTRIB_NAME)) { 60 | try { 61 | object.setDomain(attribute.getValue()); 62 | } catch (IllegalArgumentException e) { 63 | throw new UnmarshallingException(e); 64 | } 65 | } 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/com/google/enterprise/adaptor/secmgr/saml/SamlLogUtil.java: -------------------------------------------------------------------------------- 1 | // Copyright 2009 Google Inc. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor.secmgr.saml; 16 | 17 | import com.google.enterprise.adaptor.secmgr.common.XmlUtil; 18 | 19 | import org.opensaml.Configuration; 20 | import org.opensaml.common.SAMLObject; 21 | import org.opensaml.xml.io.MarshallingException; 22 | import org.w3c.dom.Element; 23 | 24 | import java.io.IOException; 25 | 26 | /** 27 | * Utilities for logging SAML messages. 28 | */ 29 | public class SamlLogUtil { 30 | 31 | private SamlLogUtil() { 32 | // prevent instantiation 33 | throw new UnsupportedOperationException(); 34 | } 35 | 36 | /** 37 | * Generate a log message containing a SAML object. The returned string 38 | * should be passed to a logger; if there's an error converting the SAML 39 | * object to a string, the returned string will be an error message. In 40 | * either case we log the result. 41 | * 42 | * @param message Some text that will be prefixed to the log entry. 43 | * @param so A SAML object that will be converted to a string and appended to 44 | * the log entry. 45 | * @return A suitably formatted log message. 46 | * @throws IOException if unable to serialize XML string. 47 | */ 48 | public static String xmlMessage(String message, SAMLObject so) 49 | throws IOException { 50 | Element element = null; 51 | try { 52 | element = Configuration.getMarshallerFactory().getMarshaller(so).marshall(so); 53 | } catch (MarshallingException e) { 54 | return message + ": MarshallingException while marshalling " + so.toString() 55 | + ": " + e.getMessage(); 56 | } 57 | if (element == null) { 58 | return message + ": SAMLObject marshalls to null " + so.toString(); 59 | } 60 | return message + ":\n" + XmlUtil.getInstance().buildXmlString(element.getOwnerDocument()); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/com/google/enterprise/adaptor/secmgr/saml/SecmgrCredential.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.google.enterprise.adaptor.secmgr.saml; 17 | 18 | import org.opensaml.common.SAMLObject; 19 | 20 | import java.util.List; 21 | 22 | import javax.annotation.Nonnull; 23 | import javax.annotation.Nullable; 24 | import javax.annotation.ParametersAreNonnullByDefault; 25 | import javax.xml.XMLConstants; 26 | import javax.xml.namespace.QName; 27 | 28 | /** 29 | * A SAML extension added to AuthzDecisionQuery messages by the secmgr. 30 | */ 31 | @ParametersAreNonnullByDefault 32 | public interface SecmgrCredential extends SAMLObject { 33 | public static final QName DEFAULT_ELEMENT_NAME 34 | = new QName(OpenSamlUtil.GOOGLE_NS_URI, "SecmgrCredential", OpenSamlUtil.GOOGLE_NS_PREFIX); 35 | public static final QName NAME_ATTRIB_NAME 36 | = new QName(XMLConstants.NULL_NS_URI, "name", XMLConstants.DEFAULT_NS_PREFIX); 37 | public static final QName NAMESPACE_ATTRIB_NAME 38 | = new QName(XMLConstants.NULL_NS_URI, "namespace", XMLConstants.DEFAULT_NS_PREFIX); 39 | public static final QName DOMAIN_ATTRIB_NAME 40 | = new QName(XMLConstants.NULL_NS_URI, "domain", XMLConstants.DEFAULT_NS_PREFIX); 41 | public static final QName PASSWORD_ATTRIB_NAME 42 | = new QName(XMLConstants.NULL_NS_URI, "password", XMLConstants.DEFAULT_NS_PREFIX); 43 | 44 | @Nonnull 45 | public String getName(); 46 | 47 | @Nonnull 48 | public String getNamespace(); 49 | 50 | @Nonnull 51 | public String getPassword(); 52 | 53 | @Nullable 54 | public String getDomain(); 55 | 56 | public List getGroups(); 57 | 58 | public void setName(String name); 59 | 60 | public void setNamespace(String namespace); 61 | 62 | public void setDomain(@Nullable String domain); 63 | 64 | public void setPassword(@Nullable String password); 65 | 66 | public void setGroups(List groups); 67 | } 68 | -------------------------------------------------------------------------------- /src/com/google/enterprise/adaptor/secmgr/saml/SecmgrCredentialBuilder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.google.enterprise.adaptor.secmgr.saml; 17 | 18 | import org.opensaml.common.impl.AbstractSAMLObjectBuilder; 19 | 20 | /** A factory for {@link SecmgrCredential}. */ 21 | final class SecmgrCredentialBuilder extends AbstractSAMLObjectBuilder { 22 | @Override 23 | public SecmgrCredential buildObject() { 24 | return buildObject( 25 | SecmgrCredential.DEFAULT_ELEMENT_NAME.getNamespaceURI(), 26 | SecmgrCredential.DEFAULT_ELEMENT_NAME.getLocalPart(), 27 | SecmgrCredential.DEFAULT_ELEMENT_NAME.getPrefix()); 28 | } 29 | 30 | @Override 31 | public SecmgrCredential buildObject(String nsUri, String localName, String nsPrefix) { 32 | return new SecmgrCredentialImpl(nsUri, localName, nsPrefix); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/com/google/enterprise/adaptor/secmgr/saml/SecmgrCredentialImpl.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.google.enterprise.adaptor.secmgr.saml; 17 | 18 | import com.google.common.base.Preconditions; 19 | 20 | import org.opensaml.common.impl.AbstractSAMLObject; 21 | import org.opensaml.xml.XMLObject; 22 | import org.opensaml.xml.util.XMLObjectChildrenList; 23 | 24 | import java.util.ArrayList; 25 | import java.util.Collections; 26 | import java.util.List; 27 | 28 | import javax.annotation.ParametersAreNonnullByDefault; 29 | 30 | /** An implementation for {@link SecmgrCredential}. */ 31 | @ParametersAreNonnullByDefault 32 | final class SecmgrCredentialImpl extends AbstractSAMLObject implements SecmgrCredential { 33 | private String name; 34 | private String namespace; 35 | private String domain; 36 | private String password; 37 | private final XMLObjectChildrenList groups; 38 | 39 | SecmgrCredentialImpl(String nsUri, String localName, String nsPrefix) { 40 | super(nsUri, localName, nsPrefix); 41 | groups = new XMLObjectChildrenList(this); 42 | } 43 | 44 | @Override 45 | public String getName() { 46 | Preconditions.checkState(name != null, "Name must be non-null"); 47 | return name; 48 | } 49 | 50 | @Override 51 | public String getNamespace() { 52 | Preconditions.checkState(namespace != null, "Namespace must be non-null"); 53 | return namespace; 54 | } 55 | 56 | @Override 57 | public String getDomain() { 58 | return domain; 59 | } 60 | 61 | @Override 62 | public String getPassword() { 63 | return password; 64 | } 65 | 66 | @Override 67 | public List getGroups() { 68 | return groups; 69 | } 70 | 71 | @Override 72 | public void setName(String name) { 73 | Preconditions.checkState(name != null, "Name must be non-null"); 74 | this.name = name; 75 | } 76 | 77 | @Override 78 | public void setNamespace(String namespace) { 79 | Preconditions.checkState(namespace != null, "Namespace must be non-null"); 80 | this.namespace = namespace; 81 | } 82 | 83 | @Override 84 | public void setDomain(String domain) { 85 | this.domain = domain; 86 | } 87 | 88 | @Override 89 | public void setPassword(String password) { 90 | this.password = password; 91 | } 92 | 93 | @Override 94 | public void setGroups(List groups) { 95 | this.groups.clear(); 96 | for (Group g : groups) { 97 | g.detach(); 98 | this.groups.add(g); 99 | } 100 | } 101 | 102 | @Override 103 | public List getOrderedChildren() { 104 | ArrayList children = new ArrayList(); 105 | children.addAll(groups); 106 | return Collections.unmodifiableList(children); 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /src/com/google/enterprise/adaptor/secmgr/saml/SecmgrCredentialMarshaller.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2014 Google Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.google.enterprise.adaptor.secmgr.saml; 17 | 18 | import org.opensaml.common.impl.AbstractSAMLObjectMarshaller; 19 | import org.opensaml.xml.XMLObject; 20 | import org.opensaml.xml.util.XMLHelper; 21 | import org.w3c.dom.Element; 22 | 23 | /** A marshaller (object to XML converter) for {@link SecmgrCredential}. */ 24 | final class SecmgrCredentialMarshaller extends AbstractSAMLObjectMarshaller { 25 | @Override 26 | protected void marshallAttributes(XMLObject xmlObject, Element domElement) { 27 | SecmgrCredential object = (SecmgrCredential) xmlObject; 28 | XMLHelper.marshallAttribute(SecmgrCredential.NAME_ATTRIB_NAME, object.getName(), 29 | domElement, false); 30 | XMLHelper.marshallAttribute(SecmgrCredential.NAMESPACE_ATTRIB_NAME, object.getNamespace(), 31 | domElement, false); 32 | if (object.getDomain() != null) { 33 | XMLHelper.marshallAttribute(SecmgrCredential.DOMAIN_ATTRIB_NAME, object.getDomain(), 34 | domElement, false); 35 | } 36 | if (object.getPassword() != null) { 37 | XMLHelper.marshallAttribute(SecmgrCredential.PASSWORD_ATTRIB_NAME, object.getPassword(), 38 | domElement, false); 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/com/google/enterprise/adaptor/testing/UnsupportedRequest.java: -------------------------------------------------------------------------------- 1 | // Copyright 2017 Google Inc. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor.testing; 16 | 17 | import com.google.enterprise.adaptor.DocId; 18 | import com.google.enterprise.adaptor.DocRequest; 19 | import com.google.enterprise.adaptor.Request; 20 | 21 | import java.util.Date; 22 | 23 | /** 24 | * An implementation of {@link Request} that throws an 25 | * {@code UnsupportedOperationException} if any method is called. 26 | * 27 | *

This class is intended to be extended for unit testing, rather 28 | * than implementing the {@link Request} interface directly. 29 | * 30 | * @see DocRequest 31 | */ 32 | public class UnsupportedRequest implements Request { 33 | /** @throws UnsupportedOperationException always */ 34 | @Override 35 | public boolean hasChangedSinceLastAccess(Date lastModified) { 36 | throw new UnsupportedOperationException("UnsupportedRequest was called"); 37 | } 38 | 39 | /** @throws UnsupportedOperationException always */ 40 | @Override 41 | public boolean canRespondWithNoContent(Date lastModified) { 42 | throw new UnsupportedOperationException("UnsupportedRequest was called"); 43 | } 44 | 45 | /** @throws UnsupportedOperationException always */ 46 | @Override 47 | public Date getLastAccessTime() { 48 | throw new UnsupportedOperationException("UnsupportedRequest was called"); 49 | } 50 | 51 | /** @throws UnsupportedOperationException always */ 52 | @Override 53 | public DocId getDocId() { 54 | throw new UnsupportedOperationException("UnsupportedRequest was called"); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /test-resources/com/google/enterprise/adaptor/GsaCommunicationHandlerTest.valid.jks: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/googlegsa/library/4a3b24d885b465cb990a7520900f7cdb56d01dd8/test-resources/com/google/enterprise/adaptor/GsaCommunicationHandlerTest.valid.jks -------------------------------------------------------------------------------- /test-resources/com/google/enterprise/adaptor/JavaExecTestChild.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/googlegsa/library/4a3b24d885b465cb990a7520900f7cdb56d01dd8/test-resources/com/google/enterprise/adaptor/JavaExecTestChild.jar -------------------------------------------------------------------------------- /test-resources/com/google/enterprise/adaptor/resources/DashboardHandlerTest.test.css: -------------------------------------------------------------------------------- 1 | html {} 2 | -------------------------------------------------------------------------------- /test-resources/com/google/enterprise/adaptor/resources/DashboardHandlerTest.test.html: -------------------------------------------------------------------------------- 1 | Test successful. 2 | -------------------------------------------------------------------------------- /test-resources/com/google/enterprise/adaptor/resources/DashboardHandlerTest.test.js: -------------------------------------------------------------------------------- 1 | alert('Test successful.'); 2 | -------------------------------------------------------------------------------- /test-resources/com/google/enterprise/adaptor/resources/DashboardHandlerTest.test.unknown: -------------------------------------------------------------------------------- 1 | !@#$%^&* 2 | -------------------------------------------------------------------------------- /test-resources/test-cacerts.jks: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/googlegsa/library/4a3b24d885b465cb990a7520900f7cdb56d01dd8/test-resources/test-cacerts.jks -------------------------------------------------------------------------------- /test-resources/test-keys.jks: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/googlegsa/library/4a3b24d885b465cb990a7520900f7cdb56d01dd8/test-resources/test-keys.jks -------------------------------------------------------------------------------- /test/com/google/enterprise/adaptor/AbortImmediatelyFilterTest.java: -------------------------------------------------------------------------------- 1 | // Copyright 2013 Google Inc. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor; 16 | 17 | import static org.junit.Assert.assertEquals; 18 | import static org.junit.Assert.assertNotNull; 19 | 20 | import com.sun.net.httpserver.Filter; 21 | import com.sun.net.httpserver.HttpExchange; 22 | import com.sun.net.httpserver.HttpHandler; 23 | 24 | import org.junit.Rule; 25 | import org.junit.Test; 26 | import org.junit.rules.ExpectedException; 27 | 28 | import java.io.IOException; 29 | import java.util.Arrays; 30 | import java.util.List; 31 | 32 | /** Test cases for {@link AbortImmediatelyFilter}. */ 33 | public class AbortImmediatelyFilterTest { 34 | @Rule 35 | public ExpectedException thrown = ExpectedException.none(); 36 | 37 | private Filter filter = new AbortImmediatelyFilter(); 38 | private List filters = Arrays.asList(filter); 39 | private MockHttpExchange ex = new MockHttpExchange("GET", "/", 40 | new MockHttpContext("/")); 41 | 42 | @Test 43 | public void testDescription() { 44 | assertNotNull(filter.description()); 45 | } 46 | 47 | @Test 48 | public void testNormal() throws Exception { 49 | new Filter.Chain(filters, new SuccessHandler()).doFilter(ex); 50 | assertEquals(200, ex.getResponseCode()); 51 | } 52 | 53 | @Test 54 | public void testOverloaded() throws Exception { 55 | Filter.Chain chain = new Filter.Chain(filters, new SuccessHandler()); 56 | HttpExchanges.abortImmediately.set(new Object()); 57 | thrown.expect(IOException.class); 58 | try { 59 | chain.doFilter(ex); 60 | } finally { 61 | HttpExchanges.abortImmediately.set(null); 62 | } 63 | } 64 | 65 | private static class SuccessHandler implements HttpHandler { 66 | @Override 67 | public void handle(HttpExchange ex) throws IOException { 68 | // Translation used in garbage. 69 | HttpExchanges.cannedRespond(ex, 200, Translation.HTTP_NOT_FOUND); 70 | } 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /test/com/google/enterprise/adaptor/AuthnIdentityImplTest.java: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Google Inc. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor; 16 | 17 | import static org.junit.Assert.assertEquals; 18 | import static org.junit.Assert.assertNull; 19 | 20 | import org.junit.Rule; 21 | import org.junit.Test; 22 | import org.junit.rules.ExpectedException; 23 | 24 | import java.util.Collections; 25 | import java.util.Set; 26 | import java.util.TreeSet; 27 | 28 | /** 29 | * Test cases for {@link AuthnIdentityImpl}. 30 | */ 31 | public class AuthnIdentityImplTest { 32 | @Rule 33 | public ExpectedException thrown = ExpectedException.none(); 34 | 35 | @Test 36 | public void testNullUsername() { 37 | thrown.expect(NullPointerException.class); 38 | new AuthnIdentityImpl.Builder(null); 39 | } 40 | 41 | private static Set makeGroups(String name) { 42 | return GroupPrincipal.makeSet(Collections.singleton(name)); 43 | } 44 | 45 | @Test 46 | public void testSetAllConstruction() { 47 | AuthnIdentity identity = new AuthnIdentityImpl 48 | .Builder(new UserPrincipal("testing")) 49 | .setPassword("pass").setGroups(makeGroups("group")) 50 | .build(); 51 | assertEquals("testing", identity.getUser().getName()); 52 | assertEquals("pass", identity.getPassword()); 53 | assertEquals(makeGroups("group"), identity.getGroups()); 54 | } 55 | 56 | @Test 57 | public void testDefaults() { 58 | AuthnIdentity identity = new AuthnIdentityImpl 59 | .Builder(new UserPrincipal("testing")).build(); 60 | assertEquals("testing", identity.getUser().getName()); 61 | assertNull(identity.getPassword()); 62 | assertNull(identity.getGroups()); 63 | } 64 | 65 | @Test 66 | public void testImmutable() { 67 | Set groups = new TreeSet(); 68 | groups.add(new GroupPrincipal("group")); 69 | Set groups2 = new TreeSet(); 70 | groups2.add(new GroupPrincipal("group")); 71 | 72 | AuthnIdentity identity = new AuthnIdentityImpl 73 | .Builder(new UserPrincipal("testing")) 74 | .setGroups(groups).build(); 75 | groups.add(new GroupPrincipal("anotherGroup")); 76 | assertEquals(groups2, identity.getGroups()); 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /test/com/google/enterprise/adaptor/AuthzByAclMockAdaptor.java: -------------------------------------------------------------------------------- 1 | // Copyright 2014 Google Inc. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor; 16 | 17 | import java.util.Arrays; 18 | import java.util.Collection; 19 | import java.util.Collections; 20 | import java.util.HashMap; 21 | import java.util.Map; 22 | 23 | /** 24 | * Mock of {@link Adaptor} that does authz by checking Acl of the document. 25 | */ 26 | class AuthzByAclMockAdaptor extends MockAdaptor { 27 | private final Map aclMap; 28 | 29 | public AuthzByAclMockAdaptor(Map aclMap) { 30 | this.aclMap = aclMap; 31 | } 32 | 33 | @Override 34 | public Map isUserAuthorized(AuthnIdentity identity, 35 | Collection ids) { 36 | Map result = 37 | new HashMap(ids.size() * 2); 38 | for (DocId docId : ids) { 39 | Acl acl = aclMap.get(docId.getUniqueId()); 40 | if (acl == null) { 41 | result.put(docId, AuthzStatus.DENY); 42 | } else { 43 | result.put(docId, Acl.isAuthorized(identity, Arrays.asList(acl))); 44 | } 45 | } 46 | return Collections.unmodifiableMap(result); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /test/com/google/enterprise/adaptor/AuthzByPasswordMockAdaptor.java: -------------------------------------------------------------------------------- 1 | // Copyright 2014 Google Inc. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor; 16 | 17 | import java.util.Collection; 18 | import java.util.Collections; 19 | import java.util.HashMap; 20 | import java.util.Map; 21 | 22 | /** 23 | * Mock of {@link Adaptor} that does authz by checking whether the subject has 24 | * correct password. 25 | */ 26 | class AuthzByPasswordMockAdaptor extends MockAdaptor { 27 | private final Map usernamePasswordMap; 28 | 29 | public AuthzByPasswordMockAdaptor(Map usernamePasswordMap) { 30 | this.usernamePasswordMap = usernamePasswordMap; 31 | } 32 | 33 | private static boolean oequals(Object a, Object b) { 34 | return (a == b) || (a != null && a.equals(b)); 35 | } 36 | 37 | @Override 38 | public Map isUserAuthorized(AuthnIdentity identity, 39 | Collection ids) { 40 | Map result = 41 | new HashMap(ids.size() * 2); 42 | 43 | String fullUsername = identity.getUser().getName(); 44 | String password = identity.getPassword(); 45 | 46 | boolean authenticated = false; 47 | if (usernamePasswordMap.containsKey(fullUsername)) { 48 | authenticated = oequals(password, usernamePasswordMap.get(fullUsername)); 49 | } 50 | 51 | AuthzStatus decision = 52 | authenticated ? AuthzStatus.PERMIT : AuthzStatus.DENY; 53 | for (DocId id : ids) { 54 | result.put(id, decision); 55 | } 56 | return Collections.unmodifiableMap(result); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /test/com/google/enterprise/adaptor/DocIdTest.java: -------------------------------------------------------------------------------- 1 | // Copyright 2011 Google Inc. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor; 16 | 17 | import static org.junit.Assert.assertEquals; 18 | import static org.junit.Assert.assertFalse; 19 | import static org.junit.Assert.assertTrue; 20 | 21 | import org.junit.Rule; 22 | import org.junit.Test; 23 | import org.junit.rules.ExpectedException; 24 | 25 | /** 26 | * Tests for {@link DocId}. 27 | */ 28 | public class DocIdTest { 29 | @Rule 30 | public ExpectedException thrown = ExpectedException.none(); 31 | 32 | @Test 33 | public void testDocIdHash() { 34 | DocId id = new DocId("procure/book3/sd7823.flx"); 35 | DocId id2 = new DocId("procure/book3/sd7823.flx"); 36 | assertEquals("hash mismatch", id.hashCode(), id2.hashCode()); 37 | } 38 | 39 | @Test 40 | public void testDocIdEqual() { 41 | DocId id = new DocId("procure/book3/sd7823.flx"); 42 | DocId id2 = new DocId("procure/book3/sd7823.flx"); 43 | assertEquals(id, id2); 44 | assertFalse(id.equals(new DocId("procure/book3/XYZXYZ.flx"))); 45 | assertFalse(id.equals("Some random object")); 46 | assertFalse(id.equals(null)); 47 | } 48 | 49 | @Test 50 | public void testToString() { 51 | String rawId = "some docid"; 52 | String docIdToString = new DocId(rawId).toString(); 53 | assertTrue(docIdToString.contains(rawId)); 54 | } 55 | 56 | @Test 57 | public void testConstructorNull() { 58 | thrown.expect(NullPointerException.class); 59 | new DocId(null); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /test/com/google/enterprise/adaptor/DocRequestTest.java: -------------------------------------------------------------------------------- 1 | // Copyright 2017 Google Inc. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor; 16 | 17 | import static java.util.concurrent.TimeUnit.DAYS; 18 | import static org.hamcrest.CoreMatchers.containsString; 19 | import static org.junit.Assert.assertFalse; 20 | import static org.junit.Assert.assertNull; 21 | import static org.junit.Assert.assertThat; 22 | import static org.junit.Assert.assertTrue; 23 | 24 | import org.junit.Rule; 25 | import org.junit.Test; 26 | import org.junit.rules.ExpectedException; 27 | 28 | import java.util.Date; 29 | 30 | /** 31 | * Tests for {@link DocRequest}. 32 | */ 33 | public class DocRequestTest { 34 | @Rule 35 | public ExpectedException thrown = ExpectedException.none(); 36 | 37 | @Test 38 | public void testConstructor_nullDocId() { 39 | thrown.expect(NullPointerException.class); 40 | new DocRequest(null); 41 | } 42 | 43 | @Test 44 | public void testConstructor_nullLastAccessTime() { 45 | Request request = new DocRequest(new DocId("42"), null); 46 | assertNull(request.getLastAccessTime()); 47 | } 48 | 49 | @Test 50 | public void testToString() { 51 | // Epoch plus one day is 1970 in every time zone. 52 | Request request = 53 | new DocRequest(new DocId("xyggy"), new Date(DAYS.toMillis(1L))); 54 | assertThat(request.toString(), containsString("xyggy")); 55 | assertThat(request.toString(), containsString("1970")); 56 | } 57 | 58 | @Test 59 | public void testHasChangedSinceLastAccess_nullLastModifiedTime() { 60 | Request request = new DocRequest(new DocId("42"), new Date()); 61 | thrown.expect(NullPointerException.class); 62 | request.hasChangedSinceLastAccess(null); 63 | } 64 | 65 | @Test 66 | public void testHasChangedSinceLastAccess_nullLastAccessTime() { 67 | Request request = new DocRequest(new DocId("42"), null); 68 | assertTrue(request.hasChangedSinceLastAccess(null)); 69 | } 70 | 71 | @Test 72 | public void testCanRespondWithNoContent_supported() { 73 | // Construct a request for an unchanged document. 74 | DocId docId = new DocId("42"); 75 | Date lastAccessTime = new Date(); 76 | Date lastModifiedTime = new Date(0L); 77 | 78 | Request request = new DocRequest(docId, lastAccessTime, true); 79 | assertTrue(request.canRespondWithNoContent(lastModifiedTime)); 80 | } 81 | 82 | @Test 83 | public void testCanRespondWithNoContent_unsupported() { 84 | // Construct a request for an unchanged document. 85 | DocId docId = new DocId("42"); 86 | Date lastAccessTime = new Date(); 87 | Date lastModifiedTime = new Date(0L); 88 | 89 | Request request = new DocRequest(docId, lastAccessTime, false); 90 | assertFalse(request.canRespondWithNoContent(lastModifiedTime)); 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /test/com/google/enterprise/adaptor/ExceptionHandlersTest.java: -------------------------------------------------------------------------------- 1 | // Copyright 2013 Google Inc. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor; 16 | 17 | import static org.junit.Assert.assertEquals; 18 | import static org.junit.Assert.assertFalse; 19 | 20 | import org.junit.Test; 21 | 22 | import java.util.concurrent.TimeUnit; 23 | 24 | /** Tests for {@link ExceptionHandlers}. */ 25 | public class ExceptionHandlersTest { 26 | @Test 27 | public void testBackoffToString() { 28 | ExceptionHandler handler 29 | = ExceptionHandlers.exponentialBackoffHandler(1, 2, TimeUnit.MINUTES); 30 | assertEquals("ExponentialBackoffExceptionHandler(1,2 MINUTES)", 31 | handler.toString()); 32 | } 33 | 34 | @Test 35 | public void testNoRetry() throws Exception { 36 | ExceptionHandler handler = ExceptionHandlers.noRetryHandler(); 37 | // Make sure it doesn't throw InterruptedException 38 | Thread.currentThread().interrupt(); 39 | assertFalse(handler.handleException(new RuntimeException(), 1)); 40 | // Clear flag 41 | Thread.interrupted(); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /test/com/google/enterprise/adaptor/GsaVersionTest.java: -------------------------------------------------------------------------------- 1 | // Copyright 2013 Google Inc. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor; 16 | 17 | import static org.junit.Assert.assertFalse; 18 | import static org.junit.Assert.assertTrue; 19 | 20 | import org.junit.Rule; 21 | import org.junit.Test; 22 | import org.junit.rules.ExpectedException; 23 | 24 | /** Tests for {@link GsaVersion}. */ 25 | public class GsaVersionTest { 26 | @Rule 27 | public ExpectedException thrown = ExpectedException.none(); 28 | 29 | private GsaVersion base = new GsaVersion("7.2.1-1"); 30 | 31 | @Test 32 | public void testConstructorNotEnough() { 33 | thrown.expect(IllegalArgumentException.class); 34 | new GsaVersion("7.2.1"); 35 | } 36 | 37 | @Test 38 | public void testConstructorBadFormat() { 39 | thrown.expect(IllegalArgumentException.class); 40 | new GsaVersion("7.2.1.1"); 41 | } 42 | 43 | @Test 44 | public void testConstructorToomuch() { 45 | thrown.expect(IllegalArgumentException.class); 46 | new GsaVersion("7.2.1.1-1"); 47 | } 48 | 49 | @Test 50 | public void testAtLeastWithBigger() { 51 | assertFalse(base.isAtLeast("7.4.0-1")); 52 | } 53 | 54 | @Test 55 | public void testAtLeastWithSmaller() { 56 | assertTrue(base.isAtLeast("6.14.36-155")); 57 | } 58 | 59 | @Test 60 | public void testAtLeastWithEqual() { 61 | assertTrue(base.isAtLeast("7.2.1-1")); 62 | } 63 | 64 | @Test 65 | public void testAtLeastWithWrongArgument() { 66 | thrown.expect(IllegalArgumentException.class); 67 | assertTrue(base.isAtLeast("6.p.36-155")); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /test/com/google/enterprise/adaptor/IOHelperTest.java: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Google Inc. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor; 16 | 17 | import static org.junit.Assert.assertEquals; 18 | 19 | import org.junit.Test; 20 | 21 | import java.io.ByteArrayInputStream; 22 | import java.nio.charset.Charset; 23 | 24 | /** 25 | * Tests for {@link IOHelper}. 26 | */ 27 | public class IOHelperTest { 28 | private static final Charset charset = Charset.forName("ASCII"); 29 | @Test 30 | public void testReadFullySuccess() throws Exception { 31 | final String golden = "Testing"; 32 | int expected = golden.length(); 33 | byte[] in = new byte[expected + 1]; 34 | int read = IOHelper.readFully( 35 | new ByteArrayInputStream(golden.getBytes(charset)), in, 1, expected); 36 | assertEquals(expected, read); 37 | assertEquals(golden, new String(in, 1, read, charset)); 38 | } 39 | 40 | @Test 41 | public void testReadFullyReachEof() throws Exception { 42 | final String golden = "Testing"; 43 | byte[] in = new byte[golden.length() + 1]; 44 | int read = IOHelper.readFully( 45 | new ByteArrayInputStream(golden.getBytes(charset)), in, 0, in.length); 46 | assertEquals(in.length - 1, read); 47 | assertEquals(golden, new String(in, 0, read, charset)); 48 | } 49 | 50 | @Test 51 | public void testReadFullyImmediateEof() throws Exception { 52 | byte[] in = new byte[1]; 53 | int read = IOHelper.readFully( 54 | new ByteArrayInputStream(new byte[0]), in, 0, in.length); 55 | assertEquals(-1, read); 56 | } 57 | 58 | @Test 59 | public void testReadFullyZeroLengthBuffer() throws Exception { 60 | byte[] in = new byte[0]; 61 | int read = IOHelper.readFully( 62 | new ByteArrayInputStream(new byte[1]), in, 0, 0); 63 | assertEquals(0, read); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /test/com/google/enterprise/adaptor/LoggingFilterTest.java: -------------------------------------------------------------------------------- 1 | // Copyright 2013 Google Inc. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor; 16 | 17 | import static org.junit.Assert.assertEquals; 18 | import static org.junit.Assert.assertNotNull; 19 | import static org.junit.Assert.assertSame; 20 | 21 | import com.sun.net.httpserver.Filter; 22 | import com.sun.net.httpserver.Headers; 23 | import com.sun.net.httpserver.HttpExchange; 24 | import com.sun.net.httpserver.HttpHandler; 25 | 26 | import org.junit.Test; 27 | 28 | import java.io.IOException; 29 | import java.util.Arrays; 30 | import java.util.List; 31 | 32 | /** Test cases for {@link LoggingFilter}. */ 33 | public class LoggingFilterTest { 34 | private LoggingFilter filter = new LoggingFilter(); 35 | private List filters = Arrays.asList(filter); 36 | private MockHttpExchange ex = new MockHttpExchange("GET", "/", 37 | new MockHttpContext(new MockHttpHandler(200, null), "/")); 38 | 39 | @Test 40 | public void testDescription() { 41 | assertNotNull(filter.description()); 42 | } 43 | 44 | @Test 45 | public void testNormal() throws Exception { 46 | new Filter.Chain(filters, new HttpHandler() { 47 | @Override 48 | public void handle(HttpExchange ex) throws IOException { 49 | // Translation used in garbage. 50 | HttpExchanges.cannedRespond(ex, 200, Translation.HTTP_NOT_FOUND); 51 | } 52 | }).doFilter(ex); 53 | assertEquals(200, ex.getResponseCode()); 54 | } 55 | 56 | @Test 57 | public void testRuntimeException() throws Exception { 58 | final RuntimeException rtex = new RuntimeException(); 59 | Filter.Chain chain = new Filter.Chain(filters, new HttpHandler() { 60 | @Override 61 | public void handle(HttpExchange ex) throws IOException { 62 | throw rtex; 63 | } 64 | }); 65 | try { 66 | chain.doFilter(ex); 67 | } catch (RuntimeException ex) { 68 | assertSame(rtex, ex); 69 | } 70 | } 71 | 72 | @Test 73 | public void testIOException() { 74 | final IOException ioex = new IOException(); 75 | Filter.Chain chain = new Filter.Chain(filters, new HttpHandler() { 76 | @Override 77 | public void handle(HttpExchange ex) throws IOException { 78 | throw ioex; 79 | } 80 | }); 81 | try { 82 | chain.doFilter(ex); 83 | } catch (IOException ex) { 84 | assertSame(ioex, ex); 85 | } 86 | } 87 | 88 | @Test 89 | public void testGetLoggableHeaders() { 90 | Headers headers = new Headers(); 91 | headers.set("a", "1"); 92 | headers.set("B", "2"); 93 | assertEquals("A: 1, B: 2", filter.getLoggableHeaders(headers)); 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /test/com/google/enterprise/adaptor/MockAdaptor.java: -------------------------------------------------------------------------------- 1 | // Copyright 2011 Google Inc. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor; 16 | 17 | import java.io.IOException; 18 | import java.util.Collection; 19 | import java.util.Collections; 20 | import java.util.HashMap; 21 | import java.util.Map; 22 | 23 | /** 24 | * Mock of {@link Adaptor}. All documents authz as PERMIT. 25 | */ 26 | class MockAdaptor extends AbstractAdaptor implements AuthzAuthority { 27 | public byte[] documentBytes = new byte[] {1, 2, 3}; 28 | 29 | @Override 30 | public void init(AdaptorContext context) throws Exception { 31 | context.setAuthzAuthority(this); 32 | } 33 | 34 | @Override 35 | public void getDocIds(DocIdPusher pusher) throws InterruptedException, 36 | IOException { 37 | throw new UnsupportedOperationException(); 38 | } 39 | 40 | @Override 41 | public void getDocContent(Request request, Response response) 42 | throws IOException, InterruptedException { 43 | response.getOutputStream().write(documentBytes); 44 | } 45 | 46 | @Override 47 | public Map isUserAuthorized(AuthnIdentity identity, 48 | Collection ids) { 49 | Map result 50 | = new HashMap(ids.size() * 2); 51 | for (DocId id : ids) { 52 | result.put(id, AuthzStatus.PERMIT); 53 | } 54 | return Collections.unmodifiableMap(result); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /test/com/google/enterprise/adaptor/MockConfig.java: -------------------------------------------------------------------------------- 1 | // Copyright 2011 Google Inc. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor; 16 | 17 | import java.io.IOException; 18 | import java.io.Reader; 19 | import java.util.Properties; 20 | import java.util.Set; 21 | 22 | /** 23 | * Mock of {@link Config}. 24 | */ 25 | public class MockConfig extends Config { 26 | protected Properties config = new Properties(); 27 | 28 | public MockConfig() { 29 | } 30 | 31 | public Set getAllKeys() { 32 | return config.stringPropertyNames(); 33 | } 34 | 35 | public String getValue(String key) { 36 | String value = config.getProperty(key); 37 | if (value == null) { 38 | throw new IllegalStateException("unknown key"); 39 | } 40 | return value; 41 | } 42 | 43 | public void load(Reader configFile) throws IOException { 44 | throw new UnsupportedOperationException(); 45 | } 46 | 47 | public void loadDefaultConfigFile() {} 48 | 49 | public String[] autoConfig(String[] args) { 50 | return args; 51 | } 52 | 53 | public void addKey(String key, String defaultValue) { 54 | } 55 | 56 | /** Normal way to add things to this Mock */ 57 | public void setKey(String key, String value) { 58 | config.setProperty(key, value); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /test/com/google/enterprise/adaptor/MockDocIdCodec.java: -------------------------------------------------------------------------------- 1 | // Copyright 2011 Google Inc. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor; 16 | 17 | import java.net.URI; 18 | import java.net.URISyntaxException; 19 | 20 | /** 21 | * Mock of {@link DocIdDecoder}. 22 | */ 23 | class MockDocIdCodec implements DocIdDecoder, DocIdEncoder { 24 | @Override 25 | public DocId decodeDocId(URI uri) { 26 | return new DocId(uri.getPath().substring(1)); 27 | } 28 | 29 | @Override 30 | public URI encodeDocId(DocId docId) { 31 | URI base = URI.create("http://localhost/"); 32 | URI resource; 33 | try { 34 | resource = new URI(null, null, "/" + docId.getUniqueId(), null); 35 | } catch (URISyntaxException ex) { 36 | throw new AssertionError(); 37 | } 38 | return base.resolve(resource); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /test/com/google/enterprise/adaptor/MockFile.java: -------------------------------------------------------------------------------- 1 | // Copyright 2013 Google Inc. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor; 16 | 17 | import java.io.ByteArrayInputStream; 18 | import java.io.File; 19 | import java.io.InputStream; 20 | import java.io.Reader; 21 | import java.io.StringReader; 22 | import java.nio.charset.Charset; 23 | import java.util.Arrays; 24 | 25 | /** Mock File for testing file-related code paths. */ 26 | class MockFile extends File { 27 | private static final Charset CHARSET = Charset.forName("UTF-8"); 28 | 29 | private String fileContents = ""; 30 | private long lastModified; 31 | private boolean exists = true; 32 | private boolean isFile = true; 33 | private File[] children; 34 | 35 | public MockFile(String name) { 36 | super(name); 37 | } 38 | 39 | public Reader createReader() { 40 | if (!exists || !isFile) { 41 | throw new IllegalStateException("File does not exist"); 42 | } 43 | return new StringReader(fileContents); 44 | } 45 | 46 | public InputStream createInputStream() { 47 | if (!exists || !isFile) { 48 | throw new IllegalStateException("File does not exist"); 49 | } 50 | return new ByteArrayInputStream(fileContents.getBytes(CHARSET)); 51 | } 52 | 53 | public MockFile setFileContents(String fileContents) { 54 | this.fileContents = fileContents; 55 | return this; 56 | } 57 | 58 | @Override 59 | public long lastModified() { 60 | if (!exists) { 61 | return 0; 62 | } 63 | return lastModified; 64 | } 65 | 66 | @Override 67 | public boolean setLastModified(long time) { 68 | this.lastModified = time; 69 | return exists; 70 | } 71 | 72 | @Override 73 | public boolean exists() { 74 | return exists; 75 | } 76 | 77 | public MockFile setExists(boolean exists) { 78 | this.exists = exists; 79 | return this; 80 | } 81 | 82 | @Override 83 | public boolean isFile() { 84 | return exists && isFile; 85 | } 86 | 87 | @Override 88 | public boolean isDirectory() { 89 | return exists && !isFile; 90 | } 91 | 92 | /** Marks the file and a directory, with the provided children. */ 93 | public MockFile setChildren(File[] children) { 94 | isFile = false; 95 | this.children = children; 96 | return this; 97 | } 98 | 99 | @Override 100 | public File[] listFiles() { 101 | if (!exists || isFile) { 102 | return null; 103 | } 104 | return Arrays.copyOf(children, children.length); 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /test/com/google/enterprise/adaptor/MockHttpContext.java: -------------------------------------------------------------------------------- 1 | // Copyright 2011 Google Inc. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor; 16 | 17 | import com.sun.net.httpserver.Authenticator; 18 | import com.sun.net.httpserver.Filter; 19 | import com.sun.net.httpserver.HttpContext; 20 | import com.sun.net.httpserver.HttpHandler; 21 | import com.sun.net.httpserver.HttpServer; 22 | 23 | import java.util.Collections; 24 | import java.util.HashMap; 25 | import java.util.List; 26 | import java.util.Map; 27 | import java.util.concurrent.CopyOnWriteArrayList; 28 | 29 | /** 30 | * Mock {@link HttpContext}. 31 | */ 32 | public class MockHttpContext extends HttpContext { 33 | private HttpHandler handler; 34 | private final String path; 35 | private final Map attributes 36 | = Collections.synchronizedMap(new HashMap()); 37 | private final List filters = new CopyOnWriteArrayList(); 38 | private final HttpServer httpServer; 39 | private Authenticator authenticator; 40 | 41 | public MockHttpContext(String path) { 42 | this(new MockHttpServer(), path); 43 | } 44 | 45 | public MockHttpContext(HttpHandler handler, String path) { 46 | this(new MockHttpServer(), path); 47 | this.handler = handler; 48 | } 49 | 50 | public MockHttpContext(HttpServer server, String path) { 51 | if (server == null || path == null) { 52 | throw new NullPointerException(); 53 | } 54 | this.httpServer = server; 55 | this.path = path; 56 | } 57 | 58 | @Override 59 | public Map getAttributes() { 60 | return attributes; 61 | } 62 | 63 | @Override 64 | public synchronized Authenticator getAuthenticator() { 65 | return authenticator; 66 | } 67 | 68 | @Override 69 | public List getFilters() { 70 | return filters; 71 | } 72 | 73 | @Override 74 | public synchronized HttpHandler getHandler() { 75 | return handler; 76 | } 77 | 78 | @Override 79 | public String getPath() { 80 | return path; 81 | } 82 | 83 | @Override 84 | public HttpServer getServer() { 85 | return httpServer; 86 | } 87 | 88 | @Override 89 | public synchronized Authenticator setAuthenticator(Authenticator auth) { 90 | Authenticator old = authenticator; 91 | authenticator = auth; 92 | return old; 93 | } 94 | 95 | @Override 96 | public synchronized void setHandler(HttpHandler h) { 97 | if (h == null) { 98 | throw new NullPointerException(); 99 | } 100 | if (handler != null) { 101 | throw new IllegalArgumentException(); 102 | } 103 | handler = h; 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /test/com/google/enterprise/adaptor/MockHttpHandler.java: -------------------------------------------------------------------------------- 1 | // Copyright 2011 Google Inc. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor; 16 | 17 | import com.sun.net.httpserver.Headers; 18 | import com.sun.net.httpserver.HttpExchange; 19 | import com.sun.net.httpserver.HttpHandler; 20 | 21 | import java.io.IOException; 22 | import java.net.URI; 23 | import java.util.Collections; 24 | import java.util.List; 25 | import java.util.Map; 26 | 27 | /** 28 | * Mock {@link HttpHandler}. 29 | */ 30 | public class MockHttpHandler implements HttpHandler { 31 | private final int responseCode; 32 | private final byte[] responseBytes; 33 | private final Map> responseHeaders; 34 | private String requestMethod; 35 | private URI requestUri; 36 | private Headers requestHeaders; 37 | private byte[] requestBytes; 38 | 39 | public MockHttpHandler(int responseCode, byte[] responseBytes) { 40 | this(responseCode, responseBytes, 41 | Collections.>emptyMap()); 42 | } 43 | 44 | public MockHttpHandler(int responseCode, byte[] responseBytes, 45 | Map> responseHeaders) { 46 | this.responseCode = responseCode; 47 | this.responseBytes = responseBytes; 48 | this.responseHeaders = responseHeaders; 49 | } 50 | 51 | @Override 52 | public void handle(HttpExchange ex) throws IOException { 53 | requestMethod = ex.getRequestMethod(); 54 | requestUri = ex.getRequestURI(); 55 | requestHeaders = new Headers(); 56 | requestHeaders.putAll(ex.getRequestHeaders()); 57 | requestBytes = IOHelper.readInputStreamToByteArray(ex.getRequestBody()); 58 | ex.getResponseHeaders().putAll(responseHeaders); 59 | ex.sendResponseHeaders(responseCode, responseBytes == null ? -1 : 0); 60 | if (responseBytes != null) { 61 | ex.getResponseBody().write(responseBytes); 62 | ex.getResponseBody().flush(); 63 | ex.getResponseBody().close(); 64 | } 65 | ex.close(); 66 | } 67 | 68 | public String getRequestMethod() { 69 | return requestMethod; 70 | } 71 | 72 | public URI getRequestUri() { 73 | return requestUri; 74 | } 75 | 76 | public Headers getRequestHeaders() { 77 | return requestHeaders; 78 | } 79 | 80 | public byte[] getRequestBytes() { 81 | return requestBytes; 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /test/com/google/enterprise/adaptor/MockHttpsContext.java: -------------------------------------------------------------------------------- 1 | // Copyright 2013 Google Inc. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor; 16 | 17 | import com.sun.net.httpserver.HttpHandler; 18 | import com.sun.net.httpserver.HttpServer; 19 | 20 | /** 21 | * Mock {@link HttpContext}. 22 | */ 23 | public class MockHttpsContext extends MockHttpContext { 24 | private final HttpServer httpServer = new MockHttpsServer(); 25 | 26 | public MockHttpsContext(HttpHandler handler, String path) { 27 | super(handler, path); 28 | } 29 | 30 | @Override 31 | public HttpServer getServer() { 32 | return httpServer; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /test/com/google/enterprise/adaptor/MockHttpsServer.java: -------------------------------------------------------------------------------- 1 | // Copyright 2013 Google Inc. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor; 16 | 17 | import com.sun.net.httpserver.HttpContext; 18 | import com.sun.net.httpserver.HttpHandler; 19 | import com.sun.net.httpserver.HttpsConfigurator; 20 | import com.sun.net.httpserver.HttpsServer; 21 | 22 | import java.net.InetSocketAddress; 23 | import java.util.concurrent.Executor; 24 | 25 | /** 26 | * Mock {@link HttpsServer}. 27 | */ 28 | public class MockHttpsServer extends HttpsServer { 29 | @Override 30 | public void bind(InetSocketAddress addr, int backlog) { 31 | throw new UnsupportedOperationException(); 32 | } 33 | 34 | @Override 35 | public HttpContext createContext(String path) { 36 | throw new UnsupportedOperationException(); 37 | } 38 | 39 | @Override 40 | public HttpContext createContext(String path, HttpHandler handler) { 41 | throw new UnsupportedOperationException(); 42 | } 43 | 44 | @Override 45 | public InetSocketAddress getAddress() { 46 | throw new UnsupportedOperationException(); 47 | } 48 | 49 | @Override 50 | public Executor getExecutor() { 51 | throw new UnsupportedOperationException(); 52 | } 53 | 54 | @Override 55 | public void removeContext(HttpContext context) { 56 | throw new UnsupportedOperationException(); 57 | } 58 | 59 | @Override 60 | public void removeContext(String path) { 61 | throw new UnsupportedOperationException(); 62 | } 63 | 64 | @Override 65 | public void setExecutor(Executor executor) { 66 | throw new UnsupportedOperationException(); 67 | } 68 | 69 | @Override 70 | public void start() { 71 | throw new UnsupportedOperationException(); 72 | } 73 | 74 | @Override 75 | public void stop(int delay) { 76 | throw new UnsupportedOperationException(); 77 | } 78 | 79 | @Override 80 | public HttpsConfigurator getHttpsConfigurator() { 81 | throw new UnsupportedOperationException(); 82 | } 83 | 84 | @Override 85 | public void setHttpsConfigurator(HttpsConfigurator conf) { 86 | throw new UnsupportedOperationException(); 87 | } 88 | 89 | } 90 | -------------------------------------------------------------------------------- /test/com/google/enterprise/adaptor/MockJournal.java: -------------------------------------------------------------------------------- 1 | // Copyright 2011 Google Inc. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor; 16 | 17 | import java.util.List; 18 | 19 | /** 20 | * Mock {@link Journal} that always throws {@link 21 | * UnsupportedOperationException}. 22 | */ 23 | public class MockJournal extends Journal { 24 | public MockJournal(TimeProvider timeProvider) { 25 | super(timeProvider); 26 | } 27 | 28 | @Override 29 | void recordDocIdPush(List pushed) { 30 | throw new UnsupportedOperationException(); 31 | } 32 | 33 | @Override 34 | void recordGsaContentRequest(DocId docId) { 35 | throw new UnsupportedOperationException(); 36 | } 37 | 38 | @Override 39 | void recordNonGsaContentRequest(DocId requested) { 40 | throw new UnsupportedOperationException(); 41 | } 42 | 43 | @Override 44 | void recordRequestProcessingStart() { 45 | throw new UnsupportedOperationException(); 46 | } 47 | 48 | @Override 49 | void recordRequestProcessingEnd(long responseSize) { 50 | throw new UnsupportedOperationException(); 51 | } 52 | 53 | @Override 54 | JournalSnapshot getSnapshot() { 55 | throw new UnsupportedOperationException(); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /test/com/google/enterprise/adaptor/MockStatus.java: -------------------------------------------------------------------------------- 1 | // Copyright 2012 Google Inc. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor; 16 | 17 | import java.util.Locale; 18 | 19 | /** 20 | * Simplistic implementation of {@link Status} without localization support. 21 | */ 22 | class MockStatus implements Status { 23 | private final Code code; 24 | private final String message; 25 | 26 | public MockStatus(Code code) { 27 | this(code, null); 28 | } 29 | 30 | public MockStatus(Code code, String message) { 31 | if (code == null) { 32 | throw new NullPointerException(); 33 | } 34 | this.code = code; 35 | this.message = message; 36 | } 37 | 38 | @Override 39 | public Code getCode() { 40 | return code; 41 | } 42 | 43 | @Override 44 | public String getMessage(Locale locale) { 45 | return message; 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /test/com/google/enterprise/adaptor/MockStatusSource.java: -------------------------------------------------------------------------------- 1 | // Copyright 2011 Google Inc. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor; 16 | 17 | import java.util.Locale; 18 | 19 | /** 20 | * Simplistic implementation of {@link StatusSource} for use with foreign logic 21 | * calling {@link #setStatus} to update state. 22 | * 23 | *

This class is thread-safe and can safely have its status viewed and 24 | * changed in multiple threads without external synchronization. 25 | */ 26 | class MockStatusSource implements StatusSource { 27 | private final String name; 28 | private volatile Status status; 29 | 30 | public MockStatusSource(String name, Status status) { 31 | if (name == null || status == null) { 32 | throw new NullPointerException(); 33 | } 34 | this.name = name; 35 | this.status = status; 36 | } 37 | 38 | @Override 39 | public Status retrieveStatus() { 40 | return status; 41 | } 42 | 43 | /** 44 | * Set the status. 45 | * 46 | * @throws NullPointerException when {@code status} is {@code null} 47 | */ 48 | public void setStatus(Status status) { 49 | if (status == null) { 50 | throw new NullPointerException(); 51 | } 52 | this.status = status; 53 | } 54 | 55 | @Override 56 | public String getName(Locale locale) { 57 | return name; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /test/com/google/enterprise/adaptor/MockTimeProvider.java: -------------------------------------------------------------------------------- 1 | // Copyright 2011 Google Inc. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor; 16 | 17 | /** 18 | * Mock {@link Journal.TimeProvider}. 19 | */ 20 | public class MockTimeProvider implements TimeProvider { 21 | public long time; 22 | // Is needed since the Journal determines the timer resolution 23 | public boolean autoIncrement = true; 24 | 25 | public long currentTimeMillis() { 26 | long currentTime = time; 27 | if (autoIncrement) { 28 | time++; 29 | } 30 | return currentTime; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /test/com/google/enterprise/adaptor/MockWatchdog.java: -------------------------------------------------------------------------------- 1 | // Copyright 2013 Google Inc. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor; 16 | 17 | import com.google.common.util.concurrent.ThreadFactoryBuilder; 18 | 19 | import java.util.concurrent.Executors; 20 | import java.util.concurrent.ScheduledExecutorService; 21 | 22 | /** Empty watchdog implementation that never interrupts other threads. */ 23 | class MockWatchdog extends Watchdog { 24 | /** 25 | * Shared executor that nothing is executed with. It is not shutdown properly 26 | * to make using the mock easier. 27 | */ 28 | private static final ScheduledExecutorService executor 29 | = Executors.newSingleThreadScheduledExecutor( 30 | new ThreadFactoryBuilder().setDaemon(true).build()); 31 | 32 | public MockWatchdog() { 33 | super(executor); 34 | } 35 | 36 | @Override 37 | public void processingStarting(long timeout) { 38 | } 39 | 40 | @Override 41 | public void processingStarting(Thread thread, long timout) { 42 | } 43 | 44 | @Override 45 | public void processingCompleted() { 46 | } 47 | 48 | @Override 49 | public void processingCompleted(Thread thread) { 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /test/com/google/enterprise/adaptor/NamespacedSessionTest.java: -------------------------------------------------------------------------------- 1 | // Copyright 2013 Google Inc. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor; 16 | 17 | import static org.junit.Assert.assertNull; 18 | import static org.junit.Assert.assertSame; 19 | 20 | import org.junit.Rule; 21 | import org.junit.Test; 22 | import org.junit.rules.ExpectedException; 23 | 24 | /** Test cases for {@link NampespacedSession}. */ 25 | public class NamespacedSessionTest { 26 | @Rule 27 | public ExpectedException thrown = ExpectedException.none(); 28 | 29 | private Session baseSession = new HashMapSession(); 30 | private NamespacedSession session 31 | = new NamespacedSession(baseSession, "prefix-"); 32 | 33 | @Test 34 | public void testNullSession() { 35 | thrown.expect(NullPointerException.class); 36 | new NamespacedSession(null, "prefix-"); 37 | } 38 | 39 | @Test 40 | public void testNullPrefix() { 41 | thrown.expect(NullPointerException.class); 42 | new NamespacedSession(baseSession, null); 43 | } 44 | 45 | @Test 46 | public void testNamespacing() { 47 | Object o = new Object(); 48 | assertNull(session.getAttribute("item")); 49 | assertNull(session.removeAttribute("item")); 50 | session.setAttribute("item", o); 51 | assertSame(o, baseSession.getAttribute("prefix-item")); 52 | assertSame(o, session.getAttribute("item")); 53 | assertSame(o, session.removeAttribute("item")); 54 | assertNull(session.getAttribute("item")); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /test/com/google/enterprise/adaptor/PrivateMockAdaptor.java: -------------------------------------------------------------------------------- 1 | // Copyright 2011 Google Inc. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor; 16 | 17 | import java.util.Collection; 18 | import java.util.Collections; 19 | import java.util.HashMap; 20 | import java.util.Map; 21 | 22 | /** 23 | * Mock of {@link Adaptor} that denies all users. 24 | */ 25 | class PrivateMockAdaptor extends MockAdaptor { 26 | @Override 27 | public Map isUserAuthorized(AuthnIdentity identity, 28 | Collection ids) { 29 | Map result 30 | = new HashMap(ids.size() * 2); 31 | for (DocId id : ids) { 32 | result.put(id, AuthzStatus.DENY); 33 | } 34 | return Collections.unmodifiableMap(result); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /test/com/google/enterprise/adaptor/SleepHandlerTest.java: -------------------------------------------------------------------------------- 1 | // Copyright 2011 Google Inc. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor; 16 | 17 | import static org.junit.Assert.assertEquals; 18 | 19 | import org.junit.Test; 20 | 21 | /** 22 | * Tests for {@link SleepHandler}. 23 | */ 24 | public class SleepHandlerTest { 25 | private SleepHandler handler = new SleepHandler(1); 26 | 27 | @Test 28 | public void testSuccess() throws Exception { 29 | MockHttpExchange ex = makeExchange("GET", "/r", "/r"); 30 | handler.handle(ex); 31 | assertEquals(200, ex.getResponseCode()); 32 | } 33 | 34 | @Test 35 | public void testPost() throws Exception { 36 | MockHttpExchange ex = makeExchange("POST", "/r", "/r"); 37 | handler.handle(ex); 38 | assertEquals(405, ex.getResponseCode()); 39 | } 40 | 41 | @Test 42 | public void testWrongPath() throws Exception { 43 | MockHttpExchange ex = makeExchange("GET", "/rwrong", "/r"); 44 | handler.handle(ex); 45 | assertEquals(404, ex.getResponseCode()); 46 | } 47 | 48 | @Test 49 | public void testInterrupted() throws Exception { 50 | MockHttpExchange ex = makeExchange("GET", "/r", "/r"); 51 | Thread.currentThread().interrupt(); 52 | handler.handle(ex); 53 | assertEquals(500, ex.getResponseCode()); 54 | } 55 | 56 | private MockHttpExchange makeExchange(String method, String path, 57 | String contextPath) throws Exception { 58 | MockHttpExchange ex = new MockHttpExchange(method, path, 59 | new MockHttpContext(handler, contextPath)); 60 | return ex; 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /test/com/google/enterprise/adaptor/examples/MetadataTransformExampleTest.java: -------------------------------------------------------------------------------- 1 | // Copyright 2013 Google Inc. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor.examples; 16 | 17 | import static org.junit.Assert.assertEquals; 18 | 19 | import com.google.enterprise.adaptor.Metadata; 20 | import com.google.enterprise.adaptor.MetadataTransform; 21 | 22 | import org.junit.Rule; 23 | import org.junit.Test; 24 | import org.junit.rules.ExpectedException; 25 | 26 | import java.util.HashMap; 27 | import java.util.Map; 28 | 29 | /** Unit tests for {@link MetadataTransformExample}. */ 30 | public class MetadataTransformExampleTest { 31 | @Rule 32 | public ExpectedException thrown = ExpectedException.none(); 33 | 34 | private static Map makeConfig(String src, String dest) { 35 | Map config = new HashMap(); 36 | config.put("src", src); 37 | config.put("dest", dest); 38 | return config; 39 | } 40 | 41 | @Test 42 | public void testMove() { 43 | final Metadata metadataGolden; 44 | { 45 | Metadata golden = new Metadata(); 46 | golden.add("color", "orange"); 47 | golden.add("color", "keylime"); 48 | golden.add("color", "oxblood"); 49 | metadataGolden = golden.unmodifiableView(); 50 | } 51 | Metadata metadata = new Metadata(); 52 | metadata.add("colour", "keylime"); 53 | metadata.add("color", "oxblood"); 54 | metadata.add("colour", "orange"); 55 | MetadataTransform transform 56 | = MetadataTransformExample.create(makeConfig("colour", "color")); 57 | transform.transform(metadata, new HashMap()); 58 | assertEquals(metadataGolden, metadata); 59 | } 60 | 61 | @Test 62 | public void testDegenerateMove() { 63 | final Metadata metadataGolden; 64 | { 65 | Metadata golden = new Metadata(); 66 | golden.add("color", "orange"); 67 | golden.add("color", "keylime"); 68 | golden.add("color", "oxblood"); 69 | metadataGolden = golden.unmodifiableView(); 70 | } 71 | Metadata metadata = new Metadata(); 72 | metadata.add("color", "keylime"); 73 | metadata.add("color", "oxblood"); 74 | metadata.add("color", "orange"); 75 | MetadataTransform transform 76 | = MetadataTransformExample.create(makeConfig("color", "color")); 77 | transform.transform(metadata, new HashMap()); 78 | assertEquals(metadataGolden, metadata); 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /test/com/google/enterprise/adaptor/prebuilt/CommandLineTransformTest.java: -------------------------------------------------------------------------------- 1 | // Copyright 2011 Google Inc. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor.prebuilt; 16 | 17 | import static org.junit.Assert.assertEquals; 18 | 19 | import com.google.enterprise.adaptor.Metadata; 20 | import com.google.enterprise.adaptor.TestHelper; 21 | 22 | import org.junit.Test; 23 | 24 | import java.io.ByteArrayOutputStream; 25 | import java.io.IOException; 26 | import java.util.Arrays; 27 | import java.util.HashMap; 28 | import java.util.Map; 29 | 30 | /** 31 | * Tests for {@link CommandLineTransform}. 32 | */ 33 | public class CommandLineTransformTest { 34 | @Test 35 | public void testSed() throws IOException { 36 | TestHelper.assumeOsIsNotWindows(); 37 | 38 | ByteArrayOutputStream contentOut = new ByteArrayOutputStream(); 39 | Metadata metadata = new Metadata(); 40 | metadata.add("metaKey1", "metaValue1"); 41 | Map params = new HashMap(); 42 | params.put("key1", "value1"); 43 | 44 | CommandLineTransform cmd = new CommandLineTransform(); 45 | cmd.setTransformCommand(Arrays.asList(new String[] {"sed", "s/i/1/"})); 46 | cmd.setCommandAcceptsParameters(false); 47 | cmd.transform(metadata, params); 48 | 49 | assertEquals("metaValue1", metadata.getOneValue("metaKey1")); 50 | assertEquals(1, metadata.getKeys().size()); 51 | assertEquals("value1", params.get("key1")); 52 | assertEquals(1, params.keySet().size()); 53 | } 54 | 55 | @Test 56 | public void testSedWithMetadata() throws IOException { 57 | TestHelper.assumeOsIsNotWindows(); 58 | 59 | Metadata metadata = new Metadata(); 60 | metadata.add("metaKey1", "metaValue1"); 61 | Map params = new HashMap(); 62 | params.put("key1", "value1"); 63 | 64 | CommandLineTransform cmd = new CommandLineTransform(); 65 | cmd.setTransformCommand(Arrays.asList(new String[] {"/bin/sh", "-c", 66 | // Setup variables and temp space. 67 | " META=\"$0\"; PARAM=\"$1\"; TMPFILE=$(mktemp /tmp/adaptor.test.XXXXXXXX);" 68 | // Process metadata. 69 | + "(sed s/1/2/g < \"$META\" > \"$TMPFILE\"; cp \"$TMPFILE\" \"$META\") >&2;" 70 | // Process params. 71 | + "(sed s/1/3/g < \"$PARAM\" > \"$TMPFILE\"; cp \"$TMPFILE\" \"$PARAM\") >&2;" 72 | // Cleanup. 73 | + "rm \"$TMPFILE\" >&2;" 74 | })); 75 | cmd.setCommandAcceptsParameters(true); 76 | cmd.transform(metadata, params); 77 | 78 | assertEquals(1, metadata.getKeys().size()); 79 | assertEquals("metaValue2", metadata.getOneValue("metaKey2")); 80 | assertEquals("value3", params.get("key3")); 81 | assertEquals(1, params.size()); 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /test/com/google/enterprise/adaptor/prebuilt/CommandTest.java: -------------------------------------------------------------------------------- 1 | // Copyright 2011 Google Inc. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor.prebuilt; 16 | 17 | import static org.junit.Assert.assertEquals; 18 | 19 | import com.google.enterprise.adaptor.TestHelper; 20 | 21 | import org.junit.Rule; 22 | import org.junit.Test; 23 | import org.junit.rules.ExpectedException; 24 | 25 | /** 26 | * Tests for {@link Command}. 27 | */ 28 | public class CommandTest { 29 | @Rule 30 | public ExpectedException thrown = ExpectedException.none(); 31 | 32 | @Test 33 | public void testStdinStdout() throws java.io.IOException, 34 | InterruptedException { 35 | TestHelper.assumeOsIsNotWindows(); 36 | final String value = "hello"; 37 | final String encoding = "US-ASCII"; 38 | Command.Result result 39 | = Command.exec(new String[] {"cat"}, value.getBytes(encoding)); 40 | assertEquals(0, result.getReturnCode()); 41 | assertEquals(value, new String(result.getStdout(), encoding)); 42 | assertEquals(0, result.getStderr().length); 43 | } 44 | 45 | @Test 46 | public void testInterrupted() throws java.io.IOException, 47 | InterruptedException { 48 | TestHelper.assumeOsIsNotWindows(); 49 | // Only sets flag, does not immediately throw InterruptedException. 50 | Thread.currentThread().interrupt(); 51 | thrown.expect(InterruptedException.class); 52 | Command.exec(new String[] {"sleep", "10"}); 53 | } 54 | 55 | @Test 56 | public void testInterruptedWindows() throws java.io.IOException, 57 | InterruptedException { 58 | TestHelper.assumeOsIsWindows(); 59 | // Only sets flag, does not immediately throw InterruptedException. 60 | Thread.currentThread().interrupt(); 61 | thrown.expect(InterruptedException.class); 62 | // Use a 10 second ping as substitute for unix sleep. 63 | Command.exec(new String[] {"ping", "-n", "10", "localhost"}); 64 | } 65 | 66 | } 67 | -------------------------------------------------------------------------------- /test/com/google/enterprise/adaptor/prebuilt/StreamingCommandTest.java: -------------------------------------------------------------------------------- 1 | // Copyright 2011 Google Inc. All Rights Reserved. 2 | // 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | // 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | // 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | package com.google.enterprise.adaptor.prebuilt; 16 | 17 | import static org.junit.Assert.assertEquals; 18 | 19 | import com.google.enterprise.adaptor.TestHelper; 20 | 21 | import org.junit.Test; 22 | 23 | /** 24 | * Tests for {@link StreamingCommand}. 25 | */ 26 | public class StreamingCommandTest { 27 | @Test 28 | public void testNoSourceAndSink() throws Exception { 29 | TestHelper.assumeOsIsNotWindows(); 30 | String[] cmd = new String[] {"echo", "This is a test"}; 31 | assertEquals(0, StreamingCommand.exec(cmd, null, null, null)); 32 | } 33 | } 34 | --------------------------------------------------------------------------------