[jcifs] Problem (and fix) accessing file contents using smb: url

Thierry Lach thierry.lach at gmail.com
Wed Nov 15 15:16:05 GMT 2006


That would make my fix look something like this (also adding private MimeMap
mimeMap and the import up toward the beginning) I think....

    /**
     * This URLConnection method returns a calculated content type.
     *
     * @see java.net.URLConnection#getContentType()
     */
    public String getContentType() {
        String contentType = super.getContentType();
        if (contentType == null) {
            contentType =  "text/plain";
            int suffixOffset;
            String suffix;

            if(( suffixOffset = url.getPath().lastIndexOf( '.' )) > 0 &&
                    ( suffix = url.getPath().substring( suffixOffset + 1 ))
!= null &&
                    suffix.length() > 1 && suffix.length() < 6 ) {
                try {
                    if (mimeMap == null) {
                        // Don't initialize it unless necessary
                        mimeMap = new MimeMap();
                    }
                    contentType =  mimeMap.getMimeType( suffix );
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return contentType;
    }



Question.  Is it safe enough to consider the default to be text?


On 11/14/06, Michael B Allen <mba2000 at ioplex.com> wrote:
>
> There's a mime.map file in the jar. Look at
> jcifs/http/NetworkExplorer.java for how it comes up with the Content-Type.
>
> But your fix should be added too I think. I'll add it to TheList [1].
>
> Mike
>
> [1] Note that I just processed The List so it will likely be another
> year or two before this fix reaches mainline :-<
>
> On Tue, 14 Nov 2006 13:22:25 -0500
> "Thierry Lach" <thierry.lach at gmail.com> wrote:
>
> > I'm trying to access a files on a unix system through a Samba share from
> > java inside of a web application.  I want to keep the password
> configuration
> > separate from the url for security purposes.
> >
> > I'm using jcifs.Config to set all of the properties, and can access the
> smb:
> > protocol properly.  But when I try to access the file contents (as in
> the
> > following example) I get an error indicating that the content type has
> not
> > been set..
> >
> >     URL url = new URL("smb://hostname/sharename/filename.xml");
> >     URLConnection conn = url.openConnection();
> >     Object contents = conn.getContent();
> >     if (contents instanceof InputStream) {
> >         BufferedReader reader = new BufferedReader(new
> > InputStreamReader((InputStream) contents));
> >         String line;
> >         while ( null != (line = reader.readLine())) {
> >             pw.println(line);
> >         }
> >         ((InputStream)contents).close();
> >     }
> >
> >
> > I added the following code to SmbFile and now it works...
> >
> >     /**
> >      * This URLConnection method returns a generic content type.
> >      *
> >      * @see java.net.URLConnection#getContentType()
> >      */
> >     public String getContentType() {
> >         String contentType = super.getContentType();
> >         if (contentType == null) {
> >             // Call it binary since we don't know any better
> >             contentType = "application/octet-stream";
> >         }
> >         return contentType;
> >     }
> >
> > This can probably look at file extensions to better select a content
> type,
> > but this is a minimalist fix.
> >
>
>
> --
> Michael B Allen
> PHP Active Directory SSO
> http://www.ioplex.com/
>
-------------- next part --------------
HTML attachment scrubbed and removed


More information about the jcifs mailing list